@@ -95,8 +95,8 @@ class BaseTaskTests:
95
95
Task = None
96
96
Future = None
97
97
98
- def new_task (self , loop , coro , name = 'TestTask' ):
99
- return self .__class__ .Task (coro , loop = loop , name = name )
98
+ def new_task (self , loop , coro , name = 'TestTask' , context = None ):
99
+ return self .__class__ .Task (coro , loop = loop , name = name , context = context )
100
100
101
101
def new_future (self , loop ):
102
102
return self .__class__ .Future (loop = loop )
@@ -2527,6 +2527,90 @@ async def main():
2527
2527
2528
2528
self .assertEqual (cvar .get (), - 1 )
2529
2529
2530
+ def test_context_4 (self ):
2531
+ cvar = contextvars .ContextVar ('cvar' )
2532
+
2533
+ async def coro (val ):
2534
+ await asyncio .sleep (0 )
2535
+ cvar .set (val )
2536
+
2537
+ async def main ():
2538
+ ret = []
2539
+ ctx = contextvars .copy_context ()
2540
+ ret .append (ctx .get (cvar ))
2541
+ t1 = self .new_task (loop , coro (1 ), context = ctx )
2542
+ await t1
2543
+ ret .append (ctx .get (cvar ))
2544
+ t2 = self .new_task (loop , coro (2 ), context = ctx )
2545
+ await t2
2546
+ ret .append (ctx .get (cvar ))
2547
+ return ret
2548
+
2549
+ loop = asyncio .new_event_loop ()
2550
+ try :
2551
+ task = self .new_task (loop , main ())
2552
+ ret = loop .run_until_complete (task )
2553
+ finally :
2554
+ loop .close ()
2555
+
2556
+ self .assertEqual ([None , 1 , 2 ], ret )
2557
+
2558
+ def test_context_5 (self ):
2559
+ cvar = contextvars .ContextVar ('cvar' )
2560
+
2561
+ async def coro (val ):
2562
+ await asyncio .sleep (0 )
2563
+ cvar .set (val )
2564
+
2565
+ async def main ():
2566
+ ret = []
2567
+ ctx = contextvars .copy_context ()
2568
+ ret .append (ctx .get (cvar ))
2569
+ t1 = asyncio .create_task (coro (1 ), context = ctx )
2570
+ await t1
2571
+ ret .append (ctx .get (cvar ))
2572
+ t2 = asyncio .create_task (coro (2 ), context = ctx )
2573
+ await t2
2574
+ ret .append (ctx .get (cvar ))
2575
+ return ret
2576
+
2577
+ loop = asyncio .new_event_loop ()
2578
+ try :
2579
+ task = self .new_task (loop , main ())
2580
+ ret = loop .run_until_complete (task )
2581
+ finally :
2582
+ loop .close ()
2583
+
2584
+ self .assertEqual ([None , 1 , 2 ], ret )
2585
+
2586
+ def test_context_6 (self ):
2587
+ cvar = contextvars .ContextVar ('cvar' )
2588
+
2589
+ async def coro (val ):
2590
+ await asyncio .sleep (0 )
2591
+ cvar .set (val )
2592
+
2593
+ async def main ():
2594
+ ret = []
2595
+ ctx = contextvars .copy_context ()
2596
+ ret .append (ctx .get (cvar ))
2597
+ t1 = loop .create_task (coro (1 ), context = ctx )
2598
+ await t1
2599
+ ret .append (ctx .get (cvar ))
2600
+ t2 = loop .create_task (coro (2 ), context = ctx )
2601
+ await t2
2602
+ ret .append (ctx .get (cvar ))
2603
+ return ret
2604
+
2605
+ loop = asyncio .new_event_loop ()
2606
+ try :
2607
+ task = loop .create_task (main ())
2608
+ ret = loop .run_until_complete (task )
2609
+ finally :
2610
+ loop .close ()
2611
+
2612
+ self .assertEqual ([None , 1 , 2 ], ret )
2613
+
2530
2614
def test_get_coro (self ):
2531
2615
loop = asyncio .new_event_loop ()
2532
2616
coro = coroutine_function ()
0 commit comments