blob: 3112b630c71bd78efa7b0a2b6a0abcec0b5c9774 [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 Bekkum830d5d12021-01-28 18:58:03 -080031void pw_this_thread_CallSleepFor(pw_chrono_SystemClock_Duration for_at_least);
Ewout van Bekkume3b56032020-12-22 12:00:18 -080032void pw_this_thread_CallSleepUntil(
33 pw_chrono_SystemClock_TimePoint until_at_least);
34
35} // extern "C"
36
Ewout van Bekkume3b56032020-12-22 12:00:18 -080037// We can't control the SystemClock's period configuration, so just in case
38// duration cannot be accurately expressed in integer ticks, round the
Ewout van Bekkum959b8832021-03-11 08:55:01 -080039// duration up.
40constexpr SystemClock::duration kRoundedArbitraryDuration =
41 SystemClock::for_at_least(42ms);
Ewout van Bekkum830d5d12021-01-28 18:58:03 -080042constexpr pw_chrono_SystemClock_Duration kRoundedArbitraryDurationInC =
43 PW_SYSTEM_CLOCK_MS(42);
Ewout van Bekkume3b56032020-12-22 12:00:18 -080044
45TEST(Sleep, SleepFor) {
46 // Ensure we are in a thread context, meaning we are permitted to sleep.
47 ASSERT_NE(get_id(), thread::Id());
48
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);
53}
54
55TEST(Sleep, SleepUntil) {
56 // Ensure we are in a thread context, meaning we are permitted to sleep.
57 ASSERT_NE(get_id(), thread::Id());
58
59 const SystemClock::time_point deadline =
60 SystemClock::now() + kRoundedArbitraryDuration;
61 sleep_until(deadline);
62 EXPECT_GE(SystemClock::now(), deadline);
63}
64
65TEST(Sleep, SleepForInC) {
66 // Ensure we are in a thread context, meaning we are permitted to sleep.
67 ASSERT_NE(get_id(), thread::Id());
68
69 pw_chrono_SystemClock_TimePoint before = pw_chrono_SystemClock_Now();
70 pw_this_thread_SleepFor(kRoundedArbitraryDurationInC);
Ewout van Bekkum830d5d12021-01-28 18:58:03 -080071 pw_chrono_SystemClock_Duration time_elapsed =
72 pw_chrono_SystemClock_TimeElapsed(before, pw_chrono_SystemClock_Now());
73 EXPECT_GE(time_elapsed.ticks, kRoundedArbitraryDurationInC.ticks);
Ewout van Bekkume3b56032020-12-22 12:00:18 -080074}
75
76TEST(Sleep, SleepUntilInC) {
77 // Ensure we are in a thread context, meaning we are permitted to sleep.
78 ASSERT_NE(get_id(), thread::Id());
79
80 pw_chrono_SystemClock_TimePoint deadline;
Ewout van Bekkum830d5d12021-01-28 18:58:03 -080081 deadline.duration_since_epoch.ticks =
82 pw_chrono_SystemClock_Now().duration_since_epoch.ticks +
83 kRoundedArbitraryDurationInC.ticks;
Ewout van Bekkume3b56032020-12-22 12:00:18 -080084 pw_this_thread_CallSleepUntil(deadline);
Ewout van Bekkum830d5d12021-01-28 18:58:03 -080085 EXPECT_GE(pw_chrono_SystemClock_Now().duration_since_epoch.ticks,
86 deadline.duration_since_epoch.ticks);
Ewout van Bekkume3b56032020-12-22 12:00:18 -080087}
88
89} // namespace
90} // namespace pw::this_thread