Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / third_party / pigweed / repo / pw_thread / sleep_facade_test.cc
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 using namespace std::chrono_literals;
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.
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);
34
35 }  // extern "C"
36
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/ ceil.
40 constexpr auto kRoundedArbitraryDuration =
41     std::chrono::ceil<SystemClock::duration>(42ms);
42 constexpr pw_chrono_SystemClock_Duration kRoundedArbitraryDurationInC =
43     PW_SYSTEM_CLOCK_MS(42);
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_Duration time_elapsed =
72       pw_chrono_SystemClock_TimeElapsed(before, pw_chrono_SystemClock_Now());
73   EXPECT_GE(time_elapsed.ticks, kRoundedArbitraryDurationInC.ticks);
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.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);
87 }
88
89 }  // namespace
90 }  // namespace pw::this_thread