@@ -374,39 +374,97 @@ def condition_test():
374
374
# ************ Queue test ************
375
375
376
376
from primitives .queue import Queue
377
- q = Queue ()
378
377
379
378
async def slow_process ():
380
379
await asyncio .sleep (2 )
381
380
return 42
382
381
383
- async def bar ():
382
+ async def bar (q ):
384
383
print ('Waiting for slow process.' )
385
384
result = await slow_process ()
386
385
print ('Putting result onto queue' )
387
386
await q .put (result ) # Put result on q
388
387
389
- async def foo ():
388
+ async def foo (q ):
390
389
print ("Running foo()" )
391
390
result = await q .get ()
392
391
print ('Result was {}' .format (result ))
393
392
394
- async def queue_go (delay ):
395
- asyncio .create_task (foo ())
396
- asyncio .create_task (bar ())
397
- await asyncio .sleep (delay )
393
+ async def q_put (n , q ):
394
+ for x in range (8 ):
395
+ obj = (n , x )
396
+ await q .put (obj )
397
+ await asyncio .sleep (0 )
398
+
399
+ async def q_get (n , q ):
400
+ for x in range (8 ):
401
+ await q .get ()
402
+ await asyncio .sleep (0 )
403
+
404
+ async def putter (q ):
405
+ # put some item, then sleep
406
+ for _ in range (20 ):
407
+ await q .put (1 )
408
+ await asyncio .sleep_ms (50 )
409
+
410
+
411
+ async def getter (q ):
412
+ # checks for new items, and relies on the "blocking" of the get method
413
+ for _ in range (20 ):
414
+ await q .get ()
415
+
416
+ async def queue_go ():
417
+ q = Queue (10 )
418
+ asyncio .create_task (foo (q ))
419
+ asyncio .create_task (bar (q ))
420
+ await asyncio .sleep (3 )
421
+ for n in range (4 ):
422
+ asyncio .create_task (q_put (n , q ))
423
+ await asyncio .sleep (1 )
424
+ assert q .qsize () == 10
425
+ await q .get ()
426
+ await asyncio .sleep (0.1 )
427
+ assert q .qsize () == 10
428
+ while not q .empty ():
429
+ await q .get ()
430
+ await asyncio .sleep (0.1 )
431
+ assert q .empty ()
432
+ print ('Competing put tasks test complete' )
433
+
434
+ for n in range (4 ):
435
+ asyncio .create_task (q_get (n , q ))
436
+ await asyncio .sleep (1 )
437
+ x = 0
438
+ while not q .full ():
439
+ await q .put (x )
440
+ await asyncio .sleep (0.3 )
441
+ x += 1
442
+ assert q .qsize () == 10
443
+ print ('Competing get tasks test complete' )
444
+ await asyncio .gather (
445
+ putter (q ),
446
+ getter (q )
447
+ )
448
+ print ('Queue tests complete' )
398
449
print ("I've seen starships burn off the shoulder of Orion..." )
399
450
print ("Time to die..." )
400
451
401
452
def queue_test ():
402
- printexp ('''Running (runtime = 3s ):
453
+ printexp ('''Running (runtime = 20s ):
403
454
Running foo()
404
455
Waiting for slow process.
405
456
Putting result onto queue
457
+ Result was 42
458
+ Competing put tasks test complete
459
+ Competing get tasks test complete
460
+ Queue tests complete
461
+
462
+
406
463
I've seen starships burn off the shoulder of Orion...
407
464
Time to die...
408
- ''' , 3 )
409
- asyncio .run (queue_go (3 ))
465
+
466
+ ''' , 20 )
467
+ asyncio .run (queue_go ())
410
468
411
469
def test (n ):
412
470
try :
0 commit comments