class TestAsyncEvent(tornado.testing.AsyncTestCase): def setUp(self): super(TestAsyncEvent, self).setUp() self._event = AsyncEvent(self.io_loop) @tornado.testing.gen_test def test_timeout_of_until_set(self): @tornado.gen.coroutine def call_set(): self._event.set() # Test for timing out with self.assertRaises(tornado.gen.TimeoutError): yield self._event.until_set(timeout=0.1) # Test for NOT timing out self.io_loop.add_callback(call_set) yield self._event.until_set(timeout=0.1)
def test_monitor_resource_sync_state(self): m_res = mock.Mock() callback = mock.Mock() exit_event = AsyncEvent() synced = AsyncEvent() not_synced = AsyncEvent() m_res.until_synced = synced.until_set m_res.until_not_synced = not_synced.until_set def set_synced(sync): if sync: not_synced.clear() synced.set() else: synced.clear() not_synced.set() loop_done_future = resource_client.monitor_resource_sync_state( m_res, callback, exit_event) yield tornado.gen.moment self.assertEqual(callback.call_args_list, [mock.call(False)]) callback.reset_mock() # Check that it exits if exit_event is set exit_event.set() yield tornado.gen.moment self.assertFalse(callback.called, 'No callback should be made when exit_event is set') self.assertTrue(loop_done_future.done(), 'Monitor loop should terminate when exit_event is set') exit_event.clear() loop_done_future = resource_client.monitor_resource_sync_state( m_res, callback, exit_event) set_synced(True) yield tornado.gen.moment self.assertEqual(callback.call_args_list, [mock.call(False), mock.call(True)]) callback.reset_mock() set_synced(False) yield tornado.gen.moment self.assertEqual(callback.call_args_list, [mock.call(False)]) callback.reset_mock() # Now check exit_event when synced is set set_synced(True) yield tornado.gen.moment self.assertEqual(callback.call_args_list, [mock.call(True)]) callback.reset_mock() self.assertFalse(loop_done_future.done(), 'Monitor loop should only terminate is exit_event is set') exit_event.set() yield tornado.gen.moment self.assertFalse(callback.called) self.assertTrue(loop_done_future.done(), 'Monitor loop should terminate when exit_event is set')
def setUp(self): super(TestAsyncEvent, self).setUp() self._event = AsyncEvent(self.io_loop)