async def adder(self, p: BarrierState): for i in range(100): assert p.size == i r = ReplyPromise(reply_to=p.reply_to, correlation_id=str(i)) p.add(r) assert len(p.pending) == i + 1 await asyncio.sleep(0.5) assert p.size == i + 1
async def test_sync_join(self): p = BarrierState(reply_to='rt') assert not p.pending assert p.size == 0 await self.adder(p) await self.fulfiller(p) await self.finalizer(p, 0.0) await self.joiner(p) assert p.get_nowait()
async def test_sync_iterate(self): p = BarrierState(reply_to='rt') assert not p.pending assert p.size == 0 await self.adder(p) await self.finalizer(p, 0.0) await self.fulfiller(p) assert p.done() await self.consumer(p)
async def test_iterate__completion(self): p = BarrierState(reply_to='rt') p.done = Mock(name='done') p.done.return_value = False p._results.put_nowait(None) p._results.get = AsyncMock(name='get') def se(): p.done.return_value = True return None p._results.get.coro.side_effect = se assert [x async for x in p.iterate()] == []
async def test_parallel_iterate(self): p = BarrierState(reply_to='rt') assert not p.pending assert p.size == 0 done, pending = await asyncio.wait( [ self.adder(p), self.fulfiller(p), self.finalizer(p, 1.0), self.consumer(p), ], timeout=5.0, ) if pending: raise Exception(f'Test did not return in 5s:\n' f' DONE_TASKS={done}\n' f' PENDING_TASKS={pending}\n' f' size={p.size}\n' f' total={p.total}\n' f' fulfilled={p.fulfilled}\n' f' pending={len(p.pending)}\n' f' done={p.done()}' f' result={p.result() if p.done() else None}')
async def consumer(self, p: BarrierState): results = [None] * 100 async for correlation_id, value in p.iterate(): results[int(correlation_id)] = (correlation_id, value) for i, (correlation_id, value) in enumerate(results): assert correlation_id == str(i) assert value == str(i)
async def test_parallel_join(self): p = BarrierState(reply_to='rt') assert not p.pending assert p.size == 0 await asyncio.gather( self.adder(p), self.fulfiller(p), self.finalizer(p, 1.0), self.joiner(p), )
async def test_get_nowait__exhaust_sentinels(self): p = BarrierState(reply_to='rt') for _ in range(20): p._results.put_nowait(None) with pytest.raises(asyncio.QueueEmpty): p.get_nowait()
async def fulfiller(self, p: BarrierState): for i in range(100): await asyncio.sleep(0.01) p.fulfill(correlation_id=str(i), value=str(i))
async def finalizer(self, p: BarrierState, sleep_for): await asyncio.sleep(sleep_for) p.finalize() assert p.total == 100