def test_02_req_rep_sockets_over_inproc(self): # Basic send/receive over REQ/REP sockets print "Test if sending works over REQ/REP socket using inproc, includes a username/password" sender = ZmqSender(zmq_req_endpoints=["inproc://test"], username="******", password="******") receiver_thread = ZmqReceiverThread(zmq_rep_bind_address="inproc://test", username="******", password="******") receiver_thread.start() sender.send("test", time_out_waiting_for_response_in_sec=3) self.assertEquals(receiver_thread.last_received_message(), 'test') print "Test if sending wrong password over REP/REQ connection results in error (actually timeout)" sender = ZmqSender(zmq_req_endpoints=["inproc://test"], username="******", password="******") try: sender.send("test", time_out_waiting_for_response_in_sec=3) print "Error. Did get answer from remote system which was not expected" except: # Could not send message, which is ok in this case print "Success." receiver_thread.stop() receiver_thread.join() sender.destroy() # Cleaning up sockets takes some time time.sleep(1)
def test_14_proxy(self): # With proxy elements print "Add a proxy setup to the end to end chain pub->proxy.req->proxy.rep->pub->sub" sender = ZmqSender(zmq_pub_endpoint="tcp://*:57000") proxy_sub_req_thread = ZmqProxySub2ReqThread(zmq_sub_connect_addresses=['tcp://localhost:57000'], zmq_req_connect_addresses=["tcp://localhost:57001"], username_req="username", password_req="password") proxy_sub_req_thread.start() proxy_rep_pub_thread = ZmqProxyRep2PubThread(zmq_rep_bind_address='tcp://*:57001', zmq_pub_bind_address='tcp://*:57002', username_rep="username", password_rep="password") proxy_rep_pub_thread.start() receiver_thread = ZmqReceiverThread(zmq_sub_connect_addresses=["tcp://localhost:57002"]) receiver_thread.start() # Take 0.5 second for sockets to connect to prevent 'slow joiner' problem time.sleep(0.5) sender.send("test") # Sleep for pub/sub not guaranteed to be done on completing send_pub_socket time.sleep(1) print "last received message by proxy_sub_req_thread: {0}".format(proxy_sub_req_thread.last_received_message()) print "last received message by proxy_rep_pub_thread: {0}".format(proxy_rep_pub_thread.last_received_message()) print "last received message by receiver_thread: {0}".format(receiver_thread.last_received_message()) self.assertEqual(receiver_thread.last_received_message(), 'test') receiver_thread.stop() receiver_thread.join() proxy_sub_req_thread.stop() proxy_sub_req_thread.join() proxy_rep_pub_thread.stop() proxy_rep_pub_thread.join() sender.destroy() # Cleaning up sockets takes some time time.sleep(1)
def test_10_pub_sub_timeout_per_socket(self): # Basic send/receive over PUB/SUB sockets print("Test a timeout per socket") sender = ZmqSender(zmq_pub_endpoint="tcp://*:47001") receiver_thread = ZmqReceiverThread( zmq_sub_connect_addresses=[("tcp://localhost:47001", 3)], recreate_sockets_on_timeout_of_sec=10) receiver_thread.start() # Slow joiner time.sleep(0.1) first_socket = receiver_thread.receiver.sub_sockets[0].zmq_socket sender.send("test") # Take 2 seconds to see if it works in case of within the 3 seconds window. time.sleep(2) self.assertEqual(receiver_thread.last_received_message(), 'test') # Now send another but with 2 seconds delay, which should be ok, followed by 4 heartbeats. # Socket should not be refreshed. sender.send("test2") time.sleep(2) sender.send_heartbeat() time.sleep(2) sender.send_heartbeat() time.sleep(2) sender.send_heartbeat() time.sleep(2) sender.send_heartbeat() self.assertEqual(receiver_thread.last_received_message(), 'test2') self.assertEqual(receiver_thread.receiver.sub_sockets[0].zmq_socket, first_socket) # Now send another but with 4 seconds delay, which should restart the sockets, but message should arrive sender.send("test3") time.sleep(4) self.assertEqual(receiver_thread.last_received_message(), 'test3') second_socket = receiver_thread.receiver.sub_sockets[0].zmq_socket self.assertNotEqual(second_socket, first_socket) # Now send another but with 2 seconds delay, which should be ok sender.send("test4") time.sleep(2) self.assertEqual(receiver_thread.last_received_message(), 'test4') self.assertEqual(receiver_thread.receiver.sub_sockets[0].zmq_socket, second_socket) receiver_thread.stop() receiver_thread.join() sender.destroy() # Cleaning up sockets takes some time time.sleep(1)
def test_10_pub_sub_timeout_per_socket(self): # Basic send/receive over PUB/SUB sockets print "Test a timeout per socket" sender = ZmqSender(zmq_pub_endpoint="tcp://*:47001") receiver_thread = ZmqReceiverThread(zmq_sub_connect_addresses=[("tcp://localhost:47001", 3)], recreate_sockets_on_timeout_of_sec=10) receiver_thread.start() # Slow joiner time.sleep(0.1) first_socket = receiver_thread.receiver.sub_sockets[0].zmq_socket sender.send("test") # Take 2 seconds to see if it works in case of within the 3 seconds window. time.sleep(2) self.assertEqual(receiver_thread.last_received_message(), 'test') # Now send another but with 2 seconds delay, which should be ok, followed by 4 heartbeats. # Socket should not be refreshed. sender.send("test2") time.sleep(2) sender.send_heartbeat() time.sleep(2) sender.send_heartbeat() time.sleep(2) sender.send_heartbeat() time.sleep(2) sender.send_heartbeat() self.assertEqual(receiver_thread.last_received_message(), 'test2') self.assertEqual(receiver_thread.receiver.sub_sockets[0].zmq_socket, first_socket) # Now send another but with 4 seconds delay, which should restart the sockets, but message should arrive sender.send("test3") time.sleep(4) self.assertEqual(receiver_thread.last_received_message(), 'test3') second_socket = receiver_thread.receiver.sub_sockets[0].zmq_socket self.assertNotEqual(second_socket, first_socket) # Now send another but with 2 seconds delay, which should be ok sender.send("test4") time.sleep(2) self.assertEqual(receiver_thread.last_received_message(), 'test4') self.assertEqual(receiver_thread.receiver.sub_sockets[0].zmq_socket, second_socket) receiver_thread.stop() receiver_thread.join() sender.destroy() # Cleaning up sockets takes some time time.sleep(1)
def test_14_proxy(self): # With proxy elements print( "Add a proxy setup to the end to end chain pub->proxy.req->proxy.rep->pub->sub" ) sender = ZmqSender(zmq_pub_endpoint="tcp://*:57000") proxy_sub_req_thread = ZmqProxySub2ReqThread( zmq_sub_connect_addresses=['tcp://localhost:57000'], zmq_req_connect_addresses=["tcp://localhost:57001"], username_req="username", password_req="password") proxy_sub_req_thread.start() proxy_rep_pub_thread = ZmqProxyRep2PubThread( zmq_rep_bind_address='tcp://*:57001', zmq_pub_bind_address='tcp://*:57002', username_rep="username", password_rep="password") proxy_rep_pub_thread.start() receiver_thread = ZmqReceiverThread( zmq_sub_connect_addresses=["tcp://localhost:57002"]) receiver_thread.start() # Take 0.5 second for sockets to connect to prevent 'slow joiner' problem time.sleep(0.5) sender.send("test") # Sleep for pub/sub not guaranteed to be done on completing send_pub_socket time.sleep(1) print("last received message by proxy_sub_req_thread: {0}".format( proxy_sub_req_thread.last_received_message())) print("last received message by proxy_rep_pub_thread: {0}".format( proxy_rep_pub_thread.last_received_message())) print("last received message by receiver_thread: {0}".format( receiver_thread.last_received_message())) self.assertEqual(receiver_thread.last_received_message(), 'test') receiver_thread.stop() receiver_thread.join() proxy_sub_req_thread.stop() proxy_sub_req_thread.join() proxy_rep_pub_thread.stop() proxy_rep_pub_thread.join() sender.destroy() # Cleaning up sockets takes some time time.sleep(1)
def test_03_pub_sub_without_passwords(self): # Basic send/receive over PUB/SUB sockets print "Test if sending works over PUB/SUB sockets without passwords" sender = ZmqSender(zmq_pub_endpoint="tcp://*:47001") receiver_thread = ZmqReceiverThread(zmq_sub_connect_addresses=["tcp://localhost:47001"]) receiver_thread.start() # Take 0.5 second for sockets to connect to prevent 'slow joiner' problem time.sleep(0.5) sender.send("test") # Sleep for pub/sub not guaranteed to be done on completing send_pub_socket time.sleep(0.1) self.assertEqual(receiver_thread.last_received_message(), 'test') receiver_thread.stop() receiver_thread.join() sender.destroy() # Cleaning up sockets takes some time time.sleep(1)
def test_04_pub_sub_without_passwords_over_inproc(self): # Basic send/receive over PUB/SUB sockets print("Test if sending works over PUB/SUB sockets without passwords") sender = ZmqSender(zmq_pub_endpoint="inproc://my_test") receiver_thread = ZmqReceiverThread( zmq_sub_connect_addresses=["inproc://my_test"]) receiver_thread.start() # Take 0.5 second for sockets to connect to prevent 'slow joiner' problem time.sleep(0.5) sender.send("test") # Sleep for pub/sub not guaranteed to be done on completing send_pub_socket time.sleep(0.1) self.assertEqual(receiver_thread.last_received_message(), 'test') receiver_thread.stop() receiver_thread.join() sender.destroy() # Cleaning up sockets takes some time time.sleep(1)
def test_02_req_rep_sockets_over_inproc(self): # Basic send/receive over REQ/REP sockets print( "Test if sending works over REQ/REP socket using inproc, includes a username/password" ) sender = ZmqSender(zmq_req_endpoints=["inproc://test"], username="******", password="******") receiver_thread = ZmqReceiverThread( zmq_rep_bind_address="inproc://test", username="******", password="******") receiver_thread.start() sender.send("test", time_out_waiting_for_response_in_sec=3) self.assertEquals(receiver_thread.last_received_message(), 'test') print( "Test if sending wrong password over REP/REQ connection results in error (actually timeout)" ) sender = ZmqSender(zmq_req_endpoints=["inproc://test"], username="******", password="******") try: sender.send("test", time_out_waiting_for_response_in_sec=3) print( "Error. Did get answer from remote system which was not expected" ) except: # Could not send message, which is ok in this case print("Success.") receiver_thread.stop() receiver_thread.join() sender.destroy() # Cleaning up sockets takes some time time.sleep(1)