-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #27 from syrusakbary/await-blocked-by-context-fix
Fix await blocked by context exit
- Loading branch information
Showing
5 changed files
with
104 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
from pytest import mark | ||
from promise import Promise | ||
from promise.dataloader import DataLoader | ||
|
||
|
||
def id_loader(**options): | ||
load_calls = [] | ||
|
||
resolve = options.pop('resolve', Promise.resolve) | ||
|
||
def fn(keys): | ||
load_calls.append(keys) | ||
return resolve(keys) | ||
|
||
identity_loader = DataLoader(fn, **options) | ||
return identity_loader, load_calls | ||
|
||
|
||
@mark.asyncio | ||
async def test_await_dataloader(): | ||
identity_loader, load_calls = id_loader() | ||
async def load_multiple(identity_loader): | ||
one = identity_loader.load('load1') | ||
two = identity_loader.load('load2') | ||
return await Promise.all([one, two]) | ||
|
||
result = await load_multiple(identity_loader) | ||
assert result == ['load1', 'load2'] | ||
assert load_calls == [['load1'], ['load2']] | ||
|
||
|
||
@mark.asyncio | ||
async def test_await_dataloader_safe_promise(): | ||
identity_loader, load_calls = id_loader() | ||
|
||
@Promise.safe | ||
async def load_multiple(identity_loader): | ||
one = identity_loader.load('load1') | ||
two = identity_loader.load('load2') | ||
return await Promise.all([one, two]) | ||
|
||
result = await load_multiple(identity_loader) | ||
assert result == ['load1', 'load2'] | ||
assert load_calls == [['load1'], ['load2']] | ||
|
||
|
||
@mark.asyncio | ||
async def test_await_dataloader_individual(): | ||
identity_loader, load_calls = id_loader() | ||
|
||
async def load_one_then_two(identity_loader): | ||
one = await identity_loader.load('load1') | ||
two = await identity_loader.load('load2') | ||
return [one, two] | ||
|
||
result = await load_one_then_two(identity_loader) | ||
assert result == ['load1', 'load2'] | ||
assert load_calls == [['load1'], ['load2']] | ||
|
||
|
||
@mark.asyncio | ||
async def test_await_dataloader_individual_safe_promise(): | ||
identity_loader, load_calls = id_loader() | ||
|
||
@Promise.safe | ||
async def load_one_then_two(identity_loader): | ||
one = await identity_loader.load('load1') | ||
two = await identity_loader.load('load2') | ||
return [one, two] | ||
|
||
result = await load_one_then_two(identity_loader) | ||
assert result == ['load1', 'load2'] | ||
assert load_calls == [['load1'], ['load2']] |