def test_call_soon(self): ioloop = get_event_loop() tid = yield loop_thread_id(ioloop) d = Future() callback = lambda: d.set_result(current_thread().ident) cbk = ioloop.call_soon(callback) self.assertEqual(cbk._callback, callback) self.assertEqual(cbk._args, ()) # we should be able to wait less than a second result = yield d self.assertEqual(result, tid)
def test_yield(self): '''Yielding a future calling back on separate thread''' worker = pulsar.get_actor() loop = pulsar.get_request_loop() loop_tid = yield pulsar.loop_thread_id(loop) self.assertNotEqual(worker.tid, current_thread().ident) self.assertEqual(loop_tid, current_thread().ident) yield None self.assertEqual(loop_tid, current_thread().ident) d = Future() # We are calling back the future in the event_loop which is on # a separate thread def _callback(): d.set_result(current_thread().ident) worker._loop.call_later(0.2, _callback) result = yield d self.assertEqual(worker.tid, result) self.assertNotEqual(worker.tid, current_thread().ident) self.assertEqual(loop_tid, current_thread().ident)
def test_call_later(self): ioloop = get_event_loop() tid = yield loop_thread_id(ioloop) d = Future() timeout1 = ioloop.call_later( 20, lambda: d.set_result(current_thread().ident)) timeout2 = ioloop.call_later( 10, lambda: d.set_result(current_thread().ident)) # lets wake the ioloop self.assertTrue(has_callback(ioloop, timeout1)) self.assertTrue(has_callback(ioloop, timeout2)) timeout1.cancel() timeout2.cancel() self.assertTrue(timeout1._cancelled) self.assertTrue(timeout2._cancelled) timeout1 = ioloop.call_later( 0.1, lambda: d.set_result(current_thread().ident)) yield d self.assertTrue(d.done()) self.assertEqual(d.result(), tid) self.assertFalse(has_callback(ioloop, timeout1))
def test_yield(self): '''Yielding a future calling back on separate thread''' worker = pulsar.get_actor() loop = get_event_loop() loop_tid = yield pulsar.loop_thread_id(loop) self.assertNotEqual(worker.tid, current_thread().ident) self.assertEqual(loop_tid, current_thread().ident) yield None self.assertEqual(loop_tid, current_thread().ident) d = Future(loop=worker._loop) # We are calling back the future in the event_loop which is on # a separate thread def _callback(): d.set_result(current_thread().ident) worker._loop.call_soon_threadsafe( worker._loop.call_later, 0.2, _callback) result = yield d self.assertEqual(worker.tid, result) self.assertNotEqual(worker.tid, current_thread().ident) self.assertEqual(loop_tid, current_thread().ident)
def test_io_loop_tid(self): loop = pulsar.get_io_loop() self.assertTrue(loop) tid = yield loop_thread_id(loop) self.assertNotEqual(tid, current_thread().ident)
def test_io_loop(self): ioloop = get_event_loop() self.assertTrue(ioloop) tid = yield loop_thread_id(ioloop) self.assertNotEqual(tid, current_thread().ident)