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(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 for (size_t i = 0; i < kNumRequests; i++) {
109 receivers.push_back(client.Echo(
110 {.msg = kTestMessage},
111 [&state](const pw_rpc_transport::testing::pwpb::EchoMessage::Message&
112 response,
113 Status status) {
114 EXPECT_EQ(status, OkStatus());
115 EXPECT_EQ(response.msg, kTestMessage);
116 state.successes++;
117 state.sem.release();
118 },
119 [&state](Status) {
120 state.errors++;
121 state.sem.release();
122 }));
123 }
124
125 for (size_t i = 0; i < kNumRequests; i++) {
126 state.sem.acquire();
127 }
128
129 EXPECT_EQ(state.successes.load(), kNumRequests);
130 EXPECT_EQ(state.errors.load(), 0u);
131
132 egress.Stop();
133 egress_thread.join();
134 }
135
TEST(LocalRpcEgressTest,PacketQueueExhausted)136 TEST(LocalRpcEgressTest, PacketQueueExhausted) {
137 constexpr size_t kMaxPacketSize = 100;
138 constexpr size_t kPacketQueueSize = 1;
139 constexpr uint32_t kChannelId = 1;
140
141 LocalRpcEgress<kPacketQueueSize, kMaxPacketSize> egress;
142 std::array channels = {rpc::Channel::Create<kChannelId>(&egress)};
143 ServiceRegistry registry(channels);
144
145 ControlledTestEchoService service;
146 registry.RegisterService(service);
147
148 egress.set_packet_processor(registry);
149 auto egress_thread = thread::Thread(thread::stl::Options(), egress);
150
151 auto client =
152 registry
153 .CreateClient<pw_rpc_transport::testing::pw_rpc::pwpb::TestService>(
154 kChannelId);
155
156 auto receiver = client.Echo({.msg = kTestMessage});
157 service.Wait();
158
159 // echo_call is blocked in ServiceRegistry waiting for the Proceed() call.
160 // Since there is only one packet queue buffer available at a time, other
161 // packets will get rejected with RESOURCE_EXHAUSTED error until the first
162 // one is handled.
163 EXPECT_EQ(egress.Send({}), Status::ResourceExhausted());
164 service.Proceed();
165
166 // Expecting egress to return the packet queue buffer within a reasonable
167 // amount of time; currently there is no way to explicitly synchronize on
168 // its availability, so we give it few seconds to recover.
169 auto deadline = chrono::SystemClock::now() + 5s;
170 bool egress_ok = false;
171 while (chrono::SystemClock::now() <= deadline) {
172 if (egress.Send({}).ok()) {
173 egress_ok = true;
174 break;
175 }
176 }
177
178 EXPECT_TRUE(egress_ok);
179
180 egress.Stop();
181 egress_thread.join();
182 }
183
TEST(LocalRpcEgressTest,NoPacketProcessor)184 TEST(LocalRpcEgressTest, NoPacketProcessor) {
185 constexpr size_t kPacketQueueSize = 10;
186 constexpr size_t kMaxPacketSize = 10;
187 LocalRpcEgress<kPacketQueueSize, kMaxPacketSize> egress;
188 EXPECT_EQ(egress.Send({}), Status::FailedPrecondition());
189 }
190
TEST(LocalRpcEgressTest,PacketTooBig)191 TEST(LocalRpcEgressTest, PacketTooBig) {
192 constexpr size_t kPacketQueueSize = 10;
193 constexpr size_t kMaxPacketSize = 10;
194 constexpr uint32_t kChannelId = 1;
195 LocalRpcEgress<kPacketQueueSize, kMaxPacketSize> egress;
196
197 std::array<std::byte, kMaxPacketSize + 1> packet{};
198 std::array channels = {rpc::Channel::Create<kChannelId>(&egress)};
199 ServiceRegistry registry(channels);
200 egress.set_packet_processor(registry);
201
202 EXPECT_EQ(egress.Send(packet), Status::InvalidArgument());
203 }
204
TEST(LocalRpcEgressTest,EgressStopped)205 TEST(LocalRpcEgressTest, EgressStopped) {
206 constexpr size_t kPacketQueueSize = 10;
207 constexpr size_t kMaxPacketSize = 10;
208 constexpr uint32_t kChannelId = 1;
209 LocalRpcEgress<kPacketQueueSize, kMaxPacketSize> egress;
210
211 std::array channels = {rpc::Channel::Create<kChannelId>(&egress)};
212 ServiceRegistry registry(channels);
213 egress.set_packet_processor(registry);
214
215 auto egress_thread = thread::Thread(thread::stl::Options(), egress);
216 EXPECT_EQ(egress.Send({}), OkStatus());
217 egress.Stop();
218 EXPECT_EQ(egress.Send({}), Status::FailedPrecondition());
219
220 egress_thread.join();
221 }
222
223 } // namespace
224 } // namespace pw::rpc
225