@@ -24,11 +24,12 @@ def print_tests():
24
24
test(0) Print this list.
25
25
test(1) Test message acknowledge.
26
26
test(2) Test Messge and Lock objects.
27
- test(3) Test the Barrier class.
28
- test(4) Test Semaphore
29
- test(5) Test BoundedSemaphore.
30
- test(6) Test the Condition class.
31
- test(7) Test the Queue class.
27
+ test(3) Test the Barrier class with callback.
28
+ test(4) Test the Barrier class with coroutine.
29
+ test(5) Test Semaphore
30
+ test(6) Test BoundedSemaphore.
31
+ test(7) Test the Condition class.
32
+ test(8) Test the Queue class.
32
33
'''
33
34
print ('\x1b [32m' )
34
35
print (st )
@@ -186,6 +187,49 @@ def barrier_test():
186
187
asyncio .create_task (report (barrier ))
187
188
asyncio .run (killer (2 ))
188
189
190
+ # ************ Barrier test 1 ************
191
+
192
+ async def my_coro (text ):
193
+ try :
194
+ await asyncio .sleep_ms (0 )
195
+ while True :
196
+ await asyncio .sleep (1 )
197
+ print (text )
198
+ except asyncio .CancelledError :
199
+ print ('my_coro was cancelled.' )
200
+
201
+ async def report1 (barrier , x ):
202
+ await asyncio .sleep (x )
203
+ print ('report instance' , x , 'waiting' )
204
+ await barrier
205
+ print ('report instance' , x , 'done' )
206
+
207
+ async def bart ():
208
+ barrier = Barrier (4 , my_coro , ('my_coro running' ,))
209
+ for x in range (3 ):
210
+ asyncio .create_task (report1 (barrier , x ))
211
+ await barrier
212
+ # Must yield before reading result(). Here we wait long enough for
213
+ await asyncio .sleep_ms (1500 ) # coro to print
214
+ barrier .result ().cancel ()
215
+ await asyncio .sleep (2 )
216
+
217
+ def barrier_test1 ():
218
+ printexp ('''Running (runtime = 5s):
219
+ report instance 0 waiting
220
+ report instance 1 waiting
221
+ report instance 2 waiting
222
+ report instance 2 done
223
+ report instance 1 done
224
+ report instance 0 done
225
+ my_coro running
226
+ my_coro was cancelled.
227
+
228
+ Exact report instance done sequence may vary, but 3 instances should report
229
+ done before my_coro runs.
230
+ ''' , 5 )
231
+ asyncio .run (bart ())
232
+
189
233
# ************ Semaphore test ************
190
234
191
235
async def run_sema (n , sema , barrier ):
@@ -373,12 +417,14 @@ def test(n):
373
417
elif n == 3 :
374
418
barrier_test () # Test the Barrier class.
375
419
elif n == 4 :
376
- semaphore_test ( False ) # Test Semaphore
420
+ barrier_test1 () # Test the Barrier class.
377
421
elif n == 5 :
378
- semaphore_test (True ) # Test BoundedSemaphore.
422
+ semaphore_test (False ) # Test Semaphore
379
423
elif n == 6 :
380
- condition_test ( ) # Test the Condition class .
424
+ semaphore_test ( True ) # Test BoundedSemaphore .
381
425
elif n == 7 :
426
+ condition_test () # Test the Condition class.
427
+ elif n == 8 :
382
428
queue_test () # Test the Queue class.
383
429
except KeyboardInterrupt :
384
430
print ('Interrupted' )
0 commit comments