xref: /aosp_15_r20/external/pigweed/pw_async2/system_time_provider_test.cc (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1 // Copyright 2024 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_async2/system_time_provider.h"
16 
17 #include <chrono>
18 
19 #include "pw_unit_test/framework.h"
20 
21 namespace {
22 
23 using ::pw::async2::Context;
24 using ::pw::async2::Dispatcher;
25 using ::pw::async2::GetSystemTimeProvider;
26 using ::pw::async2::Pending;
27 using ::pw::async2::Poll;
28 using ::pw::async2::Ready;
29 using ::pw::async2::Task;
30 using ::pw::async2::TimeFuture;
31 using ::pw::chrono::SystemClock;
32 using ::std::chrono_literals::operator""ms;
33 using ::std::chrono_literals::operator""s;
34 
35 struct WaitTask : public Task {
WaitTask__anond02169300111::WaitTask36   WaitTask(TimeFuture<SystemClock>&& future)
37       : time_completed_(Pending()), future_(std::move(future)) {}
38 
DoPend__anond02169300111::WaitTask39   Poll<> DoPend(Context& cx) final {
40     if (future_.Pend(cx).IsPending()) {
41       return Pending();
42     }
43     time_completed_ = SystemClock::now();
44     return Ready();
45   }
46 
47   Poll<SystemClock::time_point> time_completed_ = Pending();
48   TimeFuture<SystemClock> future_;
49 };
50 
TEST(SystemTimeProvider,InvokesTimerAfterDelay)51 TEST(SystemTimeProvider, InvokesTimerAfterDelay) {
52   SystemClock::time_point start_time = SystemClock().now();
53   SystemClock::time_point expected_completion = start_time + 50ms;
54   WaitTask task(GetSystemTimeProvider().WaitUntil(expected_completion));
55   Dispatcher dispatcher;
56   dispatcher.Post(task);
57   dispatcher.RunToCompletion();
58   ASSERT_TRUE(task.time_completed_.IsReady());
59   EXPECT_GE(*task.time_completed_, expected_completion);
60 }
61 
TEST(SystemTimeProvider,InvokesTwoTimersInOrder)62 TEST(SystemTimeProvider, InvokesTwoTimersInOrder) {
63   SystemClock::time_point start_time = SystemClock().now();
64   SystemClock::time_point expected_c1 = start_time + 200ms;
65   SystemClock::time_point expected_c2 = start_time + 10ms;
66   WaitTask t1(GetSystemTimeProvider().WaitUntil(expected_c1));
67   WaitTask t2(GetSystemTimeProvider().WaitUntil(expected_c2));
68   Dispatcher dispatcher;
69   dispatcher.Post(t1);
70   dispatcher.Post(t2);
71   dispatcher.RunToCompletion();
72   ASSERT_TRUE(t1.time_completed_.IsReady());
73   EXPECT_GE(t1.time_completed_->time_since_epoch().count(),
74             expected_c1.time_since_epoch().count());
75   ASSERT_TRUE(t2.time_completed_.IsReady());
76   EXPECT_GE(*t2.time_completed_, expected_c2);
77   EXPECT_GT(*t1.time_completed_, *t2.time_completed_);
78 }
79 
TEST(SystemTimeProvider,CancelThroughDestruction)80 TEST(SystemTimeProvider, CancelThroughDestruction) {
81   // We can't control the SystemClock's period configuration, so just in case
82   // duration cannot be accurately expressed in integer ticks, round the
83   // duration up.
84   constexpr SystemClock::duration kRoundedArbitraryLongDuration =
85       SystemClock::for_at_least(1s);
86 
87   std::ignore = GetSystemTimeProvider().WaitFor(kRoundedArbitraryLongDuration);
88   // TimeFuture destroyed here and not used.
89 }
90 
91 }  // namespace
92