def test_send_no_connect(self):
     ebus = EventBus(options={'auto_connect': False})
     try:
         ebus.register_handler("echo-back", lambda x: print(x))
         self.fail("should not here")
     except Exception as e:
         self.assertEqual(str(e.args[0]), "socket has been closed.")
Ejemplo n.º 2
0
def setup_client():
    LOGGER.debug("Set up the client")
    client = EventBus(host=HOST, port=PORT)
    received_message_from_client = dict(data=None)

    def handle(text):
        received_message_from_client['body'] = text

    client.add_listen_func("api.versions", handle)
    return client, received_message_from_client
 def test_register_unregister(self):
     latch = CountDownLatch(2)
     ebus = EventBus(options={'connect': True})
     
     def list_handlers(message):
         handler_list = message['body']['list']
         addresses = [e['address'] for e in handler_list]
         if "first" in message['headers'] and message['headers']['first']:
             self.assertIn("list-handler", addresses)
             self.assertIn("test-handler", addresses)
             ebus.unregister_handler("test-handler")
             self.assertIn("list-handler", ebus.handlers())
             self.assertNotIn("test-handler", ebus.handlers())
             ebus.send("list", reply_address="list-handler")
             latch.count_down()
         else:
             self.assertIn("list-handler", addresses)
             self.assertNotIn("test-handler", addresses)
             ebus.close()
             latch.count_down()
         # check list handler again, no test-handler anymore !!
     
     ebus.register_handler("list-handler", list_handlers)
     ebus.register_handler("test-handler", lambda x: print(x))
     self.assertIn("list-handler", ebus.handlers())
     self.assertIn("test-handler", ebus.handlers())
     ebus.send("list", reply_address="list-handler", headers={'first': True})
     latch.awaits(5)
 def test_err_handler_bad_address(self):
     latch = CountDownLatch()
     
     def err_handler(message):
         self.assertEqual(message['type'], 'err')
         self.assertEqual(message['message'], 'access_denied')
         ebus.close()
         latch.count_down()
     ebus = EventBus(err_handler=err_handler)
     ebus.send('#-Bad_Address$', body={'name': 'python'})
     latch.awaits(5)
 def test_send_with_handler(self):
     latch = CountDownLatch()
     ebus = EventBus()
     ebus.connect()
     
     def handler(message):
         self.assertEqual(message['body']['hello'], 'world')
         latch.count_down()
     ebus.send("echo", body={'hello': 'world'}, reply_handler=handler)
     latch.awaits(5)
     ebus.close()
 def test_send(self):
     latch = CountDownLatch()
     ebus = EventBus()
     ebus.connect()
     
     def handler(message):
         self.assertEqual(message['body']['hello'], 'world')
         ebus.close()
         latch.count_down()
     ebus.register_handler("echo-back", handler)
     ebus.send("echo", reply_address="echo-back", body={'hello': 'world'})
     latch.awaits(5)
 def test_publish(self):
     latch = CountDownLatch()
     ebus = EventBus()
     ebus.connect()
     
     def handler(message):
         print("got publish messages back")
         self.assertEqual(message['body']['hello'], 'world')
         self.assertEqual(message['headers']['name'], 'vertx-python')
         ebus.close()
         latch.count_down()
     ebus.register_handler("publish-back", handler)
     ebus.publish("publish-back", headers={'name': 'vertx-python'}, body={'hello': 'world'})
     latch.awaits(5)
 def test_re_connect_with_registered_handlers(self):
     latch = CountDownLatch()
     
     def local_handler(message):
         self.assertEqual(message['type'], 'message')
         self.assertEqual(message['body']['name'], 'python')
         ebus.close()
         latch.count_down()
     ebus = EventBus()
     # register local before getting connect
     ebus._register_local("local-handler", local_handler)
     ebus.connect()
     ebus.send('local-handler', body={'name': 'python'})
     latch.awaits(5)
    def test_tls_client_auth_on(self):
        """
        Tests when tls.CLIENT_AUTH is enabled.
        """
        latch = CountDownLatch()
        starter = EventBusBridgeStarter(
            debug=True,
            conf={
                "server-options": {
                    "ssl": "true",
                    "clientAuth": "REQUIRED",
                    "pemKeyCertOptions": {
                        "keyPath": "test/systemtesting/ca.key",
                        "certPath": "test/systemtesting/ca.crt"
                    },
                    "pemTrustOptions": {
                        "certPaths": ["test/systemtesting/ca.crt"]
                    }
                }
            })
        try:
            starter.start()
            starter.wait_started()

            ebus = EventBus(
                options={
                    "debug": "True",
                    "ssl_options": {
                        "ca_file": "test/systemtesting/ca.crt",
                        "cert_file": "test/systemtesting/ca.crt",
                        "key_file": "test/systemtesting/ca.key"
                    }
                })
            ebus.connect()

            def handler(message):
                self.assertEqual(message['body']['hello'], 'world')
                ebus.close()
                print("Passed!")
                latch.count_down()

            ebus.register_handler("echo-back", handler)
            ebus.send("echo",
                      reply_address="echo-back",
                      body={'hello': 'world'})
            latch.awaits(10)
        finally:
            starter.stop()
Ejemplo n.º 10
0
    def test_reconnect_before_server_start(self):
        """
        Tests that server starts 5 seconds after client tries to connect to it.
        """
        latch = CountDownLatch()
        ebus = EventBus(options={"auto_connect": "True", "max_reconnect": "5"})
        starter = EventBusBridgeStarter(debug=False)
        # server will start after 5 seconds in background thread
        starter.start_async(delay=5)
        # this will block for 5 seconds to connect until server gets started
        ebus.connect()

        def handler(message):
            self.assertEqual(message['body']['hello'], 'world')
            ebus.close()
            print("Passed!")
            latch.count_down()

        ebus.register_handler("echo-back", handler)
        ebus.send("echo", reply_address="echo-back", body={'hello': 'world'})
        latch.awaits(10)
        starter.stop()
Ejemplo n.º 11
0
    def test_reconnect_server_restart(self):
        """
        Tests that server paused for 5 seconds, and restarts after that.
        All local handlers should be registered again after server restarted.
        """
        latch = CountDownLatch(2)
        ebus = EventBus(options={
            "auto_connect": "True",
            "max_reconnect": "5",
            "debug": "True"
        })
        starter = EventBusBridgeStarter(debug=True)

        def handler(message):
            self.assertEqual(message['body']['hello'], 'world')
            latch.count_down()

        try:
            starter.start()
            starter.wait_started()
            ebus.connect()
            ebus.register_handler("echo-back", handler)
            ebus.send("echo",
                      reply_address="echo-back",
                      body={'hello': 'world'})
            latch.awaits(5, to_count=1)
        finally:
            print("Now stop the server")
            starter.stop()

        print("Now wait for 5 seconds")
        time.sleep(5)
        try:
            print("Now start the server again.")
            starter.start()
            starter.wait_started()
            print(
                "ebus should get connected automatically and the handler gets registered again."
            )
            ebus.send("echo",
                      reply_address="echo-back",
                      body={'hello': 'world'})
            latch.awaits(10)
        finally:
            print("close the client now...")
            ebus.close()
            starter.stop()