def test_messages_are_deserialized_after_transport(mockstomp): """Test the message serialization.""" banana = {"entry": [0, "banana"]} banana_str = '{"entry": [0, "banana"]}' stomp = StompTransport() stomp.connect() mockconn = mockstomp.Connection.return_value message_handler = mockconn.set_listener.call_args[0][1].on_message # Test subscriptions callback = mock.Mock() stomp.subscribe("channel", callback) subscription_id = mockconn.subscribe.call_args[0][1] message_handler(_frame({"subscription": subscription_id}, banana_str)) callback.assert_called_once_with({"subscription": subscription_id}, banana) message_handler( _frame({"subscription": subscription_id}, mock.sentinel.undeserializable)) callback.assert_called_with({"subscription": subscription_id}, mock.sentinel.undeserializable) # Test broadcast subscriptions callback = mock.Mock() stomp.subscribe_broadcast("channel", callback) subscription_id = mockconn.subscribe.call_args[0][1] message_handler(_frame({"subscription": subscription_id}, banana_str)) callback.assert_called_once_with({"subscription": subscription_id}, banana) message_handler( _frame({"subscription": subscription_id}, mock.sentinel.undeserializable)) callback.assert_called_with({"subscription": subscription_id}, mock.sentinel.undeserializable) # Test subscriptions with mangling disabled callback = mock.Mock() stomp.subscribe("channel", callback, disable_mangling=True) subscription_id = mockconn.subscribe.call_args[0][1] message_handler(_frame({"subscription": subscription_id}, banana_str)) callback.assert_called_once_with({"subscription": subscription_id}, banana_str) # Test broadcast subscriptions with mangling disabled callback = mock.Mock() stomp.subscribe_broadcast("channel", callback, disable_mangling=True) subscription_id = mockconn.subscribe.call_args[0][1] message_handler(_frame({"subscription": subscription_id}, banana_str)) callback.assert_called_once_with({"subscription": subscription_id}, banana_str)
class Worker(object): def __init__(self, wait_time=5): self.wait_time = wait_time self.heartbeat_timestamp = None self.transport = StompTransport() self.transport.connect() self.transport.subscribe_broadcast("heartbeat", self.read_heartbeat) self.transport.subscribe("outbound", self.read) self.wait() def timeout(self): from time import time if self.heartbeat_timestamp is None: self.heartbeat_timestamp = time() else: if time() - self.heartbeat_timestamp > self.wait_time: return True return False def wait(self): from time import time, sleep try: while not self.timeout(): sleep(1) except KeyboardInterrupt: pass def send(self, message): print("Sending") self.transport.send("inbound", message) def read_heartbeat(self, header, message): from time import time self.heartbeat_timestamp = time() def read(self, header, message): message = {"result": "Hey Hey!"} self.send(message)
def receive_message_but_exit_on_error(*args, **kwargs): try: receive_message(*args, **kwargs) except KeyboardInterrupt: print("Terminating.") sys.exit(0) except Exception as e: print("Uncaught exception:", e) print("Terminating.") sys.exit(1) stomp = StompTransport() stomp.connect() stomp.subscribe('processing_ingest', receive_message_but_exit_on_error, acknowledgement=True) stomp.subscribe('ispyb.processing_ingest', receive_message_but_exit_on_error, acknowledgement=True, ignore_namespace=True) stomp.subscribe('zocalo.ispyb', receive_message_but_exit_on_error, acknowledgement=True, ignore_namespace=True) # Run for max 24 hrs, then terminate. Service will be restarted automatically. try: time.sleep(24 * 3600) except KeyboardInterrupt: print("Terminating.")