def test_check_config_file_behaviour(mockstomp): """Check that a specified configuration file is read, that command line parameters have precedence and are passed on to the stomp layer.""" mockconn = mock.Mock() mockstomp.Connection.return_value = mockconn parser = optparse.OptionParser() stomp = StompTransport() stomp.add_command_line_options(parser) # Temporarily create an example stomp configuration file cfgfile = tempfile.NamedTemporaryFile(delete=False) try: cfgfile.write(""" # An example stomp configuration file # Only lines in the [stomp] block will be interpreted [stomp] #host = 127.0.0.1 port = 1234 username = someuser password = somesecret prefix = namespace """.encode("utf-8")) cfgfile.close() parser.parse_args( ["--stomp-conf", cfgfile.name, "--stomp-user", mock.sentinel.user]) # Command line parameters are shared for all instances stomp = StompTransport() stomp.connect() # Reset configuration for subsequent tests by reloading StompTransport importlib.reload(workflows.transport.stomp_transport) globals( )["StompTransport"] = workflows.transport.stomp_transport.StompTransport mockstomp.Connection.assert_called_once_with([("localhost", 1234)]) mockconn.connect.assert_called_once_with(mock.sentinel.user, "somesecret", wait=False) assert stomp.get_namespace() == "namespace" finally: os.remove(cfgfile.name) # Loading a non-existing configuration file with pytest.raises(workflows.Error): parser.parse_args(["--stomp-conf", ""])
def test_namespace_is_used_correctly(mockstomp): """Test that a configured namespace is correctly used when subscribing and sending messages.""" mockconn = mockstomp.Connection.return_value StompTransport.defaults["--stomp-prfx"] = "" stomp = StompTransport() stomp.connect() assert stomp.get_namespace() == "" StompTransport.defaults["--stomp-prfx"] = "ns." stomp = StompTransport() stomp.connect() assert stomp.get_namespace() == "ns" stomp._send("some_queue", mock.sentinel.message1) mockconn.send.assert_called_once() assert mockconn.send.call_args[0] == ( "/queue/ns.some_queue", mock.sentinel.message1, ) stomp._send("some_queue", mock.sentinel.message2, ignore_namespace=True) assert mockconn.send.call_args[0] == ("/queue/some_queue", mock.sentinel.message2) StompTransport.defaults["--stomp-prfx"] = "ns" stomp = StompTransport() stomp.connect() assert stomp.get_namespace() == "ns" stomp._send("some_queue", mock.sentinel.message1) assert mockconn.send.call_args[0] == ( "/queue/ns.some_queue", mock.sentinel.message1, ) stomp._broadcast("some_topic", mock.sentinel.message2) assert mockconn.send.call_args[0] == ( "/topic/ns.some_topic", mock.sentinel.message2, ) stomp._broadcast("some_topic", mock.sentinel.message3, ignore_namespace=True) assert mockconn.send.call_args[0] == ("/topic/some_topic", mock.sentinel.message3) stomp._subscribe(1, "sub_queue", None) mockconn.subscribe.assert_called_once() assert mockconn.subscribe.call_args[0] == ("/queue/ns.sub_queue", 1) stomp._subscribe(2, "sub_queue", None, ignore_namespace=True) assert mockconn.subscribe.call_args[0] == ("/queue/sub_queue", 2) stomp._subscribe_broadcast(3, "sub_topic", None) assert mockconn.subscribe.call_args[0] == ("/topic/ns.sub_topic", 3) stomp._subscribe_broadcast(4, "sub_topic", None, ignore_namespace=True) assert mockconn.subscribe.call_args[0] == ("/topic/sub_topic", 4) stomp.broadcast_status("some status") assert mockconn.send.call_args[0] == ("/topic/ns.transient.status", '"some status"')