xref: /aosp_15_r20/external/pigweed/pw_rpc_transport/local_rpc_egress_test.cc (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1 // Copyright 2023 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 "pw_rpc_transport/local_rpc_egress.h"
16 
17 #include "pw_chrono/system_clock.h"
18 #include "pw_log/log.h"
19 #include "pw_rpc/client_server.h"
20 #include "pw_rpc/packet_meta.h"
21 #include "pw_rpc_transport/internal/test.rpc.pwpb.h"
22 #include "pw_rpc_transport/rpc_transport.h"
23 #include "pw_rpc_transport/service_registry.h"
24 #include "pw_status/status.h"
25 #include "pw_sync/counting_semaphore.h"
26 #include "pw_sync/thread_notification.h"
27 #include "pw_thread/thread.h"
28 #include "pw_thread_stl/options.h"
29 #include "pw_unit_test/framework.h"
30 
31 namespace pw::rpc {
32 namespace {
33 
34 using namespace std::literals::chrono_literals;
35 using namespace std::literals::string_view_literals;
36 
37 const auto kTestMessage = "I hope that someone gets my message in a bottle"sv;
38 
39 class TestEchoService final
40     : public pw_rpc_transport::testing::pw_rpc::pwpb::TestService::Service<
41           TestEchoService> {
42  public:
Echo(const pw_rpc_transport::testing::pwpb::EchoMessage::Message & request,pw_rpc_transport::testing::pwpb::EchoMessage::Message & response)43   Status Echo(
44       const pw_rpc_transport::testing::pwpb::EchoMessage::Message& request,
45       pw_rpc_transport::testing::pwpb::EchoMessage::Message& response) {
46     response.msg = request.msg;
47     return OkStatus();
48   }
49 };
50 
51 // Test service that can be controlled from the test, e.g. the test can tell the
52 // service when it's OK to proceed. Useful for testing packet queue exhaustion.
53 class ControlledTestEchoService final
54     : public pw_rpc_transport::testing::pw_rpc::pwpb::TestService::Service<
55           ControlledTestEchoService> {
56  public:
Echo(const pw_rpc_transport::testing::pwpb::EchoMessage::Message & request,pw_rpc_transport::testing::pwpb::EchoMessage::Message & response)57   Status Echo(
58       const pw_rpc_transport::testing::pwpb::EchoMessage::Message& request,
59       pw_rpc_transport::testing::pwpb::EchoMessage::Message& response) {
60     start_.release();
61     process_.acquire();
62     response.msg = request.msg;
63     return OkStatus();
64   }
65 
Wait()66   void Wait() { start_.acquire(); }
Proceed()67   void Proceed() { process_.release(); }
68 
69  private:
70   sync::ThreadNotification start_;
71   sync::ThreadNotification process_;
72 };
73 
TEST(LocalRpcEgressTest,PacketsGetDeliveredToPacketProcessor)74 TEST(LocalRpcEgressTest, PacketsGetDeliveredToPacketProcessor) {
75   constexpr size_t kMaxPacketSize = 100;
76   constexpr size_t kNumRequests = 10;
77   // Size the queue so we don't exhaust it (we don't want this test to flake;
78   // exhaustion is tested separately).
79   constexpr size_t kPacketQueueSize = 2 * kNumRequests;
80   constexpr uint32_t kChannelId = 1;
81 
82   LocalRpcEgress<kPacketQueueSize, kMaxPacketSize> egress;
83   std::array channels = {rpc::Channel::Create<kChannelId>(&egress)};
84   ServiceRegistry registry(channels);
85 
86   TestEchoService service;
87   registry.RegisterService(service);
88 
89   egress.set_packet_processor(registry);
90   auto egress_thread = Thread(thread::stl::Options(), egress);
91 
92   auto client =
93       registry
94           .CreateClient<pw_rpc_transport::testing::pw_rpc::pwpb::TestService>(
95               kChannelId);
96 
97   std::vector<rpc::PwpbUnaryReceiver<
98       pw_rpc_transport::testing::pwpb::EchoMessage::Message>>
99       receivers;
100 
101   struct State {
102     // Stash the receivers to keep the calls alive.
103     std::atomic<uint32_t> successes = 0;
104     std::atomic<uint32_t> errors = 0;
105     sync::CountingSemaphore sem;
106   } state;
107 
108   receivers.reserve(kNumRequests);
109   for (size_t i = 0; i < kNumRequests; i++) {
110     receivers.push_back(client.Echo(
111         {.msg = kTestMessage},
112         [&state](const pw_rpc_transport::testing::pwpb::EchoMessage::Message&
113                      response,
114                  Status status) {
115           EXPECT_EQ(status, OkStatus());
116           EXPECT_EQ(response.msg, kTestMessage);
117           state.successes++;
118           state.sem.release();
119         },
120         [&state](Status) {
121           state.errors++;
122           state.sem.release();
123         }));
124   }
125 
126   for (size_t i = 0; i < kNumRequests; i++) {
127     state.sem.acquire();
128   }
129 
130   EXPECT_EQ(state.successes.load(), kNumRequests);
131   EXPECT_EQ(state.errors.load(), 0u);
132 
133   egress.Stop();
134   egress_thread.join();
135 }
136 
TEST(LocalRpcEgressTest,PacketQueueExhausted)137 TEST(LocalRpcEgressTest, PacketQueueExhausted) {
138   constexpr size_t kMaxPacketSize = 100;
139   constexpr size_t kPacketQueueSize = 1;
140   constexpr uint32_t kChannelId = 1;
141 
142   LocalRpcEgress<kPacketQueueSize, kMaxPacketSize> egress;
143   std::array channels = {rpc::Channel::Create<kChannelId>(&egress)};
144   ServiceRegistry registry(channels);
145 
146   ControlledTestEchoService service;
147   registry.RegisterService(service);
148 
149   egress.set_packet_processor(registry);
150   auto egress_thread = Thread(thread::stl::Options(), egress);
151 
152   auto client =
153       registry
154           .CreateClient<pw_rpc_transport::testing::pw_rpc::pwpb::TestService>(
155               kChannelId);
156 
157   auto receiver = client.Echo({.msg = kTestMessage});
158   service.Wait();
159 
160   // echo_call is blocked in ServiceRegistry waiting for the Proceed() call.
161   // Since there is only one packet queue buffer available at a time, other
162   // packets will get rejected with RESOURCE_EXHAUSTED error until the first
163   // one is handled.
164   EXPECT_EQ(egress.Send({}), Status::ResourceExhausted());
165   service.Proceed();
166 
167   // Expecting egress to return the packet queue buffer within a reasonable
168   // amount of time; currently there is no way to explicitly synchronize on
169   // its availability, so we give it few seconds to recover.
170   auto deadline = chrono::SystemClock::now() + 5s;
171   bool egress_ok = false;
172   while (chrono::SystemClock::now() <= deadline) {
173     if (egress.Send({}).ok()) {
174       egress_ok = true;
175       break;
176     }
177   }
178 
179   EXPECT_TRUE(egress_ok);
180 
181   egress.Stop();
182   egress_thread.join();
183 }
184 
TEST(LocalRpcEgressTest,NoPacketProcessor)185 TEST(LocalRpcEgressTest, NoPacketProcessor) {
186   constexpr size_t kPacketQueueSize = 10;
187   constexpr size_t kMaxPacketSize = 10;
188   LocalRpcEgress<kPacketQueueSize, kMaxPacketSize> egress;
189   EXPECT_EQ(egress.Send({}), Status::FailedPrecondition());
190 }
191 
TEST(LocalRpcEgressTest,PacketTooBig)192 TEST(LocalRpcEgressTest, PacketTooBig) {
193   constexpr size_t kPacketQueueSize = 10;
194   constexpr size_t kMaxPacketSize = 10;
195   constexpr uint32_t kChannelId = 1;
196   LocalRpcEgress<kPacketQueueSize, kMaxPacketSize> egress;
197 
198   std::array<std::byte, kMaxPacketSize + 1> packet{};
199   std::array channels = {rpc::Channel::Create<kChannelId>(&egress)};
200   ServiceRegistry registry(channels);
201   egress.set_packet_processor(registry);
202 
203   EXPECT_EQ(egress.Send(packet), Status::InvalidArgument());
204 }
205 
TEST(LocalRpcEgressTest,EgressStopped)206 TEST(LocalRpcEgressTest, EgressStopped) {
207   constexpr size_t kPacketQueueSize = 10;
208   constexpr size_t kMaxPacketSize = 10;
209   constexpr uint32_t kChannelId = 1;
210   LocalRpcEgress<kPacketQueueSize, kMaxPacketSize> egress;
211 
212   std::array channels = {rpc::Channel::Create<kChannelId>(&egress)};
213   ServiceRegistry registry(channels);
214   egress.set_packet_processor(registry);
215 
216   auto egress_thread = Thread(thread::stl::Options(), egress);
217   EXPECT_EQ(egress.Send({}), OkStatus());
218   egress.Stop();
219   EXPECT_EQ(egress.Send({}), Status::FailedPrecondition());
220 
221   egress_thread.join();
222 }
223 
224 }  // namespace
225 }  // namespace pw::rpc
226