• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/fuzz/alarm_timer.h"
16 
17 #include <chrono>
18 
19 #include "gtest/gtest.h"
20 #include "pw_sync/binary_semaphore.h"
21 
22 namespace pw::rpc::fuzz {
23 namespace {
24 
25 using namespace std::chrono_literals;
26 
TEST(AlarmTimerTest,Start)27 TEST(AlarmTimerTest, Start) {
28   sync::BinarySemaphore sem;
29   AlarmTimer timer([&sem](chrono::SystemClock::time_point) { sem.release(); });
30   timer.Start(10ms);
31   sem.acquire();
32 }
33 
TEST(AlarmTimerTest,Restart)34 TEST(AlarmTimerTest, Restart) {
35   sync::BinarySemaphore sem;
36   AlarmTimer timer([&sem](chrono::SystemClock::time_point) { sem.release(); });
37   timer.Start(50ms);
38   for (size_t i = 0; i < 10; ++i) {
39     timer.Restart();
40     EXPECT_FALSE(sem.try_acquire_for(10us));
41   }
42   sem.acquire();
43 }
44 
TEST(AlarmTimerTest,Cancel)45 TEST(AlarmTimerTest, Cancel) {
46   sync::BinarySemaphore sem;
47   AlarmTimer timer([&sem](chrono::SystemClock::time_point) { sem.release(); });
48   timer.Start(50ms);
49   timer.Cancel();
50   EXPECT_FALSE(sem.try_acquire_for(100us));
51 }
52 
TEST(AlarmTimerTest,Destroy)53 TEST(AlarmTimerTest, Destroy) {
54   sync::BinarySemaphore sem;
55   {
56     AlarmTimer timer(
57         [&sem](chrono::SystemClock::time_point) { sem.release(); });
58     timer.Start(50ms);
59   }
60   EXPECT_FALSE(sem.try_acquire_for(100us));
61 }
62 
63 }  // namespace
64 }  // namespace pw::rpc::fuzz
65