Exemplo n.º 1
0
 def test_send(self):
     """ test sending a message"""
     eb = Eventbus(port=7001,debug=self.debug)
     handler=Handler(self.debug)
     address="echoMe"
     eb.registerHandler(address, handler.handle)
     cmd=EchoCommand("time","send",address)
     eb.wait(State.OPEN)
     eb.send('echo',cmd)
     # wait for the message to arrive
     time.sleep(RECEIVE_WAIT)
     eb.close()
     assert 'received_nanotime' in handler.result
     assert 'iso_time' in handler.result
Exemplo n.º 2
0
 def test_publish(self):
     """ test publishing a message to the echo server """
     eb = Eventbus(port=7001,debug=True)
     handler=Handler(self.debug)
     eb.registerHandler("echo", handler.handle)
     #jsonObject -body
     body1 = {'msg': 'testpublish 1'}
     eb.wait(State.OPEN)
     # publish without headers
     eb.publish('echo', body1)
     # wait for the message to arrive
     time.sleep(RECEIVE_WAIT)
     eb.close()
     assert handler.result == body1
Exemplo n.º 3
0
 def test_reply(self):
     """ test sending a message with a reply handler """
     eb = Eventbus(port=7001,debug=self.debug)
     handler=Handler(self.debug)
     body = {'msg': 'test reply' }
     eb.wait(State.OPEN)
     eb.send('echo',body,callback=handler.handle)
     # wait for the message to arrive
     time.sleep(RECEIVE_WAIT)
     eb.close()
     assert handler.result==body
Exemplo n.º 4
0
 def testSocketOfEventBus(self):
     """ send a message using the private function of the event bus"""
     eb = Eventbus(port=7001,debug=True,connect=False)
     eb._connect()
     eb.state=State.OPEN
     body = {'msg': 'test socketOfEventBus' }
     eb._send('publish','echo', body)
     eb.sock.close()
Exemplo n.º 5
0
    def test_registerHandler(self):
        """ test registering a handler"""
        eb = Eventbus(port=7001,debug=self.debug)
        handler=Handler(self.debug);
        eb.registerHandler('echo', handler.handle)
        assert(eb.handlers['echo'] != None)

        eb.unregisterHandler('echo',handler.handle)
        assert eb.handlers is not None
        assert len(eb.handlers) == 0
        # close
        eb.close()
Exemplo n.º 6
0
 def test_sendInvalidAddress(self):
     """ test trying to send to an invalid address"""
     eb = Eventbus(port=7001,debug=self.debug)
     handler=Handler(self.debug)
     errorHandler=Handler(self.debug)
     address="unpermitted_address"
     eb.registerHandler(address, handler.handle)
     eb.onError=errorHandler.handle        
     cmd=EchoCommand("time","send",address)
     eb.send('echo',cmd)
     time.sleep(RECEIVE_WAIT)
     #  40 message bytes with payload {'type': 'err', 'message': 'access_denied'} not handled yet ... expected
     eb.close()
     assert errorHandler.err is not None
     assert errorHandler.result is None
     assert handler.err is None
     assert handler.result is None
Exemplo n.º 7
0
 def test_ping(self):
     """ test sending a ping"""
     eb = Eventbus(port=7001,options={"vertxbus_ping_interval":500},debug=self.debug)
     eb.wait(State.OPEN)
     time.sleep(1.2)
     eb.close()
     assert eb.pongCount==2
Exemplo n.º 8
0
 def testCreateWithInvalidPort(self):
     """
     test creating an event bus for an invalid port
     """
     hasErr=False
     try:
         eb=Eventbus(port=9999,debug=self.debug)
         assert eb is not None
     except Exception as e:
         if self.debug:
             print(e)
         hasErr=e is not None
         pass
     assert hasErr,"There should be an exception that eventbus is not expected at this port"
Exemplo n.º 9
0
 def testMergeHeaders(self):
     """ test merging headers """
     eb = Eventbus(port=7001,debug=self.debug,connect=False)
     eb.addHeader("dh1", "dv1")
     mh1=eb._mergeHeaders();
     assert mh1=={'dh1': 'dv1'};
     headers={"h1","v1"}
     mh2=eb._mergeHeaders(headers)
     assert mh2=={'dh1': 'dv1', 'h': '1', 'v': '1'}
Exemplo n.º 10
0
 def testRegisterHandler(self):
     """
     test a successful handler registration
     """
     eb=Eventbus(port=7001)
     handler=Handler(self.debug)
     eb.registerHandler("echo", handler.handle)
     assert(eb.handlers['echo'] != None)
     eb.close()
Exemplo n.º 11
0
 def testRegisterWithClosedBus(self):
     """
     try registering an event bus for a closed port
     """
     eb=Eventbus(port=7001,debug=self.debug)
     eb.close();
     handler=Handler(self.debug);
     err=False
     try:
         eb.registerHandler("address1", handler.handle)
     except Exception as e:
         if self.debug:
             print (e)
         err=e is not None
         pass
     assert err, "It should not be possible to register a handler while the eventbus is closed"
Exemplo n.º 12
0
 def test_publishWithMultipleHandlers(self):
     """ test publishing a message to be handle by multiple handlers"""
     eb = Eventbus(port=7001,debug=self.debug)
     handler1=Handler(self.debug)
     handler2=Handler(self.debug)
     address="echoMe"
     eb.registerHandler(address, handler1.handle)
     eb.registerHandler(address, handler2.handle)
     eb.wait(State.OPEN)
     eb.send('echo',EchoCommand("reset","send",address))
     cmd=EchoCommand("counter","publish",address)
     eb.send('echo',cmd)
     time.sleep(RECEIVE_WAIT)
     eb.close()
     assert 'counter' in handler1.result
     assert handler1.result['counter']==1
     assert 'counter' in handler2.result
     assert handler1.result['counter']==1
Exemplo n.º 13
0
 def test_publishWithHeader(self):
     """ test publishing a message with headers """
     eb = Eventbus(port=7001,debug=self.debug)
     handler=Handler(self.debug)
     eb.registerHandler("echo", handler.handle)
     body2 = {'msg': 'testpublish with headers' }
     eb.addHeader('type', 'text')
     eb.addHeader('size', 'small')
     # publish with headers
     eb.publish('echo', body2)
     # wait for the message to arrive
     time.sleep(RECEIVE_WAIT)
     eb.close()
     assert handler.result == body2
     assert handler.headers == {'type': 'text', 'size': 'small'}
Exemplo n.º 14
0
 def testWait(self):
     """
     test waiting for the eventbus to open and close
     """
     eb=Eventbus(port=7001,debug=True)
     eb.wait()
     eb.close()
     eb.wait(State.CLOSED)
     eb=Eventbus(port=7001,debug=True,connect=False)
     timedOut=False
     try:
         eb.wait(State.OPEN,timeOut=0.1)
     except Exception as e:
         print(e)
         timedOut=e is not None
     assert timedOut