def test_on_error(self): ws = Mock() s = signal.SignalManager() error = 'test_error' self.assertRaises(Exception, s.on_error, ws, error) ws.close.assert_called_with()
def teston_msg_func_receive_pid_specified(self, mock_websocket_websocketapp): mock_app = Mock() mock_websocket_websocketapp.return_value = mock_app s = signal.SignalManager() on_msg_func = 'on_msg_func' self.mock_RACK_CTX.pid = None self.assertRaises(Exception, s.receive, on_msg_func)
def test_on_message(self): on_msg_func = Mock() ws = Mock() s = signal.SignalManager() s.on_msg_func = on_msg_func message = 'test_msg' excepted_message = copy.deepcopy(message) s.on_message(ws, message) on_msg_func.assert_called_with(excepted_message) ws.close.assert_called_with()
def test_receive(self, mock_websocket_websocketapp): mock_app = Mock() mock_websocket_websocketapp.return_value = mock_app s = signal.SignalManager() on_msg_func = 'on_msg_func' excepted_on_msg_func = copy.deepcopy(on_msg_func) s.receive(on_msg_func) mock_websocket_websocketapp.\ assert_called_with(url=s.url + '/receive', header=['PID: ' + self.mock_RACK_CTX.pid], on_message=s.on_message, on_error=s.on_error, on_close=s.on_close) mock_app.run_forever.assert_called_with() self.assertEqual(s.on_msg_func, excepted_on_msg_func)
def test_send(self, mock_create_connection): target_id = 'target_id' expected_target_id = copy.deepcopy(target_id) message = 'test_msg' expected_message = copy.deepcopy(message) url = '/test_url/' expected_url = url.rstrip('/') + '/send' ws = Mock() mock_create_connection.return_value = ws s = signal.SignalManager(url=url) s.send(target_id, message) mock_create_connection.\ assert_called_with(expected_url, header=['PID: ' + expected_target_id]) ws.send.assert_called_with(expected_message) ws.close.assert_called_with()
def test_receive_pid_specified(self, mock_websocket_websocketapp): mock_app = Mock() mock_websocket_websocketapp.return_value = mock_app url = '/test_url/' expected_url = url.rstrip('/') s = signal.SignalManager(url=url) on_msg_func = 'on_msg_func' excepted_on_msg_func = copy.deepcopy(on_msg_func) pid = 'singnal_pid' s.receive(on_msg_func, pid=pid) self.assertEqual(s.url, expected_url) mock_websocket_websocketapp.assert_called_with(url=s.url + '/receive', header=['PID: ' + pid], on_message=s.on_message, on_error=s.on_error, on_close=s.on_close) mock_app.run_forever.assert_called_with() self.assertEqual(s.on_msg_func, excepted_on_msg_func)