def test_initialize_finalize(self): self.setup_task.zmq_server.start() wait_condition(lambda: (self.setup_task.zmq_server.state == ZMQServer. STATE_WAITING)) self.setup_task.zmq_server.stop() self.assertEqual(self.setup_task.zmq_server.state, ZMQServer.STATE_STOPPED)
def mock_started_server(self, events_received, errors_received): with self.mock_server(events_received, errors_received) as server: server.start() wait_condition(lambda: server.state == ZMQServer.STATE_WAITING) yield server server._inproc_socket.data = "".encode("utf-8") wait_condition(lambda: server.state == ZMQServer.STATE_STOPPED)
def test_stop_a_stopped_server(self): def cb(event): pass def err_cb(error_type, error_msg): pass server = ZMQServer(cb, err_cb) server.start() wait_condition(lambda: server.state == ZMQServer.STATE_WAITING) server.stop() wait_condition(lambda: server.state == ZMQServer.STATE_STOPPED) server.stop() self.assertEqual(server.state, ZMQServer.STATE_STOPPED)
def test_error_conditions_waiting_pub(self): events = [] errors = [] with LogCapture(level=logging.ERROR) as capture: with self.mock_started_server(events, errors) as server: server._sub_socket.data = [ x.encode("utf-8") for x in ["HELLO", "xxx", "1"] ] wait_condition(lambda: len(capture.records) == 1) capture.check(( "force_wfmanager.server.zmq_server", "ERROR", "State WAITING cannot handle pub data. Discarding.", ))
def test_start_and_stop(self): received = [] def cb(event): received.append(event) def err_cb(error_type, error_msg): pass server = ZMQServer(cb, err_cb) self.assertEqual(server.state, ZMQServer.STATE_STOPPED) server.start() wait_condition(lambda: server.state == ZMQServer.STATE_WAITING) server.stop() wait_condition(lambda: server.state == ZMQServer.STATE_STOPPED)
def test_server_error_recv_failure(self): events = [] errors = [] with LogCapture(level=logging.ERROR): with self.mock_server(events, errors) as server: with mock.patch.object(MockSocket, "recv_multipart") as recv: recv.side_effect = Exception("Boom") server.start() wait_condition( lambda: server.state == ZMQServer.STATE_WAITING) server._sync_socket.data = [ x.encode("utf-8") for x in ["HELLO", "xxx", "1"] ] wait_condition(lambda: len(errors) != 0) self.assertEqual(errors[0][0], ZMQServer.ERROR_TYPE_WARNING) self.assertIn("Unable to retrieve data", errors[0][1]) self.assertEqual(server.state, ZMQServer.STATE_WAITING)
def test_server_error_handler_failure(self): events = [] errors = [] with LogCapture(level=logging.ERROR): with self.mock_server(events, errors) as server: with mock.patch.object(MockSocket, "send_multipart") as send: send.side_effect = Exception("Boom") server.start() wait_condition( lambda: server.state == ZMQServer.STATE_WAITING) server._sync_socket.data = [ x.encode("utf-8") for x in ["HELLO", "xxx", "1"] ] wait_condition( lambda: server.state != ZMQServer.STATE_WAITING) self.assertEqual(errors[0][0], ZMQServer.ERROR_TYPE_CRITICAL) self.assertIn("Handler", errors[0][1]) self.assertEqual(server.state, ZMQServer.STATE_STOPPED)
def test_error_conditions_receiving_sync(self): events = [] errors = [] with LogCapture(level=logging.ERROR) as capture: with self.mock_started_server(events, errors) as server: server._sync_socket.data = [ x.encode("utf-8") for x in ["HELLO", "xxx", "1"] ] wait_condition( lambda: server.state == ZMQServer.STATE_RECEIVING) server._sync_socket.data = [ x.encode("utf-8") for x in ["HELLO", "xxx", "1"] ] wait_condition(lambda: len(capture.records) == 1) server._sync_socket.data = [ x.encode("utf-8") for x in ["HELLO", "xxx"] ] wait_condition(lambda: len(capture.records) == 2) capture.check( ( "force_wfmanager.server.zmq_server", "ERROR", "Unknown request received ['HELLO', 'xxx', '1']", ), ( "force_wfmanager.server.zmq_server", "ERROR", "Unknown msg request received HELLO", ), )
def test_error_conditions_waiting_sync(self): events = [] errors = [] with LogCapture(level=logging.ERROR) as capture: with self.mock_started_server(events, errors) as server: server._sync_socket.data = ["HELLO".encode("utf-8")] wait_condition(lambda: len(capture.records) == 1) server._sync_socket.data = [ x.encode("utf-8") for x in ["WHATEVER", "xxx", "3"] ] wait_condition(lambda: len(capture.records) == 2) server._sync_socket.data = [ x.encode("utf-8") for x in ["HELLO", "xxx", "3"] ] wait_condition(lambda: len(capture.records) == 3) capture.check( ( "force_wfmanager.server.zmq_server", "ERROR", "Unknown request received ['HELLO']", ), ( "force_wfmanager.server.zmq_server", "ERROR", "Unknown msg request received WHATEVER", ), ( "force_wfmanager.server.zmq_server", "ERROR", "Unknown protocol received 3", ), )
def test_server_error_unable_to_poll(self): events = [] errors = [] with LogCapture(level=logging.ERROR): with self.mock_server(events, errors) as server: with mock.patch.object(MockPoller, "poll") as poll: poll.side_effect = Exception("Boom") server.start() wait_condition(lambda: len(errors) != 0) self.assertEqual( errors, [( ZMQServer.ERROR_TYPE_CRITICAL, "Unable to poll sockets: " "Boom.\n" "The server is now stopped. You will be " "unable to receive progress information " "from the BDSS.", )], ) self.assertEqual(server.state, ZMQServer.STATE_STOPPED)
def test_receive_info(self): events = [] errors = [] with self.mock_started_server(events, errors) as server: server._sync_socket.data = [ x.encode("utf-8") for x in ["HELLO", "xxx", "1"] ] wait_condition(lambda: server.state == ZMQServer.STATE_RECEIVING) server._sync_socket.data = [ x.encode("utf-8") for x in ["GOODBYE", "xxx"] ] wait_condition(lambda: server.state == ZMQServer.STATE_WAITING) server._sync_socket.data = [ x.encode("utf-8") for x in ["HELLO", "xxx", "1"] ] wait_condition(lambda: server.state == ZMQServer.STATE_RECEIVING) event_data = { "id": "force_bdss.events.mco_events.MCOStartEvent", "model_data": {}, } server._sub_socket.data = [ x.encode("utf-8") for x in ["MESSAGE", "xxx", json.dumps(event_data)] ] wait_condition(lambda: len(events) == 1) self.assertIsInstance(events[0], MCOStartEvent) server._sync_socket.data = [ x.encode("utf-8") for x in ["GOODBYE", "xxx"] ] wait_condition(lambda: server.state == ZMQServer.STATE_WAITING)
def test_error_conditions_receiving_pub(self): events = [] errors = [] with LogCapture(level=logging.ERROR) as capture: with self.mock_started_server(events, errors) as server: server._sync_socket.data = [ x.encode("utf-8") for x in ["HELLO", "xxx", "1"] ] wait_condition( lambda: server.state == ZMQServer.STATE_RECEIVING) server._sub_socket.data = [ x.encode("utf-8") for x in ["MESSAGE", "xxx"] ] wait_condition(lambda: len(capture.records) == 1) server._sub_socket.data = [ x.encode("utf-8") for x in ["WHATEVER", "xxx", ""] ] wait_condition(lambda: len(capture.records) == 2) server._sub_socket.data = [ x.encode("utf-8") for x in ["MESSAGE", "xxx", "bonkers"] ] wait_condition(lambda: len(capture.records) == 3) capture.check( ( "force_wfmanager.server.zmq_server", "ERROR", "Unknown request received ['MESSAGE', 'xxx']", ), ( "force_wfmanager.server.zmq_server", "ERROR", "Unknown msg request received WHATEVER", ), ( "force_wfmanager.server.zmq_server", "ERROR", "Received invalid data. Discarding", ), )