From 438aedc9273b8095b4dc4d717d39ecae41ab4275 Mon Sep 17 00:00:00 2001 From: Maximilian Warsewa Date: Fri, 21 Sep 2018 12:07:15 +0000 Subject: [PATCH] Test parallel await --- tests/execution/test_executor.py | 43 ++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/tests/execution/test_executor.py b/tests/execution/test_executor.py index 2eebca49..1ec4f286 100644 --- a/tests/execution/test_executor.py +++ b/tests/execution/test_executor.py @@ -866,3 +866,46 @@ def resolve_field(self, parent_type, source, field_nodes, path): {"foo": "barbar"}, None, ) + + @mark.asyncio + async def resolve_fields_in_parallel(): + class Barrier(object): + # Makes progress only if at least `count` callers are `wait()`ing. + def __init__(self, count): + self.ev = asyncio.Event() + self.count = count + + async def wait(self): + self.count -= 1 + if self.count == 0: + self.ev.set() + + return await self.ev.wait() + + barrier = Barrier(2) + + async def f(*args): + return await barrier.wait() + + schema = GraphQLSchema( + GraphQLObjectType( + "Object", + { + "foo": GraphQLField(GraphQLBoolean, resolve=f), + "bar": GraphQLField(GraphQLBoolean, resolve=f), + } + ) + ) + + query = '{foo, bar}' + ast = parse(query) + + res = await asyncio.wait_for( + execute(schema, ast), + 1.0, # don't wait forever for the test to fail + ) + + assert res == ( + {"foo": True, "bar": True}, + None, + )