Skip to content
This repository has been archived by the owner on Sep 20, 2022. It is now read-only.

Commit

Permalink
Merge pull request #39 from gridsmartercities/log_received_responses
Browse files Browse the repository at this point in the history
Added new (optional) error logging for received responses
  • Loading branch information
jamsidedown authored Mar 26, 2020
2 parents c876606 + db28643 commit 969ec1a
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 0 deletions.
17 changes: 17 additions & 0 deletions pywsitest/ws_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ def __init__(self, uri: str):
self.response_timeout = 10.0
self.message_timeout = 10.0
self.test_timeout = 60.0
self.log_responses_on_error = False

def with_parameter(self, key: str, value: object) -> "WSTest":
"""
Expand Down Expand Up @@ -160,6 +161,16 @@ def with_test_timeout(self, timeout: float) -> "WSTest":
self.test_timeout = timeout
return self

def with_received_response_logging(self) -> "WSTest":
"""
Enables received response logging when an exception is thrown
Returns:
(WSTest): The WSTest instance set_log_responses_on_error was called on
"""
self.log_responses_on_error = True
return self

async def run(self):
"""
Runs the integration tests
Expand Down Expand Up @@ -238,6 +249,12 @@ def _get_receive_error_message(self) -> str:
error_message = "Timed out waiting for responses:"
for response in self.expected_responses:
error_message += "\n" + str(response)

if self.log_responses_on_error:
error_message += "\nReceived responses:"
for json_response in self.received_json:
error_message += "\n" + str(json_response)

return error_message

def is_complete(self) -> bool:
Expand Down
76 changes: 76 additions & 0 deletions tests/test_ws_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,82 @@ async def test_websocket_response_timeout(self, mock_ssl, mock_websockets):
await ws_tester.run()
mock_socket.close.assert_called_once()

@patch("websockets.connect")
@patch("ssl.SSLContext")
@syncify
async def test_websocket_response_timeout_with_received_response_logging_enabled(self, mock_ssl, mock_websockets):
ws_tester = (
WSTest("wss://example.com")
.with_response_timeout(0.1)
.with_received_response_logging()
.with_response(
WSResponse()
.with_attribute("message", "hello")
)
)

mock_socket = MagicMock()
mock_socket.close = MagicMock(return_value=asyncio.Future())
mock_socket.close.return_value.set_result(MagicMock())

first_future = asyncio.Future()
first_future.set_result(json.dumps({"message": "bye"}))

mock_socket.recv = MagicMock(side_effect=[first_future, asyncio.Future()])

mock_websockets.return_value = asyncio.Future()
mock_websockets.return_value.set_result(mock_socket)

ssl_context = MagicMock()
mock_ssl.return_value = ssl_context

with self.assertRaises(WSTimeoutError) as ex:
await ws_tester.run()

expected_error = (
"Timed out waiting for responses:\n{\"message\": \"hello\"}\n" +
"Received responses:\n{\"message\": \"bye\"}"
)
self.assertEqual(expected_error, str(ex.exception))

mock_socket.close.assert_called_once()

@patch("websockets.connect")
@patch("ssl.SSLContext")
@syncify
async def test_websocket_response_timeout_with_received_response_logging_disabled(self, mock_ssl, mock_websockets):
ws_tester = (
WSTest("wss://example.com")
.with_response_timeout(0.1)
.with_response(
WSResponse()
.with_attribute("message", "hello")
)
)

mock_socket = MagicMock()
mock_socket.close = MagicMock(return_value=asyncio.Future())
mock_socket.close.return_value.set_result(MagicMock())

first_future = asyncio.Future()
first_future.set_result(json.dumps({"message": "bye"}))

mock_socket.recv = MagicMock(side_effect=[first_future, asyncio.Future()])

mock_websockets.return_value = asyncio.Future()
mock_websockets.return_value.set_result(mock_socket)

ssl_context = MagicMock()
mock_ssl.return_value = ssl_context

with self.assertRaises(WSTimeoutError) as ex:
await ws_tester.run()

expected_error = "Timed out waiting for responses:\n{\"message\": \"hello\"}"
self.assertEqual(expected_error, str(ex.exception))

mock_socket.close.assert_called_once()

@patch("websockets.connect")
@patch("ssl.SSLContext")
@syncify
Expand Down

0 comments on commit 969ec1a

Please sign in to comment.