def test_future_as_completed(self): executor = futures.TaskPoolExecutor(10) def func(x): evergreen.sleep(x) return 42 def waiter(): l = [executor.submit(func, 0.001) for x in range(10)] for f in futures.as_completed(l): self.assertEqual(f.get(), 42) evergreen.spawn(waiter) self.loop.run()
def test_future_wait_multiple(self): executor = futures.TaskPoolExecutor(10) def func(): evergreen.sleep(0.001) return 42 def waiter(): f1 = executor.submit(func) f2 = executor.submit(func) done, not_done = futures.wait([f1, f2]) self.assertTrue(f1 in done and f2 in done) self.assertEqual(f1.get(), 42) self.assertEqual(f2.get(), 42) evergreen.spawn(waiter) self.loop.run()
def test_future_wait_multiple_wait_first(self): executor = futures.TaskPoolExecutor(10) def func(x): evergreen.sleep(x) return 42 def waiter(): f = executor.submit(func, 0.01) l = [f] for x in range(100): l.append(executor.submit(func, 100)) done, not_done = futures.wait(l, return_when=futures.FIRST_COMPLETED) self.assertTrue(f in done) self.assertEqual(len(not_done), 100) self.assertEqual(f.get(), 42) self.loop.stop() evergreen.spawn(waiter) self.loop.run()
def test_future_wait_multiple_exception(self): executor = futures.TaskPoolExecutor(10) def func(): evergreen.sleep(0.001) return 42 def raiser(): 1 / 0 def waiter(): f1 = executor.submit(raiser) f2 = executor.submit(func) f3 = executor.submit(func) done, not_done = futures.wait([f1, f2, f3], return_when=futures.FIRST_EXCEPTION) self.assertTrue(f1 in done) self.assertTrue(f2 in not_done and f3 in not_done) self.assertRaises(ZeroDivisionError, f1.get) self.loop.stop() evergreen.spawn(waiter) self.loop.run()