Ejemplo n.º 1
0
        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
Ejemplo n.º 2
0
        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