Wyatt Hepler | e04d468 | 2021-07-21 08:52:28 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | # Copyright 2021 The Pigweed Authors |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 5 | # use this file except in compliance with the License. You may obtain a copy of |
| 6 | # the License at |
| 7 | # |
| 8 | # https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | # License for the specific language governing permissions and limitations under |
| 14 | # the License. |
| 15 | """Tests using the callback client for pw_rpc.""" |
| 16 | |
| 17 | from typing import List, Tuple |
| 18 | import unittest |
| 19 | |
Wyatt Hepler | 420af4b | 2021-08-05 13:47:20 -0700 | [diff] [blame] | 20 | import pw_hdlc.rpc |
Wyatt Hepler | e04d468 | 2021-07-21 08:52:28 -0700 | [diff] [blame] | 21 | from pw_rpc import benchmark_pb2, testing |
| 22 | from pw_status import Status |
| 23 | |
| 24 | ITERATIONS = 50 |
Wyatt Hepler | e04d468 | 2021-07-21 08:52:28 -0700 | [diff] [blame] | 25 | |
| 26 | |
| 27 | class RpcIntegrationTest(unittest.TestCase): |
| 28 | """Calls RPCs on an RPC server through a socket.""" |
| 29 | test_server_command: Tuple[str, ...] = () |
Wyatt Hepler | 82d499b | 2021-08-23 09:13:16 -0700 | [diff] [blame] | 30 | port: int |
Wyatt Hepler | e04d468 | 2021-07-21 08:52:28 -0700 | [diff] [blame] | 31 | |
| 32 | def setUp(self) -> None: |
Wyatt Hepler | 420af4b | 2021-08-05 13:47:20 -0700 | [diff] [blame] | 33 | self._context = pw_hdlc.rpc.HdlcRpcLocalServerAndClient( |
Wyatt Hepler | 82d499b | 2021-08-23 09:13:16 -0700 | [diff] [blame] | 34 | self.test_server_command, self.port, [benchmark_pb2]) |
Wyatt Hepler | cf43bbf | 2021-08-11 16:34:18 -0700 | [diff] [blame] | 35 | self.rpcs = self._context.client.channel(1).rpcs |
Wyatt Hepler | e04d468 | 2021-07-21 08:52:28 -0700 | [diff] [blame] | 36 | |
| 37 | def tearDown(self) -> None: |
Wyatt Hepler | cf43bbf | 2021-08-11 16:34:18 -0700 | [diff] [blame] | 38 | self._context.close() |
Wyatt Hepler | e04d468 | 2021-07-21 08:52:28 -0700 | [diff] [blame] | 39 | |
| 40 | def test_unary(self) -> None: |
| 41 | for i in range(ITERATIONS): |
| 42 | payload = f'O_o #{i}'.encode() |
| 43 | status, reply = self.rpcs.pw.rpc.Benchmark.UnaryEcho( |
| 44 | payload=payload) |
| 45 | self.assertIs(status, Status.OK) |
| 46 | self.assertEqual(reply.payload, payload) |
| 47 | |
| 48 | def test_bidirectional(self) -> None: |
| 49 | with self.rpcs.pw.rpc.Benchmark.BidirectionalEcho.invoke() as call: |
| 50 | responses = call.get_responses() |
| 51 | |
| 52 | for i in range(ITERATIONS): |
| 53 | payload = f'O_o #{i}'.encode() |
| 54 | call.send(benchmark_pb2.Payload(payload=payload)) |
| 55 | |
| 56 | self.assertEqual(next(responses).payload, payload) |
| 57 | |
Wyatt Hepler | 420af4b | 2021-08-05 13:47:20 -0700 | [diff] [blame] | 58 | def test_bidirectional_call_twice(self) -> None: |
| 59 | rpc = self.rpcs.pw.rpc.Benchmark.BidirectionalEcho |
| 60 | |
| 61 | for _ in range(ITERATIONS): |
| 62 | first_call = rpc.invoke() |
| 63 | first_call.send(payload=b'abc') |
| 64 | self.assertEqual(next(iter(first_call)), |
| 65 | rpc.response(payload=b'abc')) |
| 66 | self.assertFalse(first_call.completed()) |
| 67 | |
| 68 | second_call = rpc.invoke() |
| 69 | second_call.send(payload=b'123') |
| 70 | self.assertEqual(next(iter(second_call)), |
| 71 | rpc.response(payload=b'123')) |
| 72 | |
| 73 | self.assertIs(first_call.error, Status.CANCELLED) |
| 74 | self.assertEqual(first_call.responses, |
| 75 | [rpc.response(payload=b'abc')]) |
| 76 | |
| 77 | self.assertFalse(second_call.completed()) |
| 78 | self.assertEqual(second_call.responses, |
| 79 | [rpc.response(payload=b'123')]) |
| 80 | |
Wyatt Hepler | e04d468 | 2021-07-21 08:52:28 -0700 | [diff] [blame] | 81 | |
Wyatt Hepler | 82d499b | 2021-08-23 09:13:16 -0700 | [diff] [blame] | 82 | def _main(test_server_command: List[str], port: int, |
| 83 | unittest_args: List[str]) -> None: |
Wyatt Hepler | e04d468 | 2021-07-21 08:52:28 -0700 | [diff] [blame] | 84 | RpcIntegrationTest.test_server_command = tuple(test_server_command) |
Wyatt Hepler | 82d499b | 2021-08-23 09:13:16 -0700 | [diff] [blame] | 85 | RpcIntegrationTest.port = port |
Wyatt Hepler | e04d468 | 2021-07-21 08:52:28 -0700 | [diff] [blame] | 86 | unittest.main(argv=unittest_args) |
| 87 | |
| 88 | |
| 89 | if __name__ == '__main__': |
| 90 | _main(**vars(testing.parse_test_server_args())) |