• 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 #pragma once
15 
16 #include <chrono>
17 
18 #include "pw_assert/assert.h"
19 #include "pw_rpc/internal/fake_channel_output.h"
20 #include "pw_sync/counting_semaphore.h"
21 
22 namespace pw::rpc::test {
23 
24 // Wait until the provided RawFakeChannelOutput or NanopbFakeChannelOutput
25 // receives the specified number of packets.
26 template <unsigned kTimeoutSeconds = 10, typename Function>
WaitForPackets(internal::test::FakeChannelOutput & output,int count,Function && run_before)27 void WaitForPackets(internal::test::FakeChannelOutput& output,
28                     int count,
29                     Function&& run_before) {
30   sync::CountingSemaphore sem;
31   output.set_on_send([&sem](ConstByteSpan, Status) { sem.release(); });
32 
33   run_before();
34 
35   for (int i = 0; i < count; ++i) {
36     PW_ASSERT(sem.try_acquire_for(std::chrono::seconds(kTimeoutSeconds)));
37   }
38 
39   output.set_on_send(nullptr);
40 }
41 
42 }  // namespace pw::rpc::test
43