• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2022 The Pigweed Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not
4 // use this file except in compliance with the License. You may obtain a copy of
5 // the License at
6 //
7 //     https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 // License for the specific language governing permissions and limitations under
13 // the License.
14 
15 #include "gtest/gtest.h"
16 #include "pw_rpc/pwpb/client_server_testing.h"
17 #include "pw_rpc_test_protos/test.rpc.pwpb.h"
18 
19 namespace pw::rpc {
20 namespace {
21 
22 namespace TestRequest = ::pw::rpc::test::pwpb::TestRequest;
23 namespace TestResponse = ::pw::rpc::test::pwpb::TestResponse;
24 namespace TestStreamResponse = ::pw::rpc::test::pwpb::TestStreamResponse;
25 
26 }  // namespace
27 
28 namespace test {
29 
30 using GeneratedService = ::pw::rpc::test::pw_rpc::pwpb::TestService;
31 
32 class TestService final : public GeneratedService::Service<TestService> {
33  public:
TestUnaryRpc(const TestRequest::Message & request,TestResponse::Message & response)34   Status TestUnaryRpc(const TestRequest::Message& request,
35                       TestResponse::Message& response) {
36     response.value = request.integer + 1;
37     return static_cast<Status::Code>(request.status_code);
38   }
39 
TestAnotherUnaryRpc(const TestRequest::Message &,PwpbUnaryResponder<TestResponse::Message> &)40   void TestAnotherUnaryRpc(const TestRequest::Message&,
41                            PwpbUnaryResponder<TestResponse::Message>&) {}
42 
TestServerStreamRpc(const TestRequest::Message &,ServerWriter<TestStreamResponse::Message> &)43   static void TestServerStreamRpc(const TestRequest::Message&,
44                                   ServerWriter<TestStreamResponse::Message>&) {}
45 
TestClientStreamRpc(ServerReader<TestRequest::Message,TestStreamResponse::Message> &)46   void TestClientStreamRpc(
47       ServerReader<TestRequest::Message, TestStreamResponse::Message>&) {}
48 
TestBidirectionalStreamRpc(ServerReaderWriter<TestRequest::Message,TestStreamResponse::Message> &)49   void TestBidirectionalStreamRpc(
50       ServerReaderWriter<TestRequest::Message, TestStreamResponse::Message>&) {}
51 };
52 
53 }  // namespace test
54 
55 namespace {
56 
TEST(PwpbClientServerTestContext,ReceivesUnaryRpcReponse)57 TEST(PwpbClientServerTestContext, ReceivesUnaryRpcReponse) {
58   PwpbClientServerTestContext<> ctx;
59   test::TestService service;
60   ctx.server().RegisterService(service);
61 
62   TestResponse::Message response = {};
63   auto handler = [&response](const TestResponse::Message& server_response,
64                              pw::Status) { response = server_response; };
65 
66   TestRequest::Message request{.integer = 1, .status_code = OkStatus().code()};
67   auto call = test::GeneratedService::TestUnaryRpc(
68       ctx.client(), ctx.channel().id(), request, handler);
69   // Force manual forwarding of packets as context is not threaded
70   ctx.ForwardNewPackets();
71 
72   const auto sent_request =
73       ctx.request<test::pw_rpc::pwpb::TestService::TestUnaryRpc>(0);
74   const auto sent_response =
75       ctx.response<test::pw_rpc::pwpb::TestService::TestUnaryRpc>(0);
76 
77   EXPECT_EQ(response.value, sent_response.value);
78   EXPECT_EQ(response.value, request.integer + 1);
79   EXPECT_EQ(request.integer, sent_request.integer);
80 }
81 
TEST(PwpbClientServerTestContext,ReceivesMultipleReponses)82 TEST(PwpbClientServerTestContext, ReceivesMultipleReponses) {
83   PwpbClientServerTestContext<> ctx;
84   test::TestService service;
85   ctx.server().RegisterService(service);
86 
87   TestResponse::Message response1 = {};
88   TestResponse::Message response2 = {};
89   auto handler1 = [&response1](const TestResponse::Message& server_response,
90                                pw::Status) { response1 = server_response; };
91   auto handler2 = [&response2](const TestResponse::Message& server_response,
92                                pw::Status) { response2 = server_response; };
93 
94   TestRequest::Message request1{.integer = 1, .status_code = OkStatus().code()};
95   TestRequest::Message request2{.integer = 2, .status_code = OkStatus().code()};
96   const auto call1 = test::GeneratedService::TestUnaryRpc(
97       ctx.client(), ctx.channel().id(), request1, handler1);
98   // Force manual forwarding of packets as context is not threaded
99   ctx.ForwardNewPackets();
100   const auto call2 = test::GeneratedService::TestUnaryRpc(
101       ctx.client(), ctx.channel().id(), request2, handler2);
102   // Force manual forwarding of packets as context is not threaded
103   ctx.ForwardNewPackets();
104 
105   const auto sent_request1 =
106       ctx.request<test::pw_rpc::pwpb::TestService::TestUnaryRpc>(0);
107   const auto sent_request2 =
108       ctx.request<test::pw_rpc::pwpb::TestService::TestUnaryRpc>(1);
109   const auto sent_response1 =
110       ctx.response<test::pw_rpc::pwpb::TestService::TestUnaryRpc>(0);
111   const auto sent_response2 =
112       ctx.response<test::pw_rpc::pwpb::TestService::TestUnaryRpc>(1);
113 
114   EXPECT_EQ(response1.value, request1.integer + 1);
115   EXPECT_EQ(response2.value, request2.integer + 1);
116   EXPECT_EQ(response1.value, sent_response1.value);
117   EXPECT_EQ(response2.value, sent_response2.value);
118   EXPECT_EQ(request1.integer, sent_request1.integer);
119   EXPECT_EQ(request2.integer, sent_request2.integer);
120 }
121 
122 }  // namespace
123 }  // namespace pw::rpc
124