def flow(): try: queue = dtid() consumer_id = mqks.consume(queue=queue, events=[queue], on_msg=lambda msg: None, delete_queue_when_unused=1, confirm=True) for _ in xrange(10): mqks.publish(queue, dtid()) time.sleep(1) mqks.delete_consumer(consumer_id) except Exception: crit()
def consumer_killer(): while 1: try: for consumer_id, params in state['consumers'].iteritems(): if params['consumer_expired_time'] is None or time.time( ) < params['consumer_expired_time']: continue mqks.delete_consumer(consumer_id) del state['consumers'][consumer_id] except RuntimeError: pass time.sleep(1)
def test_rebind(self): # Consume: on_msg = Mock() consumer_id = mqks.consume('q1', ['e1', 'e2', 'u.42.e1', 'u.43.e1'], on_msg, confirm=True) # "replace" was tested in "test_update_consumer". # Testing other args here: mqks.rebind('q1', remove=['e2'], add=['e3', 'e4'], remove_mask=['u.*.e1'], confirm=True) worker = mqks.get_worker('q1') self.assertEqual(mqks.state['consumers'][worker][consumer_id], 'q1 e1 e3 e4') # Cleanup: mqks.delete_consumer(consumer_id, confirm=True) mqks.delete_queue('q1', confirm=True)
def test_update_consumer(self): # Consume: on_msg = Mock() on_disconnect = Mock() on_reconnect = Mock() consumer_id = mqks.consume('q1', ['e1', 'e2'], on_msg, on_disconnect=on_disconnect, on_reconnect=on_reconnect, delete_queue_when_unused=5.0, manual_ack=True, confirm=True) worker = mqks.get_worker('q1') self.assertEqual( mqks.state['consumers'][worker][consumer_id], 'q1 e1 e2 --delete-queue-when-unused=5.0 --manual-ack') # Rebind leads to "update consumer" flow: mqks.rebind('q1', ['e2', 'e3'], confirm=True) self.assertEqual( mqks.state['consumers'][worker][consumer_id], 'q1 e2 e3 --delete-queue-when-unused=5.0 --manual-ack') self.assertEqual( mqks._eval("' '.join(sorted(state.events_by_queues['q1']))", worker=worker), 'e2 e3') # Reconnect: old_client = mqks._eval("request['client']", worker=worker) try: mqks._eval("state.socks_by_clients[request['client']].shutdown(2)", worker=worker, timeout=1) # Disconnect. except gevent.Timeout: pass gevent.sleep(2) # Reconnect. self.assertEqual(on_disconnect.called, 1) self.assertEqual(on_reconnect.called, 1) # consumer_id is updated: self.assertEqual(on_reconnect.args[0], consumer_id) self.assertNotEqual(on_reconnect.args[1], consumer_id) consumer_id = on_reconnect.args[1] # client id is updated: new_client = mqks._eval("request['client']", worker=worker) self.assertNotEqual(old_client, new_client) # Reconsume used last consumer config: self.assertEqual( mqks._eval("' '.join(sorted(state.events_by_queues['q1']))", worker=worker), 'e2 e3') # Message delivery is still working after reconnect: self.assertEqual(on_msg.called, 0) mqks.publish('e3', 'd1', confirm=True) if not on_msg.called: gevent.sleep(0.1) self.assertEqual(on_msg.called, 1) self.assertEqual(on_msg.args[0]['data'], 'd1') # Add events: mqks.delete_consumer(consumer_id, confirm=True) consumer_id = mqks.consume('q1', ['e3', 'e4'], on_msg, delete_queue_when_unused=5.0, manual_ack=True, add_events=True, confirm=True) self.assertEqual( mqks.state['consumers'][worker][consumer_id], 'q1 e2 e3 e4 --delete-queue-when-unused=5.0 --manual-ack') self.assertEqual( mqks._eval("' '.join(sorted(state.events_by_queues['q1']))", worker=worker), 'e2 e3 e4') # Cleanup: mqks.delete_consumer(consumer_id, confirm=True) mqks.delete_queue('q1', confirm=True)
def test_manual_ack(self): # Consume: on_msg = Mock() consumer_id = mqks.consume('q1', ['e1'], on_msg, manual_ack=True, confirm=True) # Publish: mqks.publish('e1', 'd1', confirm=True) # Original message: if not on_msg.called: gevent.sleep(0.1) self.assertEqual(on_msg.called, 1) msg = on_msg.args[0] self.assertEqual(msg['data'], 'd1') self.assertNotIn('retry', msg) # Delete consumer without ack, consume again: mqks.delete_consumer(consumer_id, confirm=True) consumer_id = mqks.consume('q1', ['e1'], on_msg, manual_ack=True, confirm=True) # Retried message: if on_msg.called == 1: gevent.sleep(0.1) self.assertEqual(on_msg.called, 2) msg = on_msg.args[0] self.assertEqual(msg['data'], 'd1') self.assertEqual(msg['retry'], '1') # Ack, delete_consumer, consume, no retries: msg['ack'](confirm=True) mqks.delete_consumer(consumer_id, confirm=True) consumer_id = mqks.consume('q1', ['e1'], on_msg, manual_ack=True, confirm=True) gevent.sleep(0.5) self.assertEqual(on_msg.called, 2) # Publish, get original, reject, no other consumers, get retry: mqks.publish('e1', 'd2', confirm=True) if on_msg.called == 2: gevent.sleep(0.1) self.assertEqual(on_msg.called, 3) msg = on_msg.args[0] self.assertEqual(msg['data'], 'd2') self.assertNotIn('retry', msg) msg['reject'](confirm=True) if on_msg.called == 3: gevent.sleep(0.1) self.assertEqual(on_msg.called, 4) msg = on_msg.args[0] self.assertEqual(msg['data'], 'd2') self.assertEqual(msg['retry'], '1') # Reject all, no other consumers, get retry: mqks.reject_all(consumer_id, confirm=True) if on_msg.called == 4: gevent.sleep(0.1) self.assertEqual(on_msg.called, 5) msg = on_msg.args[0] self.assertEqual(msg['data'], 'd2') self.assertEqual(msg['retry'], '2') # Ack all, delete_consumer, consume, no retries: mqks.ack_all(consumer_id, confirm=True) mqks.delete_consumer(consumer_id, confirm=True) consumer_id = mqks.consume('q1', ['e1'], on_msg, manual_ack=True, confirm=True) gevent.sleep(0.5) self.assertEqual(on_msg.called, 5) # Cleanup: mqks.delete_consumer(consumer_id, confirm=True) mqks.delete_queue('q1', confirm=True)