def test_on_ready(self, requeue_worker_mock, fwdmsg_mock): worker_id = '9f9z' msgid = 'msg18' msg = [] waiting_msg = [ '', constants.PROTOCOL_VERSION, 'REQUEST', msgid, 'default', '', 'hello world' ] self.router.workers = { worker_id: { 'queues': [ (10, 'default'), ], 'hb': 123.2, 'available_slots': 3, }, } self.router.waiting_messages['default'] = EMQdeque(initial=[ waiting_msg, ]) self.router.on_ready(worker_id, msgid, msg) fwdmsg_mock.assert_called_with(self.router.outgoing, worker_id, waiting_msg) self.router.on_ready(worker_id, msgid + 'a', msg) self.router.requeue_worker.assert_called_with(worker_id)
def test_router_status(self): msgid1 = 'msg21' msgid2 = 'msg19' msgid3 = 'msg6' queue1_id = 'default' queue2_id = 'scallywag' # Worker ids w1 = 'w1' w2 = 'w2' w3 = 'w3' waiting_msg1 = [ '', constants.PROTOCOL_VERSION, 'REQUEST', msgid1, 'kun', '', 'hello world' ] waiting_msg2 = [ '', constants.PROTOCOL_VERSION, 'REQUEST', msgid2, 'kun', '', 'world hello' ] waiting_msg3 = [ '', constants.PROTOCOL_VERSION, 'REQUEST', msgid3, 'blu', '', 'goodbye' ] self.router.queues = { queue1_id: [(10, w1), (0, w2)], queue2_id: [(10, w3), (10, w2)], } t = monotonic() self.router.workers = { w1: { 'queues': [ (10, queue1_id), ], 'hb': t, 'available_slots': 0, }, w2: { 'queues': [(0, queue1_id), (10, queue2_id)], 'hb': t, 'available_slots': 2, }, w3: { 'queues': [ (10, queue2_id), ], 'hb': t, 'available_slots': 0, } } self.router.waiting_messages = { 'kun': EMQdeque(initial=[waiting_msg1, waiting_msg2]), 'blu': EMQdeque(initial=[ waiting_msg3, ]), } # hacky, but the serialize/deserialize converts the keys to unicode # correctly and what not. self.assertEqual( json.loads( json.dumps({ 'job_latencies': self.router.job_latencies, 'executed_functions': self.router.executed_functions, 'waiting_message_counts': [ '{}: {}'.format(q, len(self.router.waiting_messages[q])) for q in self.router.waiting_messages ] # noqa })), json.loads(self.router.get_status())) self.assertEqual( json.loads( json.dumps({ 'connected_workers': self.router.workers, 'connected_queues': self.router.queues })), json.loads(self.router.get_workers_status())) self.assertEqual( json.loads( json.dumps({'connected_schedulers': self.router.schedulers})), json.loads(self.router.get_schedulers_status()))
def test_on_ready_multpile_queues(self, requeue_worker_mock, fwdmsg_mock): # Test that if messages are waiting on multiple queues, they are # dispatched immediatly after a READY message. worker1_id = 'w1' worker2_id = 'w2' msgid1 = 'msg21' msgid2 = 'msg19' msgid3 = 'msg6' waiting_msg1 = [ '', constants.PROTOCOL_VERSION, 'REQUEST', msgid1, 'kun', '', 'hello world' ] waiting_msg2 = [ '', constants.PROTOCOL_VERSION, 'REQUEST', msgid2, 'kun', '', 'world hello' ] waiting_msg3 = [ '', constants.PROTOCOL_VERSION, 'REQUEST', msgid3, 'blu', '', 'goodbye' ] self.router.workers = { worker1_id: { 'queues': [(10, 'kun'), (0, 'blu')], 'hb': 123.2, 'available_slots': 0, }, worker2_id: { 'queues': [(10, 'blu'), (0, 'kun')], 'hb': 123.2, 'available_slots': 0 } } self.router.queues = { 'kun': [(10, worker1_id), (0, worker2_id)], 'blu': [(10, worker2_id), (0, worker1_id)], } self.router.waiting_messages = { 'kun': EMQdeque(initial=[waiting_msg1, waiting_msg2]), 'blu': EMQdeque(initial=[ waiting_msg3, ]), } # Forward waiting_msg1 ready_msgid1 = 'ready23' self.router.on_ready(worker1_id, ready_msgid1, ['READY', ready_msgid1]) fwdmsg_mock.assert_called_with(self.router.outgoing, worker1_id, waiting_msg1) # Forward waiting_msg3 -- blu is a higher priority for worker2 ready_msgid3 = 'ready19' self.router.on_ready(worker2_id, ready_msgid3, ['READY', ready_msgid3]) fwdmsg_mock.assert_called_with(self.router.outgoing, worker2_id, waiting_msg3) # Forward waiting_msg2 ready_msgid2 = 'ready5' self.router.on_ready(worker2_id, ready_msgid2, ['READY', ready_msgid2]) fwdmsg_mock.assert_called_with(self.router.outgoing, worker2_id, waiting_msg2) # There should be no keys because the code checks for their existence # to know if there is a waiting message self.assertEqual(0, len(self.router.waiting_messages.keys())) # No waiting messages self.router.on_ready(worker1_id, ready_msgid1, ['READY', ready_msgid1]) requeue_worker_mock.assert_called_with(worker1_id) self.router.on_ready(worker2_id, ready_msgid2, ['READY', ready_msgid2]) requeue_worker_mock.assert_called_with(worker2_id)