aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorMinghao Li <minghaoli@google.com>2022-04-09 11:13:36 +0800
committerGitHub <noreply@github.com>2022-04-08 20:13:36 -0700
commit8e0499c80d339eeb411ae28f5e328af722d7302d (patch)
tree3fb7ccac6fb70c91ffe9295968ec97cae431ecbb /tests
parentd1b010452445ec06207e03de23ae94cbe95a0bc7 (diff)
downloadmobly-8e0499c80d339eeb411ae28f5e328af722d7302d.tar.gz
Rename Snippet client v2's startup sequence steps (#806)
Rename the startup sequence steps (hopefully) for the better.
Diffstat (limited to 'tests')
-rwxr-xr-xtests/mobly/snippet/client_base_test.py111
1 files changed, 47 insertions, 64 deletions
diff --git a/tests/mobly/snippet/client_base_test.py b/tests/mobly/snippet/client_base_test.py
index 4da74d6..3eb6521 100755
--- a/tests/mobly/snippet/client_base_test.py
+++ b/tests/mobly/snippet/client_base_test.py
@@ -26,7 +26,7 @@ from mobly.snippet import errors
def _generate_fix_length_rpc_response(
response_length,
template='{"id": 0, "result": "%s", "error": null, "callback": null}'):
- """Generates a RPC response string with specified length.
+ """Generates an RPC response string with specified length.
This function generates a random string and formats the template with the
generated random string to get the response string. This function formats
@@ -66,13 +66,10 @@ class FakeClient(client_base.ClientBase):
def before_starting_server(self):
pass
- def do_start_server(self):
+ def start_server(self):
pass
- def build_connection(self):
- pass
-
- def after_starting_server(self):
+ def make_connection(self):
pass
def restore_server_connection(self, port=None):
@@ -103,70 +100,56 @@ class ClientBaseTest(unittest.TestCase):
self.client.host_port = 12345
@mock.patch.object(FakeClient, 'before_starting_server')
- @mock.patch.object(FakeClient, 'do_start_server')
- @mock.patch.object(FakeClient, '_build_connection')
- @mock.patch.object(FakeClient, 'after_starting_server')
- def test_start_server_stage_order(self, mock_after_func, mock_build_conn_func,
- mock_do_start_func, mock_before_func):
- """Test that starting server runs its stages in expected order."""
+ @mock.patch.object(FakeClient, 'start_server')
+ @mock.patch.object(FakeClient, '_make_connection')
+ def test_init_server_stage_order(self, mock_make_conn_func, mock_start_func,
+ mock_before_func):
+ """Test that initialization runs its stages in expected order."""
order_manager = mock.Mock()
order_manager.attach_mock(mock_before_func, 'mock_before_func')
- order_manager.attach_mock(mock_do_start_func, 'mock_do_start_func')
- order_manager.attach_mock(mock_build_conn_func, 'mock_build_conn_func')
- order_manager.attach_mock(mock_after_func, 'mock_after_func')
+ order_manager.attach_mock(mock_start_func, 'mock_start_func')
+ order_manager.attach_mock(mock_make_conn_func, 'mock_make_conn_func')
- self.client.start_server()
+ self.client.initialize()
expected_call_order = [
mock.call.mock_before_func(),
- mock.call.mock_do_start_func(),
- mock.call.mock_build_conn_func(),
- mock.call.mock_after_func(),
+ mock.call.mock_start_func(),
+ mock.call.mock_make_conn_func(),
]
self.assertListEqual(order_manager.mock_calls, expected_call_order)
@mock.patch.object(FakeClient, 'stop_server')
@mock.patch.object(FakeClient, 'before_starting_server')
- def test_start_server_before_starting_server_fail(self, mock_before_func,
- mock_stop_server):
- """Test starting server's stage before_starting_server fails."""
+ def test_init_server_before_starting_server_fail(self, mock_before_func,
+ mock_stop_server):
+ """Test before_starting_server stage of initialization fails."""
mock_before_func.side_effect = Exception('ha')
with self.assertRaisesRegex(Exception, 'ha'):
- self.client.start_server()
+ self.client.initialize()
mock_stop_server.assert_not_called()
@mock.patch.object(FakeClient, 'stop_server')
- @mock.patch.object(FakeClient, 'do_start_server')
- def test_start_server_do_start_server_fail(self, mock_do_start_func,
- mock_stop_server):
- """Test starting server's stage do_start_server fails."""
- mock_do_start_func.side_effect = Exception('ha')
+ @mock.patch.object(FakeClient, 'start_server')
+ def test_init_server_start_server_fail(self, mock_start_func,
+ mock_stop_server):
+ """Test start_server stage of initialization fails."""
+ mock_start_func.side_effect = Exception('ha')
with self.assertRaisesRegex(Exception, 'ha'):
- self.client.start_server()
+ self.client.initialize()
mock_stop_server.assert_called()
@mock.patch.object(FakeClient, 'stop_server')
- @mock.patch.object(FakeClient, '_build_connection')
- def test_start_server_build_connection_fail(self, mock_build_conn_func,
- mock_stop_server):
- """Test starting server's stage _build_connection fails."""
- mock_build_conn_func.side_effect = Exception('ha')
-
- with self.assertRaisesRegex(Exception, 'ha'):
- self.client.start_server()
- mock_stop_server.assert_called()
-
- @mock.patch.object(FakeClient, 'stop_server')
- @mock.patch.object(FakeClient, 'after_starting_server')
- def test_start_server_after_starting_server_fail(self, mock_after_func,
- mock_stop_server):
- """Test starting server's stage after_starting_server fails."""
- mock_after_func.side_effect = Exception('ha')
+ @mock.patch.object(FakeClient, '_make_connection')
+ def test_init_server_make_connection_fail(self, mock_make_conn_func,
+ mock_stop_server):
+ """Test _make_connection stage of initialization fails."""
+ mock_make_conn_func.side_effect = Exception('ha')
with self.assertRaisesRegex(Exception, 'ha'):
- self.client.start_server()
+ self.client.initialize()
mock_stop_server.assert_called()
@mock.patch.object(FakeClient, 'check_server_proc_running')
@@ -177,9 +160,9 @@ class ClientBaseTest(unittest.TestCase):
def test_rpc_stage_dependencies(self, mock_handle_resp, mock_decode_resp_str,
mock_send_request, mock_gen_request,
mock_precheck):
- """Test the internal dependencies when sending a RPC.
+ """Test the internal dependencies when sending an RPC.
- When sending a RPC, it calls multiple functions in specific order, and
+ When sending an RPC, it calls multiple functions in specific order, and
each function uses the output of the previously called function. This test
case checks above dependencies.
@@ -191,7 +174,7 @@ class ClientBaseTest(unittest.TestCase):
mock_gen_request: the mock function of FakeClient._gen_rpc_request.
mock_precheck: the mock function of FakeClient.check_server_proc_running.
"""
- self.client.start_server()
+ self.client.initialize()
expected_response_str = ('{"id": 0, "result": 123, "error": null, '
'"callback": null}')
@@ -226,8 +209,8 @@ class ClientBaseTest(unittest.TestCase):
def test_rpc_precheck_fail(self, mock_handle_resp, mock_decode_resp_str,
mock_send_request, mock_gen_request,
mock_precheck):
- """Test when RPC precheck fails it will skip sending RPC."""
- self.client.start_server()
+ """Test when RPC precheck fails it will skip sending the RPC."""
+ self.client.initialize()
mock_precheck.side_effect = Exception('server_died')
with self.assertRaisesRegex(Exception, 'server_died'):
@@ -239,7 +222,7 @@ class ClientBaseTest(unittest.TestCase):
mock_decode_resp_str.assert_not_called()
def test_gen_request(self):
- """Test generating a RPC request.
+ """Test generating an RPC request.
Test that _gen_rpc_request returns a string represents a JSON dict
with all required fields.
@@ -270,7 +253,7 @@ class ClientBaseTest(unittest.TestCase):
self.client._decode_response_string_and_validate_format(0, None)
def test_rpc_response_missing_fields(self):
- """Test parsing a RPC response that misses some required fields."""
+ """Test parsing an RPC response that misses some required fields."""
mock_resp_without_id = '{"result": 123, "error": null, "callback": null}'
with self.assertRaisesRegex(
errors.ProtocolError,
@@ -300,7 +283,7 @@ class ClientBaseTest(unittest.TestCase):
10, mock_resp_without_callback)
def test_rpc_response_error(self):
- """Test parsing a RPC response with a non-empty error field."""
+ """Test parsing an RPC response with a non-empty error field."""
mock_resp_with_error = {
'id': 10,
'result': 123,
@@ -343,7 +326,7 @@ class ClientBaseTest(unittest.TestCase):
mock_handle_callback.assert_not_called()
def test_rpc_response_id_mismatch(self):
- """Test parsing a RPC response with wrong id."""
+ """Test parsing an RPC response with a wrong id."""
right_id = 5
wrong_id = 20
resp = f'{{"id": {right_id}, "result": 1, "error": null, "callback": null}}'
@@ -358,7 +341,7 @@ class ClientBaseTest(unittest.TestCase):
mock_log = mock.Mock()
self.client.log = mock_log
self.client.set_snippet_client_verbose_logging(True)
- self.client.start_server()
+ self.client.initialize()
resp = _generate_fix_length_rpc_response(
client_base._MAX_RPC_RESP_LOGGING_LENGTH * 2)
@@ -372,7 +355,7 @@ class ClientBaseTest(unittest.TestCase):
mock_log = mock.Mock()
self.client.log = mock_log
self.client.set_snippet_client_verbose_logging(False)
- self.client.start_server()
+ self.client.initialize()
resp = _generate_fix_length_rpc_response(
int(client_base._MAX_RPC_RESP_LOGGING_LENGTH // 2))
@@ -386,7 +369,7 @@ class ClientBaseTest(unittest.TestCase):
mock_log = mock.Mock()
self.client.log = mock_log
self.client.set_snippet_client_verbose_logging(False)
- self.client.start_server()
+ self.client.initialize()
resp = _generate_fix_length_rpc_response(
client_base._MAX_RPC_RESP_LOGGING_LENGTH)
@@ -400,7 +383,7 @@ class ClientBaseTest(unittest.TestCase):
mock_log = mock.Mock()
self.client.log = mock_log
self.client.set_snippet_client_verbose_logging(False)
- self.client.start_server()
+ self.client.initialize()
max_len = client_base._MAX_RPC_RESP_LOGGING_LENGTH
resp = _generate_fix_length_rpc_response(max_len * 40)
@@ -414,7 +397,7 @@ class ClientBaseTest(unittest.TestCase):
@mock.patch.object(FakeClient, 'send_rpc_request')
def test_rpc_call_increment_counter(self, mock_send_request):
"""Test that with each RPC call the counter is incremented by 1."""
- self.client.start_server()
+ self.client.initialize()
resp = '{"id": %d, "result": 123, "error": null, "callback": null}'
mock_send_request.side_effect = (resp % (i,) for i in range(10))
@@ -424,9 +407,9 @@ class ClientBaseTest(unittest.TestCase):
self.assertEqual(next(self.client._counter), 10)
@mock.patch.object(FakeClient, 'send_rpc_request')
- def test_build_connection_reset_counter(self, mock_send_request):
- """Test that _build_connection resets the counter to zero."""
- self.client.start_server()
+ def test_init_connection_reset_counter(self, mock_send_request):
+ """Test that _make_connection resets the counter to zero."""
+ self.client.initialize()
resp = '{"id": %d, "result": 123, "error": null, "callback": null}'
mock_send_request.side_effect = (resp % (i,) for i in range(10))
@@ -434,7 +417,7 @@ class ClientBaseTest(unittest.TestCase):
self.client.some_rpc()
self.assertEqual(next(self.client._counter), 10)
- self.client._build_connection()
+ self.client._make_connection()
self.assertEqual(next(self.client._counter), 0)