1 // Copyright 2020 The Pigweed Authors
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
7 // https://www.apache.org/licenses/LICENSE-2.0
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
17 #include "gtest/gtest.h"
18 #include "pw_chrono/system_clock.h"
19 #include "pw_thread/id.h"
20 #include "pw_thread/sleep.h"
22 using pw::chrono::SystemClock;
23 using namespace std::chrono_literals;
25 namespace pw::this_thread {
30 // Functions defined in sleep_facade_test_c.c which call the API from C.
31 void pw_this_thread_CallSleepFor(pw_chrono_SystemClock_Duration for_at_least);
32 void pw_this_thread_CallSleepUntil(
33 pw_chrono_SystemClock_TimePoint until_at_least);
37 // We can't control the SystemClock's period configuration, so just in case
38 // duration cannot be accurately expressed in integer ticks, round the
40 constexpr auto kRoundedArbitraryDuration =
41 std::chrono::ceil<SystemClock::duration>(42ms);
42 constexpr pw_chrono_SystemClock_Duration kRoundedArbitraryDurationInC =
43 PW_SYSTEM_CLOCK_MS(42);
45 TEST(Sleep, SleepFor) {
46 // Ensure we are in a thread context, meaning we are permitted to sleep.
47 ASSERT_NE(get_id(), thread::Id());
49 const SystemClock::time_point before = SystemClock::now();
50 sleep_for(kRoundedArbitraryDuration);
51 const SystemClock::duration time_elapsed = SystemClock::now() - before;
52 EXPECT_GE(time_elapsed, kRoundedArbitraryDuration);
55 TEST(Sleep, SleepUntil) {
56 // Ensure we are in a thread context, meaning we are permitted to sleep.
57 ASSERT_NE(get_id(), thread::Id());
59 const SystemClock::time_point deadline =
60 SystemClock::now() + kRoundedArbitraryDuration;
61 sleep_until(deadline);
62 EXPECT_GE(SystemClock::now(), deadline);
65 TEST(Sleep, SleepForInC) {
66 // Ensure we are in a thread context, meaning we are permitted to sleep.
67 ASSERT_NE(get_id(), thread::Id());
69 pw_chrono_SystemClock_TimePoint before = pw_chrono_SystemClock_Now();
70 pw_this_thread_SleepFor(kRoundedArbitraryDurationInC);
71 pw_chrono_SystemClock_Duration time_elapsed =
72 pw_chrono_SystemClock_TimeElapsed(before, pw_chrono_SystemClock_Now());
73 EXPECT_GE(time_elapsed.ticks, kRoundedArbitraryDurationInC.ticks);
76 TEST(Sleep, SleepUntilInC) {
77 // Ensure we are in a thread context, meaning we are permitted to sleep.
78 ASSERT_NE(get_id(), thread::Id());
80 pw_chrono_SystemClock_TimePoint deadline;
81 deadline.duration_since_epoch.ticks =
82 pw_chrono_SystemClock_Now().duration_since_epoch.ticks +
83 kRoundedArbitraryDurationInC.ticks;
84 pw_this_thread_CallSleepUntil(deadline);
85 EXPECT_GE(pw_chrono_SystemClock_Now().duration_since_epoch.ticks,
86 deadline.duration_since_epoch.ticks);
90 } // namespace pw::this_thread