def test_future_wait_module_function(self): # Instead of waiting on the result, we can wait # on the future using the module functions self.assertEqual(monkey.is_module_patched('threading'), self.MONKEY_PATCHED) pool = self.pool def fn(): gevent.sleep(0.5) return 42 future = pool.submit(fn) if self.MONKEY_PATCHED: # Things work as expected when monkey-patched _done, not_done = cf_wait((future, ), timeout=0.001) self.assertEqual(list(not_done), [future]) def spawned(): return 2016 spawned_greenlet = gevent.spawn(spawned) done, _not_done = cf_wait((future, )) self.assertEqual(list(done), [future]) self.assertTrue(spawned_greenlet.ready()) self.assertEqual(spawned_greenlet.value, 2016) else: # When not monkey-patched, raises an AttributeError self.assertRaises(AttributeError, cf_wait, (future, )) pool.kill() del future del pool del self.pool
def test_future_wait_module_function(self): # Instead of waiting on the result, we can wait # on the future using the module functions self.assertEqual(monkey.is_module_patched('threading'), self.MONKEY_PATCHED) pool = self.pool def fn(): gevent.sleep(0.5) return 42 future = pool.submit(fn) # pylint:disable=no-member if self.MONKEY_PATCHED: # Things work as expected when monkey-patched _done, not_done = cf_wait((future,), timeout=0.001) self.assertEqual(list(not_done), [future]) def spawned(): return 2016 spawned_greenlet = gevent.spawn(spawned) done, _not_done = cf_wait((future,)) self.assertEqual(list(done), [future]) self.assertTrue(spawned_greenlet.ready()) self.assertEqual(spawned_greenlet.value, 2016) else: # When not monkey-patched, raises an AttributeError self.assertRaises(AttributeError, cf_wait, (future,)) pool.kill() del future del pool del self.pool
def test_future(self): self.assertEqual(monkey.is_module_patched('threading'), self.MONKEY_PATCHED) pool = self.pool calledback = [] def fn(): gevent.sleep(0.5) return 42 def callback(future): future.calledback += 1 raise greentest.ExpectedException("Expected, ignored") future = pool.submit(fn) future.calledback = 0 future.add_done_callback(callback) self.assertRaises(FutureTimeoutError, future.result, timeout=0.001) def spawned(): return 2016 spawned_greenlet = gevent.spawn(spawned) # Whether or not we are monkey patched, the background # greenlet we spawned got to run while we waited. self.assertEqual(future.result(), 42) self.assertTrue(future.done()) self.assertFalse(future.cancelled()) # Make sure the notifier has a chance to run so the call back # gets called gevent.sleep() self.assertEqual(future.calledback, 1) self.assertTrue(spawned_greenlet.ready()) self.assertEqual(spawned_greenlet.value, 2016) # Adding the callback again runs immediately future.add_done_callback(lambda f: calledback.append(True)) self.assertEqual(calledback, [True]) # We can wait on the finished future done, _not_done = cf_wait((future, )) self.assertEqual(list(done), [future]) self.assertEqual(list(cf_as_completed((future, ))), [future]) # Doing so does not call the callback again self.assertEqual(future.calledback, 1) # even after a trip around the event loop gevent.sleep() self.assertEqual(future.calledback, 1) pool.kill() del future del pool del self.pool
def test_future(self): self.assertEqual(monkey.is_module_patched('threading'), self.MONKEY_PATCHED) pool = self.pool calledback = [] def fn(): gevent.sleep(0.5) return 42 def callback(future): future.calledback += 1 raise greentest.ExpectedException("Expected, ignored") future = pool.submit(fn) # pylint:disable=no-member future.calledback = 0 future.add_done_callback(callback) self.assertRaises(FutureTimeoutError, future.result, timeout=0.001) def spawned(): return 2016 spawned_greenlet = gevent.spawn(spawned) # Whether or not we are monkey patched, the background # greenlet we spawned got to run while we waited. self.assertEqual(future.result(), 42) self.assertTrue(future.done()) self.assertFalse(future.cancelled()) # Make sure the notifier has a chance to run so the call back # gets called gevent.sleep() self.assertEqual(future.calledback, 1) self.assertTrue(spawned_greenlet.ready()) self.assertEqual(spawned_greenlet.value, 2016) # Adding the callback again runs immediately future.add_done_callback(lambda f: calledback.append(True)) self.assertEqual(calledback, [True]) # We can wait on the finished future done, _not_done = cf_wait((future,)) self.assertEqual(list(done), [future]) self.assertEqual(list(cf_as_completed((future,))), [future]) # Doing so does not call the callback again self.assertEqual(future.calledback, 1) # even after a trip around the event loop gevent.sleep() self.assertEqual(future.calledback, 1) pool.kill() del future del pool del self.pool