def test_wait_for_first_with_discard1(self): # test that uncompleted futures are cancelled fut1 = asyncio.Future(loop=self.loop) fut2 = asyncio.Future(loop=self.loop) self.loop.call_soon(lambda: fut2.set_result("done")) co = event.wait_for_first(fut1, fut2, loop=self.loop) result = self.loop.run_until_complete(co) self.assertEqual(result, "done") self.assertTrue(fut1.cancelled())
def test_wait_for_first_no_discard(self): fut1 = asyncio.Future(loop=self.loop) fut2 = asyncio.Future(loop=self.loop) self.loop.call_soon(lambda: fut2.set_result("done")) co = event.wait_for_first(fut1, fut2, discard_remaining=False, loop=self.loop) result = self.loop.run_until_complete(co) self.assertEqual(result, "done") self.assertFalse(fut1.cancelled())
def test_wait_for_first_raise_exc(self): # ensure raised exceptions are returned fut1 = asyncio.Future(loop=self.loop) fut2 = asyncio.Future(loop=self.loop) class TestExc(Exception): pass exc = TestExc('test exception') self.loop.call_soon(lambda: fut2.set_exception(exc)) co = event.wait_for_first(fut1, fut2, loop=self.loop) with self.assertRaises(TestExc): result = self.loop.run_until_complete(co) self.assertTrue(fut1.cancelled())
def test_wait_for_first_with_discard2(self): # test that racing completed futures are marked as done class Fut(asyncio.Future): def result(self): self.result_called = True return super().result() fut1 = Fut(loop=self.loop) fut2 = Fut(loop=self.loop) self.loop.call_soon(lambda: fut2.set_result("done")) self.loop.call_soon(lambda: fut1.set_result("done")) co = event.wait_for_first(fut1, fut2, loop=self.loop) result = self.loop.run_until_complete(co) self.assertEqual(result, "done") # don't care which future self.assertTrue(fut1.result_called) self.assertTrue(fut2.result_called)