def __call__(self, consumer, lag=1): rl = get_request_loop() start = time.time() loop = rl.num_loops yield async_sleep(lag) yield {'time': time.time() - start, 'loops': rl.num_loops - loop}
def test_NOT_DONE(self): worker = pulsar.get_actor() loop = pulsar.get_request_loop() count = loop.num_loops yield pulsar.NOT_DONE self.assertEqual(loop.num_loops, count+1) yield pulsar.NOT_DONE self.assertEqual(loop.num_loops, count+2)
def test_event_loop(self): '''Test event loop in test worker''' worker = pulsar.get_actor() loop = pulsar.get_request_loop() event_loop = get_event_loop() self.assertTrue(loop.is_running()) self.assertTrue(event_loop.is_running()) self.assertNotEqual(loop, event_loop) self.assertEqual(worker._loop, event_loop)
def test_event_loop(self): '''Test event loop in test worker''' worker = pulsar.get_actor() loop = pulsar.get_request_loop() event_loop = get_event_loop() self.assertTrue(loop.running) self.assertTrue(event_loop.running) self.assertNotEqual(loop, event_loop) self.assertEqual(worker.event_loop, event_loop) self.assertEqual(worker.tid, worker.event_loop.tid) self.assertNotEqual(worker.tid, loop.tid) self.assertTrue(str(event_loop))
def test_pool(self): pool = self.get_pool(threads=2) self.assertEqual(pool._state, 0) self.assertEqual(pool.status, 'running') self.assertEqual(pool.event_loop, get_event_loop()) self.assertNotEqual(pool.event_loop, get_request_loop()) #give a chance to start the pool yield async_while(3, lambda: not pool.num_threads) self.assertEqual(pool.num_threads, 2) pool.close() self.assertEqual(pool._state, 1) self.assertEqual(pool.status, 'closed') pool.join() yield async_while(3, lambda: pool.num_threads) self.assertFalse(pool.num_threads)
def test_yield(self): '''Yielding a deferred calling back on separate thread''' worker = pulsar.get_actor() loop = pulsar.get_request_loop() self.assertNotEqual(worker.tid, current_thread().ident) self.assertEqual(loop.tid, current_thread().ident) yield pulsar.NOT_DONE self.assertEqual(loop.tid, current_thread().ident) d = pulsar.Deferred() # We are calling back the deferred in the event_loop which is on # a separate thread def _callback(): d.callback(current_thread().ident) worker.event_loop.call_later(0.2, _callback) yield d self.assertEqual(worker.tid, d.result) self.assertEqual(worker.event_loop.tid, d.result) self.assertNotEqual(worker.tid, current_thread().ident) self.assertEqual(loop.tid, current_thread().ident)
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 testCPUbound(self): worker = pulsar.get_actor() loop = pulsar.get_request_loop() self.assertTrue(loop.cpubound) self.assertFalse(worker.event_loop.cpubound)
def test_request_loop(self): request_loop = pulsar.get_request_loop() event_loop = get_event_loop() self.assertNotEqual(event_loop, request_loop)
def test_event_loop(self): q1 = Queue(event_loop=get_request_loop()) q2 = Queue(event_loop=get_event_loop()) q3 = Queue() self.assertEqual(q2.event_loop, q3.event_loop) self.assertNotEqual(q1.event_loop, q3.event_loop)
def testCPUbound(self): worker = pulsar.get_actor() loop = pulsar.get_request_loop() self.assertIsInstance(loop, QueueEventLoop) self.assertNotIsInstance(worker._loop, QueueEventLoop)
def __init__(self, **kw): self._loop = get_request_loop() super(Handler, self).__init__(self._loop, **kw)