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 #pragma once
15
16 #include <chrono>
17
18 #include "pw_assert/assert.h"
19 #include "pw_chrono/system_clock.h"
20 #include "pw_rpc/internal/fake_channel_output.h"
21 #include "pw_rpc/method_info.h"
22 #include "pw_status/status.h"
23 #include "pw_sync/counting_semaphore.h"
24 #include "pw_thread/yield.h"
25
26 namespace pw::rpc::test {
27
28 // Wait until the provided RawFakeChannelOutput, NanopbFakeChannelOutput or
29 // PwpbFakeChannelOutput receives the specified number of packets.
30 template <unsigned kTimeoutSeconds = 10, typename Function>
WaitForPackets(internal::test::FakeChannelOutput & output,int count,Function && run_before)31 void WaitForPackets(internal::test::FakeChannelOutput& output,
32 int count,
33 Function&& run_before) {
34 sync::CountingSemaphore sem;
35 output.set_on_send([&sem](ConstByteSpan, Status) { sem.release(); });
36
37 run_before();
38
39 for (int i = 0; i < count; ++i) {
40 PW_ASSERT(sem.try_acquire_for(std::chrono::seconds(kTimeoutSeconds)));
41 }
42
43 output.set_on_send(nullptr);
44 }
45
46 // Checks that kMethod was called in client_context (which is a
47 // PwpbClientTestContext or a NanopbClientTestContext) and sends the response
48 // and status back.
49 //
50 // If no kMethod was called in timeout duration - returns
51 // DEADLINE_EXCEEDED. Otherwise returns OK.
52 //
53 // Example: Let's say we are testing an RPC service (my::MyService) that as part
54 // of the call (GetData) handling does another RPC call to a different service
55 // (other::OtherService::GetPart). my::MyService constructor accepts the
56 // other::OtherService::Client as an argument. To be able to test it we need to
57 // provide a prepared response when request is sent.
58 //
59 // pw::rpc::PwpbClientTestContext client_context;
60 // other::pw_rpc::pwpb::OtherService::Client other_service_client(
61 // client_context.client(), client_context.channel().id());
62 //
63 // PW_PWPB_TEST_METHOD_CONTEXT(MyService, GetData)
64 // context(other_service_client);
65 // context.call({});
66 //
67 // ASSERT_EQ(pw::rpc::test::SendResponseIfCalled<
68 // other::pw_rpc::pwpb::OtherService::GetPart>(client_context,
69 // {.value = 42}),
70 // pw::OkStatus());
71 //
72 // // At this point we have GetData handler received the response for GetPart.
73 template <auto kMethod, typename Context>
74 Status SendResponseIfCalled(
75 Context& client_context,
76 const MethodResponseType<kMethod>& response,
77 Status status = OkStatus(),
78 chrono::SystemClock::duration timeout =
79 chrono::SystemClock::for_at_least(std::chrono::milliseconds(100))) {
80 const auto start_time = chrono::SystemClock::now();
81 while (chrono::SystemClock::now() - start_time < timeout) {
82 const auto count =
83 client_context.output().template total_payloads<kMethod>();
84 if (count > 0) {
85 client_context.server().template SendResponse<kMethod>(response, status);
86 return OkStatus();
87 }
88 this_thread::yield();
89 }
90 return Status::DeadlineExceeded();
91 }
92
93 // Shortcut for SendResponseIfCalled(client_context, {}, status, timeout).
94 template <auto kMethod, typename Context>
95 Status SendResponseIfCalled(
96 Context& client_context,
97 Status status = OkStatus(),
98 chrono::SystemClock::duration timeout =
99 chrono::SystemClock::for_at_least(std::chrono::milliseconds(100))) {
100 return SendResponseIfCalled<kMethod, Context>(
101 client_context, {}, status, timeout);
102 }
103
104 } // namespace pw::rpc::test
105