blob: db9d35cbe7bb4e18d25f897fa9f88b0c6103edd4 [file] [log] [blame]
Wyatt Heplere04d4682021-07-21 08:52:28 -07001#!/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
17from typing import List, Tuple
18import unittest
19
Wyatt Hepler420af4b2021-08-05 13:47:20 -070020import pw_hdlc.rpc
Wyatt Heplere04d4682021-07-21 08:52:28 -070021from pw_rpc import benchmark_pb2, testing
22from pw_status import Status
23
24ITERATIONS = 50
Wyatt Heplere04d4682021-07-21 08:52:28 -070025
26
27class RpcIntegrationTest(unittest.TestCase):
28 """Calls RPCs on an RPC server through a socket."""
29 test_server_command: Tuple[str, ...] = ()
Wyatt Hepler82d499b2021-08-23 09:13:16 -070030 port: int
Wyatt Heplere04d4682021-07-21 08:52:28 -070031
32 def setUp(self) -> None:
Wyatt Hepler420af4b2021-08-05 13:47:20 -070033 self._context = pw_hdlc.rpc.HdlcRpcLocalServerAndClient(
Wyatt Hepler82d499b2021-08-23 09:13:16 -070034 self.test_server_command, self.port, [benchmark_pb2])
Wyatt Heplercf43bbf2021-08-11 16:34:18 -070035 self.rpcs = self._context.client.channel(1).rpcs
Wyatt Heplere04d4682021-07-21 08:52:28 -070036
37 def tearDown(self) -> None:
Wyatt Heplercf43bbf2021-08-11 16:34:18 -070038 self._context.close()
Wyatt Heplere04d4682021-07-21 08:52:28 -070039
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 Hepler420af4b2021-08-05 13:47:20 -070058 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 Heplere04d4682021-07-21 08:52:28 -070081
Wyatt Hepler82d499b2021-08-23 09:13:16 -070082def _main(test_server_command: List[str], port: int,
83 unittest_args: List[str]) -> None:
Wyatt Heplere04d4682021-07-21 08:52:28 -070084 RpcIntegrationTest.test_server_command = tuple(test_server_command)
Wyatt Hepler82d499b2021-08-23 09:13:16 -070085 RpcIntegrationTest.port = port
Wyatt Heplere04d4682021-07-21 08:52:28 -070086 unittest.main(argv=unittest_args)
87
88
89if __name__ == '__main__':
90 _main(**vars(testing.parse_test_server_args()))