def test_getCommand(self): """Test wether getting commands from the network works. Sends command through TCP/IP and check wether the same command is received on the other end.""" self.assertTrue(self.communicator.wait_listen(2), "Communicator not listening") CommandSocket = ThreadedSocket('localhost', self.testport, giveup=0, retry_timeout=0.1) self.assertTrue(CommandSocket.wait_connect(4), "Could not connect to communicator") CommandSocket.send(self.TestCommand) time.sleep(0.1) # Wait a bit for the socket to be available received, conn_id = self.communicator.get_command() self.assertEqual( received, self.TestCommand ) #,"Oh no, we received something else than we sent!")
def test_send_command(self): # Set up test communicator comm_s = None mod_s = None client = None try: #comm_s = ThreadedSocket("", self.communicatorPort) comm_s = ThreadedSocket("", "communicator") self.assertTrue(comm_s.wait_listen(), "Fake Communicator socket did not open") self.config.add_option("vision_controller", "modules", "localhost = mod1") self.config.add_option("vision_controller", "modules_settings", "mod1 = test") self.controller = visioncontroller.VisionController() self.controller.set_config(self.config) # On the fake communicator, receive the module to start and the port # to start it on, because we need that port to connect to. client = comm_s.wait_connect() self.assertTrue(client != None, "Did not get a connection") recv = client.wait_data() self.assertTrue(recv, "Did not receive any data") port = None for i in recv: if "port" in i: port = i['port'] self.assertTrue(port != None, "Did not receive a port number to connect to") # Set up a connection to the port specified by the vision # controller, it should be listening on that by now. mod_s = ThreadedSocket("localhost", port) mod_s.start() self.assertTrue(mod_s.wait_connect(), "Fake vision module did not connect") # First receive the send_capabilities command that should be send # automatically. recv = mod_s.wait_data() self.assertEquals(len(recv), 1, "Should have received one command") data = recv[0] self.assertTrue( "command" in data, "There should be a command in the received dictionary") self.assertEquals(data["command"], "send_capabilities", "Testcommand should be send_capabilities") self.assertTrue( "params" in data, "There should be command parameters in the received dictionary" ) self.assertEquals(data["params"], {}, "Test parameters should be an empty dictionary") # Try sending a test command and receive it testCommand = "test_command" testParams = {"param1": "test_param"} self.controller.send_command("localhost", "mod1", testCommand, testParams) # See if the test command has been received recv = mod_s.wait_data() self.assertEquals(len(recv), 1, "Should have received one command") data = recv[0] self.assertTrue( "command" in data, "There should be a command in the received dictionary") self.assertEquals(data["command"], testCommand, "Testcommand should be %s" % testCommand) self.assertTrue( "params" in data, "There should be command parameters in the received dictionary" ) self.assertEquals( data["params"], testParams, "Test parameters should be %s" % repr(testParams)) finally: if comm_s: comm_s.close() if mod_s: mod_s.close() if client: client.close() self.controller.stop_connections()
from util.threadedsocket import ThreadedSocket import util.nullhandler import util.loggingextra import sys import logging import time if __name__ == '__main__': host = "localhost" port = 49152 if len(sys.argv) > 2: host = sys.argv[1] port = int(sys.argv[2]) sock = ThreadedSocket(host, port, giveup=2) sock.start() try: start = time.time() while not sock.connected and time.time() - start < 1: time.sleep(0.01) if sock.connected: sock.send("diag") start = time.time() data = [] while time.time() - start < 2 and data == []: time.sleep(0.01) data = sock.get_data() for item in data: if 'status' in item: if item['status'] == 'Communicator is running ok': print "Communicator at %s is running on port %d" % (