@@ -195,136 +195,144 @@ async def is_type_of_baz(obj, *_args):
195195 None ,
196196 )
197197
198- @pytest .mark .asyncio
199- async def cancel_selection_sets_on_exception ():
200- barrier = Barrier (2 )
201- completed = False
202- 203- async def succeed (* _args ):
204- nonlocal completed
205- await barrier .wait ()
206- completed = True # pragma: no cover
207- 208- async def fail (* _args ):
209- raise RuntimeError ("Oops" )
210- 211- schema = GraphQLSchema (
212- GraphQLObjectType (
213- "Query" ,
214- {
215- "foo" : GraphQLField (GraphQLNonNull (GraphQLBoolean ), resolve = fail ),
216- "bar" : GraphQLField (GraphQLBoolean , resolve = succeed ),
217- },
198+ def describe_cancel_on_exception ():
199+ """Tests for cancellation of parallel execution on exception.
200+
201+ These tests are specifically targeted at the Python asyncio implementation.
202+ """
203+ 204+ @pytest .mark .asyncio
205+ async def cancel_selection_sets ():
206+ barrier = Barrier (2 )
207+ completed = False
208+ 209+ async def succeed (* _args ):
210+ nonlocal completed
211+ await barrier .wait ()
212+ completed = True # pragma: no cover
213+ 214+ async def fail (* _args ):
215+ raise RuntimeError ("Oops" )
216+ 217+ schema = GraphQLSchema (
218+ GraphQLObjectType (
219+ "Query" ,
220+ {
221+ "foo" : GraphQLField (
222+ GraphQLNonNull (GraphQLBoolean ), resolve = fail
223+ ),
224+ "bar" : GraphQLField (GraphQLBoolean , resolve = succeed ),
225+ },
226+ )
218227 )
219- )
220228
221- ast = parse ("{foo, bar}" )
229+ ast = parse ("{foo, bar}" )
222230
223- awaitable_result = execute (schema , ast )
224- assert isinstance (awaitable_result , Awaitable )
225- result = await asyncio .wait_for (awaitable_result , 1 )
231+ awaitable_result = execute (schema , ast )
232+ assert isinstance (awaitable_result , Awaitable )
233+ result = await asyncio .wait_for (awaitable_result , 1 )
226234
227- assert result == (
228- None ,
229- [{"message" : "Oops" , "locations" : [(1 , 2 )], "path" : ["foo" ]}],
230- )
231- 232- assert not completed
233- 234- # Unblock succeed() and check that it does not complete
235- await barrier .wait ()
236- await asyncio .sleep (0 )
237- assert not completed
235+ assert result == (
236+ None ,
237+ [{"message" : "Oops" , "locations" : [(1 , 2 )], "path" : ["foo" ]}],
238+ )
238239
239- @pytest .mark .asyncio
240- async def cancel_lists_on_exception ():
241- barrier = Barrier (2 )
242- completed = False
240+ assert not completed
243241
244- async def succeed (* _args ):
245- nonlocal completed
242+ # Unblock succeed() and check that it does not complete
246243 await barrier .wait ()
247- completed = True # pragma: no cover
248- 249- async def fail (* _args ):
250- raise RuntimeError ("Oops" )
251- 252- async def resolve_list (* args ):
253- return [fail (* args ), succeed (* args )]
254- 255- schema = GraphQLSchema (
256- GraphQLObjectType (
257- "Query" ,
258- {
259- "foo" : GraphQLField (
260- GraphQLList (GraphQLNonNull (GraphQLBoolean )),
261- resolve = resolve_list ,
262- )
263- },
244+ await asyncio .sleep (0 )
245+ assert not completed
246+ 247+ @pytest .mark .asyncio
248+ async def cancel_lists ():
249+ barrier = Barrier (2 )
250+ completed = False
251+ 252+ async def succeed (* _args ):
253+ nonlocal completed
254+ await barrier .wait ()
255+ completed = True # pragma: no cover
256+ 257+ async def fail (* _args ):
258+ raise RuntimeError ("Oops" )
259+ 260+ async def resolve_list (* args ):
261+ return [fail (* args ), succeed (* args )]
262+ 263+ schema = GraphQLSchema (
264+ GraphQLObjectType (
265+ "Query" ,
266+ {
267+ "foo" : GraphQLField (
268+ GraphQLList (GraphQLNonNull (GraphQLBoolean )),
269+ resolve = resolve_list ,
270+ )
271+ },
272+ )
264273 )
265- )
266274
267- ast = parse ("{foo}" )
275+ ast = parse ("{foo}" )
268276
269- awaitable_result = execute (schema , ast )
270- assert isinstance (awaitable_result , Awaitable )
271- result = await asyncio .wait_for (awaitable_result , 1 )
277+ awaitable_result = execute (schema , ast )
278+ assert isinstance (awaitable_result , Awaitable )
279+ result = await asyncio .wait_for (awaitable_result , 1 )
272280
273- assert result == (
274- {"foo" : None },
275- [{"message" : "Oops" , "locations" : [(1 , 2 )], "path" : ["foo" , 0 ]}],
276- )
277- 278- assert not completed
279- 280- # Unblock succeed() and check that it does not complete
281- await barrier .wait ()
282- await asyncio .sleep (0 )
283- assert not completed
281+ assert result == (
282+ {"foo" : None },
283+ [{"message" : "Oops" , "locations" : [(1 , 2 )], "path" : ["foo" , 0 ]}],
284+ )
284285
285- @pytest .mark .asyncio
286- async def cancel_async_iterator_on_exception ():
287- barrier = Barrier (2 )
288- completed = False
286+ assert not completed
289287
290- async def succeed (* _args ):
291- nonlocal completed
288+ # Unblock succeed() and check that it does not complete
292289 await barrier .wait ()
293- completed = True # pragma: no cover
294- 295- async def fail (* _args ):
296- raise RuntimeError ("Oops" )
297- 298- async def resolve_iterator (* args ):
299- yield fail (* args )
300- yield succeed (* args )
301- 302- schema = GraphQLSchema (
303- GraphQLObjectType (
304- "Query" ,
305- {
306- "foo" : GraphQLField (
307- GraphQLList (GraphQLNonNull (GraphQLBoolean )),
308- resolve = resolve_iterator ,
309- )
310- },
290+ await asyncio .sleep (0 )
291+ assert not completed
292+ 293+ @pytest .mark .asyncio
294+ async def cancel_async_iterators ():
295+ barrier = Barrier (2 )
296+ completed = False
297+ 298+ async def succeed (* _args ):
299+ nonlocal completed
300+ await barrier .wait ()
301+ completed = True # pragma: no cover
302+ 303+ async def fail (* _args ):
304+ raise RuntimeError ("Oops" )
305+ 306+ async def resolve_iterator (* args ):
307+ yield fail (* args )
308+ yield succeed (* args )
309+ 310+ schema = GraphQLSchema (
311+ GraphQLObjectType (
312+ "Query" ,
313+ {
314+ "foo" : GraphQLField (
315+ GraphQLList (GraphQLNonNull (GraphQLBoolean )),
316+ resolve = resolve_iterator ,
317+ )
318+ },
319+ )
311320 )
312- )
313321
314- ast = parse ("{foo}" )
322+ ast = parse ("{foo}" )
315323
316- awaitable_result = execute (schema , ast )
317- assert isinstance (awaitable_result , Awaitable )
318- result = await asyncio .wait_for (awaitable_result , 1 )
324+ awaitable_result = execute (schema , ast )
325+ assert isinstance (awaitable_result , Awaitable )
326+ result = await asyncio .wait_for (awaitable_result , 1 )
319327
320- assert result == (
321- {"foo" : None },
322- [{"message" : "Oops" , "locations" : [(1 , 2 )], "path" : ["foo" , 0 ]}],
323- )
328+ assert result == (
329+ {"foo" : None },
330+ [{"message" : "Oops" , "locations" : [(1 , 2 )], "path" : ["foo" , 0 ]}],
331+ )
324332
325- assert not completed
333+ assert not completed
326334
327- # Unblock succeed() and check that it does not complete
328- await barrier .wait ()
329- await asyncio .sleep (0 )
330- assert not completed
335+ # Unblock succeed() and check that it does not complete
336+ await barrier .wait ()
337+ await asyncio .sleep (0 )
338+ assert not completed
0 commit comments