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; |
| 23 | |
| 24 | namespace pw::this_thread { |
| 25 | namespace { |
| 26 | |
| 27 | extern "C" { |
| 28 | |
| 29 | // Functions defined in sleep_facade_test_c.c which call the API from C. |
| 30 | void pw_this_thread_CallSleepFor(pw_chrono_SystemClock_TickCount for_at_least); |
| 31 | void pw_this_thread_CallSleepUntil( |
| 32 | pw_chrono_SystemClock_TimePoint until_at_least); |
| 33 | |
| 34 | } // extern "C" |
| 35 | |
| 36 | static constexpr auto kArbitraryDuration = std::chrono::milliseconds(42); |
| 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 |
| 39 | // duration w/ duration_cast. |
| 40 | static constexpr auto kRoundedArbitraryDuration = |
| 41 | std::chrono::duration_cast<SystemClock::duration>(kArbitraryDuration); |
| 42 | static constexpr pw_chrono_SystemClock_TickCount kRoundedArbitraryDurationInC = |
| 43 | kRoundedArbitraryDuration.count(); |
| 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); |
| 71 | pw_chrono_SystemClock_TickCount time_elapsed = |
| 72 | pw_chrono_SystemClock_Now().ticks_since_epoch - before.ticks_since_epoch; |
| 73 | EXPECT_GE(time_elapsed, kRoundedArbitraryDurationInC); |
| 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; |
| 81 | deadline.ticks_since_epoch = pw_chrono_SystemClock_Now().ticks_since_epoch + |
| 82 | kRoundedArbitraryDurationInC; |
| 83 | pw_this_thread_CallSleepUntil(deadline); |
| 84 | EXPECT_GE(pw_chrono_SystemClock_Now().ticks_since_epoch, |
| 85 | deadline.ticks_since_epoch); |
| 86 | } |
| 87 | |
| 88 | } // namespace |
| 89 | } // namespace pw::this_thread |