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()
Ejemplo n.º 2
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()