diff options
author | Syaoran Kuo <syaoranx@google.com> | 2020-10-30 14:43:30 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-10-30 14:43:30 +0800 |
commit | d87c88e2fd8847fb4f30c28707dd1583cda6fef4 (patch) | |
tree | f08fdefe0c8c470e70096ac85ecddbb718c235db /tests/mobly/controllers | |
parent | d8ac439736cebcce8852b66497aef8a35c8e162e (diff) | |
download | mobly-d87c88e2fd8847fb4f30c28707dd1583cda6fef4.tar.gz |
Sets default not to do verbose logging (#683)
Add support to not log full RPC responses.
Some RPC responses can be large, logging every single one of them can make the log file too big to be useful for debugging.
Diffstat (limited to 'tests/mobly/controllers')
-rwxr-xr-x | tests/mobly/controllers/android_device_lib/jsonrpc_client_base_test.py | 78 |
1 files changed, 78 insertions, 0 deletions
diff --git a/tests/mobly/controllers/android_device_lib/jsonrpc_client_base_test.py b/tests/mobly/controllers/android_device_lib/jsonrpc_client_base_test.py index 7be4190..063ab4e 100755 --- a/tests/mobly/controllers/android_device_lib/jsonrpc_client_base_test.py +++ b/tests/mobly/controllers/android_device_lib/jsonrpc_client_base_test.py @@ -228,6 +228,84 @@ class JsonRpcClientBaseTest(jsonrpc_client_test_base.JsonRpcClientTestBase): self.assertEqual(next(client._counter), 10) + @mock.patch('socket.create_connection') + def test_rpc_verbose_logging_with_long_string(self, + mock_create_connection): + """Test rpc response fully write into DEBUG level log.""" + fake_file = self.setup_mock_socket_file(mock_create_connection) + testing_rpc_response = self.generate_rpc_response(4000) + fake_file.resp = testing_rpc_response + + client = FakeRpcClient() + client.connect() + + response = client._client_receive() + self.assertEqual(response, testing_rpc_response) + + client.log.debug.assert_called_with('Snippet received: %s', + testing_rpc_response) + + @mock.patch('socket.create_connection') + def test_rpc_truncated_logging_short_response(self, + mock_create_connection): + """Test rpc response will full logged when length is short.""" + fake_file = self.setup_mock_socket_file(mock_create_connection) + testing_rpc_response = self.generate_rpc_response( + int(jsonrpc_client_base._MAX_RPC_RESP_LOGGING_LENGTH / 2)) + fake_file.resp = testing_rpc_response + + client = FakeRpcClient() + client.connect() + + client.set_snippet_client_verbose_logging(False) + response = client._client_receive() + + self.assertEqual(response, testing_rpc_response) + client.log.debug.assert_called_with('Snippet received: %s', + testing_rpc_response) + + @mock.patch('socket.create_connection') + def test_rpc_truncated_logging_fit_size_response(self, + mock_create_connection): + """Test rpc response will full logged when length is equal to threshold. + """ + fake_file = self.setup_mock_socket_file(mock_create_connection) + testing_rpc_response = self.generate_rpc_response( + jsonrpc_client_base._MAX_RPC_RESP_LOGGING_LENGTH) + fake_file.resp = testing_rpc_response + + client = FakeRpcClient() + client.connect() + + client.set_snippet_client_verbose_logging(False) + response = client._client_receive() + + self.assertEqual(response, testing_rpc_response) + client.log.debug.assert_called_with('Snippet received: %s', + testing_rpc_response) + + @mock.patch('socket.create_connection') + def test_rpc_truncated_logging_long_response(self, mock_create_connection): + """Test rpc response truncated with given length in DEBUG level log.""" + fake_file = self.setup_mock_socket_file(mock_create_connection) + resp_len = jsonrpc_client_base._MAX_RPC_RESP_LOGGING_LENGTH * 40 + testing_rpc_response = self.generate_rpc_response(resp_len) + fake_file.resp = testing_rpc_response + + client = FakeRpcClient() + client.connect() + + client.set_snippet_client_verbose_logging(False) + response = client._client_receive() + + self.assertEqual(response, testing_rpc_response) + # DEBUG level log should truncated by given length. + client.log.debug.assert_called_with( + 'Snippet received: %s... %d chars are truncated', + str(testing_rpc_response) + [:jsonrpc_client_base._MAX_RPC_RESP_LOGGING_LENGTH], + resp_len - jsonrpc_client_base._MAX_RPC_RESP_LOGGING_LENGTH) + if __name__ == '__main__': unittest.main() |