blob: ca838808608df34513c4a6c8eb832bcb885770ca [file] [log] [blame]
Ewout van Bekkume3b56032020-12-22 12:00:18 -08001// Copyright 2020 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 <chrono>
16
17#include "gtest/gtest.h"
18#include "pw_chrono/system_clock.h"
19#include "pw_thread/id.h"
20#include "pw_thread/sleep.h"
21
22using pw::chrono::SystemClock;
Ewout van Bekkum830d5d12021-01-28 18:58:03 -080023using namespace std::chrono_literals;
Ewout van Bekkume3b56032020-12-22 12:00:18 -080024
25namespace pw::this_thread {
26namespace {
27
28extern "C" {
29
30// Functions defined in sleep_facade_test_c.c which call the API from C.
Ewout van Bekkum39144b62021-09-07 16:05:35 -070031void pw_this_thread_CallSleepFor(pw_chrono_SystemClock_Duration sleep_duration);
32void pw_this_thread_CallSleepUntil(pw_chrono_SystemClock_TimePoint wakeup_time);
Ewout van Bekkume3b56032020-12-22 12:00:18 -080033
34} // extern "C"
35
Ewout van Bekkume3b56032020-12-22 12:00:18 -080036// We can't control the SystemClock's period configuration, so just in case
37// duration cannot be accurately expressed in integer ticks, round the
Ewout van Bekkum959b8832021-03-11 08:55:01 -080038// duration up.
39constexpr SystemClock::duration kRoundedArbitraryDuration =
40 SystemClock::for_at_least(42ms);
Ewout van Bekkum830d5d12021-01-28 18:58:03 -080041constexpr pw_chrono_SystemClock_Duration kRoundedArbitraryDurationInC =
42 PW_SYSTEM_CLOCK_MS(42);
Ewout van Bekkume3b56032020-12-22 12:00:18 -080043
Ewout van Bekkum4b5ad7d2021-11-23 15:56:23 -080044TEST(Sleep, SleepForPositiveDuration) {
Ewout van Bekkume3b56032020-12-22 12:00:18 -080045 // Ensure we are in a thread context, meaning we are permitted to sleep.
46 ASSERT_NE(get_id(), thread::Id());
47
Ewout van Bekkumb34a1542021-11-05 10:42:17 -070048 SystemClock::time_point before = SystemClock::now();
Ewout van Bekkume3b56032020-12-22 12:00:18 -080049 sleep_for(kRoundedArbitraryDuration);
Ewout van Bekkumb34a1542021-11-05 10:42:17 -070050 SystemClock::duration time_elapsed = SystemClock::now() - before;
Ewout van Bekkume3b56032020-12-22 12:00:18 -080051 EXPECT_GE(time_elapsed, kRoundedArbitraryDuration);
Ewout van Bekkum4b5ad7d2021-11-23 15:56:23 -080052}
53
54TEST(Sleep, SleepForZeroLengthDuration) {
55 // Ensure we are in a thread context, meaning we are permitted to sleep.
56 ASSERT_NE(get_id(), thread::Id());
Ewout van Bekkumb34a1542021-11-05 10:42:17 -070057
58 // Ensure it doesn't sleep when a zero length duration is used.
Ewout van Bekkum4b5ad7d2021-11-23 15:56:23 -080059 SystemClock::time_point before = SystemClock::now();
Ewout van Bekkumb34a1542021-11-05 10:42:17 -070060 sleep_for(SystemClock::duration::zero());
Ewout van Bekkum4b5ad7d2021-11-23 15:56:23 -080061 SystemClock::duration time_elapsed = SystemClock::now() - before;
Ewout van Bekkumb34a1542021-11-05 10:42:17 -070062 EXPECT_LT(time_elapsed, kRoundedArbitraryDuration);
Ewout van Bekkume3b56032020-12-22 12:00:18 -080063}
64
Ewout van Bekkum4b5ad7d2021-11-23 15:56:23 -080065TEST(Sleep, SleepForNegativeDuration) {
66 // Ensure we are in a thread context, meaning we are permitted to sleep.
67 ASSERT_NE(get_id(), thread::Id());
68
69 // Ensure it doesn't sleep when a negative duration is used.
70 SystemClock::time_point before = SystemClock::now();
71 sleep_for(-kRoundedArbitraryDuration);
72 SystemClock::duration time_elapsed = SystemClock::now() - before;
73 EXPECT_LT(time_elapsed, kRoundedArbitraryDuration);
74}
75
76TEST(Sleep, SleepUntilFutureWakeupTime) {
Ewout van Bekkume3b56032020-12-22 12:00:18 -080077 // Ensure we are in a thread context, meaning we are permitted to sleep.
78 ASSERT_NE(get_id(), thread::Id());
79
Ewout van Bekkumb34a1542021-11-05 10:42:17 -070080 SystemClock::time_point deadline =
Ewout van Bekkume3b56032020-12-22 12:00:18 -080081 SystemClock::now() + kRoundedArbitraryDuration;
82 sleep_until(deadline);
83 EXPECT_GE(SystemClock::now(), deadline);
Ewout van Bekkum4b5ad7d2021-11-23 15:56:23 -080084}
85
86TEST(Sleep, SleepUntilCurrentWakeupTime) {
87 // Ensure we are in a thread context, meaning we are permitted to sleep.
88 ASSERT_NE(get_id(), thread::Id());
Ewout van Bekkumb34a1542021-11-05 10:42:17 -070089
90 // Ensure it doesn't sleep when now is used.
Ewout van Bekkum4b5ad7d2021-11-23 15:56:23 -080091 SystemClock::time_point deadline =
92 SystemClock::now() + kRoundedArbitraryDuration;
Ewout van Bekkumb34a1542021-11-05 10:42:17 -070093 sleep_until(SystemClock::now());
94 EXPECT_LT(SystemClock::now(), deadline);
Ewout van Bekkum4b5ad7d2021-11-23 15:56:23 -080095}
96
97TEST(Sleep, SleepUntilPastWakeupTime) {
98 // Ensure we are in a thread context, meaning we are permitted to sleep.
99 ASSERT_NE(get_id(), thread::Id());
Ewout van Bekkumb34a1542021-11-05 10:42:17 -0700100
101 // Ensure it doesn't sleep when a timestamp in the past is used.
Ewout van Bekkum4b5ad7d2021-11-23 15:56:23 -0800102 SystemClock::time_point deadline =
103 SystemClock::now() + kRoundedArbitraryDuration;
Ewout van Bekkumb34a1542021-11-05 10:42:17 -0700104 sleep_until(SystemClock::now() - kRoundedArbitraryDuration);
105 EXPECT_LT(SystemClock::now(), deadline);
Ewout van Bekkume3b56032020-12-22 12:00:18 -0800106}
107
Ewout van Bekkum4b5ad7d2021-11-23 15:56:23 -0800108TEST(Sleep, SleepForPositiveDurationInC) {
Ewout van Bekkume3b56032020-12-22 12:00:18 -0800109 // Ensure we are in a thread context, meaning we are permitted to sleep.
110 ASSERT_NE(get_id(), thread::Id());
111
112 pw_chrono_SystemClock_TimePoint before = pw_chrono_SystemClock_Now();
113 pw_this_thread_SleepFor(kRoundedArbitraryDurationInC);
Ewout van Bekkum830d5d12021-01-28 18:58:03 -0800114 pw_chrono_SystemClock_Duration time_elapsed =
115 pw_chrono_SystemClock_TimeElapsed(before, pw_chrono_SystemClock_Now());
116 EXPECT_GE(time_elapsed.ticks, kRoundedArbitraryDurationInC.ticks);
Ewout van Bekkum4b5ad7d2021-11-23 15:56:23 -0800117}
118
119TEST(Sleep, SleepForZeroLengthDurationInC) {
120 // Ensure we are in a thread context, meaning we are permitted to sleep.
121 ASSERT_NE(get_id(), thread::Id());
Ewout van Bekkumb34a1542021-11-05 10:42:17 -0700122
123 // Ensure it doesn't sleep when a zero length duration is used.
Ewout van Bekkum4b5ad7d2021-11-23 15:56:23 -0800124 pw_chrono_SystemClock_TimePoint before = pw_chrono_SystemClock_Now();
Ewout van Bekkumb34a1542021-11-05 10:42:17 -0700125 pw_this_thread_SleepFor(PW_SYSTEM_CLOCK_MS(0));
Ewout van Bekkum4b5ad7d2021-11-23 15:56:23 -0800126 pw_chrono_SystemClock_Duration time_elapsed =
Ewout van Bekkumb34a1542021-11-05 10:42:17 -0700127 pw_chrono_SystemClock_TimeElapsed(before, pw_chrono_SystemClock_Now());
128 EXPECT_LT(time_elapsed.ticks, kRoundedArbitraryDurationInC.ticks);
Ewout van Bekkume3b56032020-12-22 12:00:18 -0800129}
130
Ewout van Bekkum4b5ad7d2021-11-23 15:56:23 -0800131TEST(Sleep, SleepForNegativeDurationInC) {
132 // Ensure we are in a thread context, meaning we are permitted to sleep.
133 ASSERT_NE(get_id(), thread::Id());
134
135 // Ensure it doesn't sleep when a negative duration is used.
136 pw_chrono_SystemClock_TimePoint before = pw_chrono_SystemClock_Now();
137 pw_this_thread_SleepFor(
138 PW_SYSTEM_CLOCK_MS(-kRoundedArbitraryDurationInC.ticks));
139 pw_chrono_SystemClock_Duration time_elapsed =
140 pw_chrono_SystemClock_TimeElapsed(before, pw_chrono_SystemClock_Now());
141 EXPECT_LT(time_elapsed.ticks, kRoundedArbitraryDurationInC.ticks);
142}
143
144TEST(Sleep, SleepUntilFutureWakeupTimeInC) {
Ewout van Bekkume3b56032020-12-22 12:00:18 -0800145 // Ensure we are in a thread context, meaning we are permitted to sleep.
146 ASSERT_NE(get_id(), thread::Id());
147
148 pw_chrono_SystemClock_TimePoint deadline;
Ewout van Bekkum830d5d12021-01-28 18:58:03 -0800149 deadline.duration_since_epoch.ticks =
150 pw_chrono_SystemClock_Now().duration_since_epoch.ticks +
151 kRoundedArbitraryDurationInC.ticks;
Ewout van Bekkume3b56032020-12-22 12:00:18 -0800152 pw_this_thread_CallSleepUntil(deadline);
Ewout van Bekkum830d5d12021-01-28 18:58:03 -0800153 EXPECT_GE(pw_chrono_SystemClock_Now().duration_since_epoch.ticks,
154 deadline.duration_since_epoch.ticks);
Ewout van Bekkum4b5ad7d2021-11-23 15:56:23 -0800155}
156
157TEST(Sleep, SleepUntilCurrentWakeupTimeInC) {
158 // Ensure we are in a thread context, meaning we are permitted to sleep.
159 ASSERT_NE(get_id(), thread::Id());
Ewout van Bekkumb34a1542021-11-05 10:42:17 -0700160
161 // Ensure it doesn't sleep when now is used.
Ewout van Bekkum4b5ad7d2021-11-23 15:56:23 -0800162 pw_chrono_SystemClock_TimePoint deadline;
Ewout van Bekkumb34a1542021-11-05 10:42:17 -0700163 deadline.duration_since_epoch.ticks =
164 pw_chrono_SystemClock_Now().duration_since_epoch.ticks +
165 kRoundedArbitraryDurationInC.ticks;
166 pw_this_thread_CallSleepUntil(pw_chrono_SystemClock_Now());
167 EXPECT_LT(pw_chrono_SystemClock_Now().duration_since_epoch.ticks,
168 deadline.duration_since_epoch.ticks);
Ewout van Bekkum4b5ad7d2021-11-23 15:56:23 -0800169}
170
171TEST(Sleep, SleepUntilPastWakeupTimeInC) {
172 // Ensure we are in a thread context, meaning we are permitted to sleep.
173 ASSERT_NE(get_id(), thread::Id());
Ewout van Bekkumb34a1542021-11-05 10:42:17 -0700174
175 // Ensure it doesn't sleep when a timestamp in the past is used.
Ewout van Bekkum4b5ad7d2021-11-23 15:56:23 -0800176 pw_chrono_SystemClock_TimePoint deadline;
Ewout van Bekkumb34a1542021-11-05 10:42:17 -0700177 deadline.duration_since_epoch.ticks =
178 pw_chrono_SystemClock_Now().duration_since_epoch.ticks +
179 kRoundedArbitraryDurationInC.ticks;
180 pw_chrono_SystemClock_TimePoint old_timestamp;
181 old_timestamp.duration_since_epoch.ticks =
182 pw_chrono_SystemClock_Now().duration_since_epoch.ticks -
183 kRoundedArbitraryDurationInC.ticks;
184 pw_this_thread_CallSleepUntil(old_timestamp);
185 EXPECT_LT(pw_chrono_SystemClock_Now().duration_since_epoch.ticks,
186 deadline.duration_since_epoch.ticks);
Ewout van Bekkume3b56032020-12-22 12:00:18 -0800187}
188
189} // namespace
190} // namespace pw::this_thread