2017-03-06 11:16:34 +00:00
|
|
|
# Test that uasyncio scheduling is fair, i.e. gives all
|
|
|
|
# coroutines equal chance to run (this specifically checks
|
|
|
|
# round-robin scheduling).
|
|
|
|
import uasyncio.core as asyncio
|
|
|
|
|
|
|
|
|
2018-02-04 22:19:34 +00:00
|
|
|
COROS = 10
|
|
|
|
ITERS = 20
|
2017-03-06 11:16:34 +00:00
|
|
|
|
|
|
|
|
|
|
|
result = []
|
2018-02-04 22:19:34 +00:00
|
|
|
test_finished = False
|
2017-03-06 11:16:34 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def coro(n):
|
|
|
|
for i in range(ITERS):
|
|
|
|
result.append(n)
|
|
|
|
yield
|
|
|
|
|
|
|
|
|
|
|
|
async def done():
|
2018-02-04 22:19:34 +00:00
|
|
|
global test_finished
|
2017-03-06 11:16:34 +00:00
|
|
|
while True:
|
|
|
|
if len(result) == COROS * ITERS:
|
|
|
|
#print(result)
|
|
|
|
assert result == list(range(COROS)) * ITERS
|
2018-02-04 22:19:34 +00:00
|
|
|
test_finished = True
|
2017-03-06 11:16:34 +00:00
|
|
|
return
|
|
|
|
yield
|
|
|
|
|
|
|
|
|
|
|
|
loop = asyncio.get_event_loop()
|
|
|
|
|
|
|
|
for n in range(COROS):
|
|
|
|
loop.create_task(coro(n))
|
|
|
|
|
|
|
|
loop.run_until_complete(done())
|
2018-02-04 22:19:34 +00:00
|
|
|
|
|
|
|
assert test_finished
|