Ewout van Bekkum | e3b5603 | 2020-12-22 12:00:18 -0800 | [diff] [blame] | 1 | // 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 | |
| 22 | using pw::chrono::SystemClock; |
Ewout van Bekkum | 830d5d1 | 2021-01-28 18:58:03 -0800 | [diff] [blame] | 23 | using namespace std::chrono_literals; |
Ewout van Bekkum | e3b5603 | 2020-12-22 12:00:18 -0800 | [diff] [blame] | 24 | |
| 25 | namespace pw::this_thread { |
| 26 | namespace { |
| 27 | |
| 28 | extern "C" { |
| 29 | |
| 30 | // Functions defined in sleep_facade_test_c.c which call the API from C. |
Ewout van Bekkum | 830d5d1 | 2021-01-28 18:58:03 -0800 | [diff] [blame] | 31 | void pw_this_thread_CallSleepFor(pw_chrono_SystemClock_Duration for_at_least); |
Ewout van Bekkum | e3b5603 | 2020-12-22 12:00:18 -0800 | [diff] [blame] | 32 | void pw_this_thread_CallSleepUntil( |
| 33 | pw_chrono_SystemClock_TimePoint until_at_least); |
| 34 | |
| 35 | } // extern "C" |
| 36 | |
Ewout van Bekkum | e3b5603 | 2020-12-22 12:00:18 -0800 | [diff] [blame] | 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 |
Ewout van Bekkum | 959b883 | 2021-03-11 08:55:01 -0800 | [diff] [blame] | 39 | // duration up. |
| 40 | constexpr SystemClock::duration kRoundedArbitraryDuration = |
| 41 | SystemClock::for_at_least(42ms); |
Ewout van Bekkum | 830d5d1 | 2021-01-28 18:58:03 -0800 | [diff] [blame] | 42 | constexpr pw_chrono_SystemClock_Duration kRoundedArbitraryDurationInC = |
| 43 | PW_SYSTEM_CLOCK_MS(42); |
Ewout van Bekkum | e3b5603 | 2020-12-22 12:00:18 -0800 | [diff] [blame] | 44 | |
| 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()); |
| 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 | |
| 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()); |
| 58 | |
| 59 | const SystemClock::time_point deadline = |
| 60 | SystemClock::now() + kRoundedArbitraryDuration; |
| 61 | sleep_until(deadline); |
| 62 | EXPECT_GE(SystemClock::now(), deadline); |
| 63 | } |
| 64 | |
| 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()); |
| 68 | |
| 69 | pw_chrono_SystemClock_TimePoint before = pw_chrono_SystemClock_Now(); |
| 70 | pw_this_thread_SleepFor(kRoundedArbitraryDurationInC); |
Ewout van Bekkum | 830d5d1 | 2021-01-28 18:58:03 -0800 | [diff] [blame] | 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); |
Ewout van Bekkum | e3b5603 | 2020-12-22 12:00:18 -0800 | [diff] [blame] | 74 | } |
| 75 | |
| 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()); |
| 79 | |
| 80 | pw_chrono_SystemClock_TimePoint deadline; |
Ewout van Bekkum | 830d5d1 | 2021-01-28 18:58:03 -0800 | [diff] [blame] | 81 | deadline.duration_since_epoch.ticks = |
| 82 | pw_chrono_SystemClock_Now().duration_since_epoch.ticks + |
| 83 | kRoundedArbitraryDurationInC.ticks; |
Ewout van Bekkum | e3b5603 | 2020-12-22 12:00:18 -0800 | [diff] [blame] | 84 | pw_this_thread_CallSleepUntil(deadline); |
Ewout van Bekkum | 830d5d1 | 2021-01-28 18:58:03 -0800 | [diff] [blame] | 85 | EXPECT_GE(pw_chrono_SystemClock_Now().duration_since_epoch.ticks, |
| 86 | deadline.duration_since_epoch.ticks); |
Ewout van Bekkum | e3b5603 | 2020-12-22 12:00:18 -0800 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | } // namespace |
| 90 | } // namespace pw::this_thread |