def test_port_agent_client_send(self): ipaddr = "67.58.49.194" port = 4000 paClient = PortAgentClient(self.ipaddr, self.port) # paClient = PortAgentClient(ipaddr, port) paClient.init_comms(self.myGotData) paClient.send("this is a test\n")
def test_start_paClient_lost_port_agent_tx(self): """ This test starts the port agent and then starts the port agent client in a special way that will not start the listener thread. This will guarantee that the send context is the one the sees the error. """ self.resetTestVars() self.init_instrument_simulator() self.startPortAgent() paClient = PortAgentClient(self.ipaddr, self.data_port, self.cmd_port) """ Give the port agent time to initialize """ time.sleep(5) paClient.init_comms(self.myGotData, self.myGotRaw, self.myGotError, self.myGotListenerError, start_listener=False) try: self.stop_port_agent() data = "this big ol' test should cause send context to fail" paClient.send(data) time.sleep(1) except InstrumentConnectionException as e: log.error("Exception caught: %r" % (e)) exceptionCaught = True else: exceptionCaught = False time.sleep(5) """ Assert that the error_callback was called. For this test the listener should not be running, so the send context should see the error, and that should throw an exception. Assert that the callback WAS called and that an exception WAS thrown. """ self.assertTrue(self.errorCallbackCalled) self.assertTrue(exceptionCaught)
def test_start_paClient_lost_port_agent_tx(self): """ This test starts the port agent and then starts the port agent client in a special way that will not start the listener thread. This will guarantee that the send context is the one the sees the error. """ self.resetTestVars() self.init_instrument_simulator() self.startPortAgent() paClient = PortAgentClient(self.ipaddr, self.data_port, self.cmd_port) """ Give the port agent time to initialize """ time.sleep(5) paClient.init_comms(self.myGotData, self.myGotRaw, self.myGotError, self.myGotListenerError, start_listener = False) try: self.stop_port_agent() data = "this big ol' test should cause send context to fail" paClient.send(data) time.sleep(1) except InstrumentConnectionException as e: log.error("Exception caught: %r" % (e)) exceptionCaught = True else: exceptionCaught = False time.sleep(5) """ Assert that the error_callback was called. For this test the listener should not be running, so the send context should see the error, and that should throw an exception. Assert that the callback WAS called and that an exception WAS thrown. """ self.assertTrue(self.errorCallbackCalled) self.assertTrue(exceptionCaught)
def test_start_paClient_with_port_agent(self): self.init_instrument_simulator() self.startPortAgent() paClient = PortAgentClient(self.ipaddr, self.data_port, self.cmd_port) paClient.init_comms(self.myGotData, self.myGotRaw, self.myGotError) data = "this is a great big test" paClient.send(data) time.sleep(1) self._instrument_simulator.send(data) time.sleep(5) paClient.stop_comms() self.assertTrue(self.rawCallbackCalled) self.assertTrue(self.dataCallbackCalled)
def test_start_paClient_with_port_agent(self): self.resetTestVars() self.init_instrument_simulator() self.startPortAgent() paClient = PortAgentClient(self.ipaddr, self.data_port, self.cmd_port) try: paClient.init_comms(self.myGotData, self.myGotRaw, self.myGotListenerError, self.myGotError) except InstrumentConnectionException as e: log.error("Exception caught: %r" % (e)) exceptionCaught = True else: exceptionCaught = False data = "this is a great big test" paClient.send(data) time.sleep(1) self._instrument_simulator.send(data) time.sleep(5) paClient.stop_comms() """ Assert that the error_callback was not called, that an exception was not caught, and that the data and raw callbacks were called. """ self.assertFalse(self.errorCallbackCalled) self.assertFalse(exceptionCaught) self.assertTrue(self.rawCallbackCalled) self.assertTrue(self.dataCallbackCalled)
def test_start_pa_client_lost_port_agent_tx_rx(self): """ This test starts the port agent and the instrument_simulator and tests that data is sent and received first; then it stops the port agent and tests that the error_callback was called. """ self.resetTestVars() self.init_instrument_simulator() self.startPortAgent() pa_client = PortAgentClient(self.ipaddr, self.data_port, self.cmd_port) pa_client.init_comms(self.myGotData, self.myGotRaw, self.myGotListenerError, self.myGotError) # Now send some data; there should be no errors. try: data = "this is a great big test" pa_client.send(data) time.sleep(1) self._instrument_simulator.send(data) except InstrumentConnectionException as e: log.error("Exception caught: %r" % e) exception_caught = True else: exception_caught = False time.sleep(1) # Assert that the error_callback was NOT called, that an exception was NOT # caught, and that the data and raw callbacks WERE called. self.assertFalse(self.errorCallbackCalled) self.assertFalse(exception_caught) self.assertTrue(self.rawCallbackCalled) self.assertTrue(self.dataCallbackCalled) # Now reset the test variables and try again; this time after stopping # the port agent. Should be errors self.resetTestVars() try: self.stop_port_agent() log.debug("Port agent stopped") data = "this is another great big test" pa_client.send(data) time.sleep(1) log.debug("Sending from simulator") self._instrument_simulator.send(data) except InstrumentConnectionException as e: log.error("Exception caught: %r" % e) time.sleep(5) # Assert that the error_callback WAS called. The listener usually # is seeing the error first, and that does not call the exception, so # only assert that the error callback was called. self.assertTrue(self.errorCallbackCalled)