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" % (
import util.nullhandler import util.loggingextra import sys import logging import time if __name__ == "__main__": host = "localhost" port = 12345 if len(sys.argv) > 2: host = sys.argv[1] port = int(sys.argv[2]) # logging.getLogger('Borg.Brain').addHandler(util.loggingextra.ScreenOutput()) # logging.getLogger('Borg.Brain').setLevel(logging.INFO) sock = ThreadedSocket(host, port, giveup=0, use_pickle=False, server=False) sock.start() try: start = time.time() while not sock.connected and time.time() - start < 2: time.sleep(0.01) if sock.connected: sock.send("quit\r\n") while sock.connected and time.time() - start < 2: time.sleep(0.01) if not sock.connected: print "Pioneercontroller at %s is running on port %d" % (host, port) sys.exit(0) else: print "Service running at %s on port %d but not behaving properly as pioneercontroller" % (host, port) print repr(sock.get_data()) sys.exit(1)
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()