blob: 84bb415e3c6f2dff3f436ea1ac669c9197e53306 [file] [log] [blame]
Ewout van Bekkumf0106062021-05-06 14:08:33 -07001// 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_sync/timed_thread_notification.h"
20
21using pw::chrono::SystemClock;
22using namespace std::chrono_literals;
23
24namespace pw::sync {
25namespace {
26
27// We can't control the SystemClock's period configuration, so just in case
28// duration cannot be accurately expressed in integer ticks, round the
29// duration up.
30constexpr SystemClock::duration kRoundedArbitraryDuration =
31 SystemClock::for_at_least(42ms);
32
33TEST(TimedThreadNotification, EmptyInitialState) {
34 TimedThreadNotification notification;
35 EXPECT_FALSE(notification.try_acquire());
36}
37
38// TODO(pwbug/291): Add real concurrency tests.
39
40TEST(TimedThreadNotification, Release) {
41 TimedThreadNotification notification;
42 notification.release();
43 notification.release();
44 notification.acquire();
45 // Ensure it fails when not notified.
46 EXPECT_FALSE(notification.try_acquire());
47}
48
49TimedThreadNotification empty_initial_notification;
50TEST(TimedThreadNotification, EmptyInitialStateStatic) {
51 EXPECT_FALSE(empty_initial_notification.try_acquire());
52}
53
54TimedThreadNotification raise_notification;
55TEST(TimedThreadNotification, ReleaseStatic) {
56 raise_notification.release();
57 raise_notification.release();
58 raise_notification.acquire();
59 // Ensure it fails when not notified.
60 EXPECT_FALSE(raise_notification.try_acquire());
61}
62
Ewout van Bekkum4b5ad7d2021-11-23 15:56:23 -080063TEST(TimedThreadNotification, TryAcquireForNotified) {
Ewout van Bekkumf0106062021-05-06 14:08:33 -070064 TimedThreadNotification notification;
65 notification.release();
66
Ewout van Bekkum4b5ad7d2021-11-23 15:56:23 -080067 // Ensure it doesn't block and succeeds when notified.
Ewout van Bekkumf0106062021-05-06 14:08:33 -070068 SystemClock::time_point before = SystemClock::now();
69 EXPECT_TRUE(notification.try_acquire_for(kRoundedArbitraryDuration));
70 SystemClock::duration time_elapsed = SystemClock::now() - before;
71 EXPECT_LT(time_elapsed, kRoundedArbitraryDuration);
Ewout van Bekkum4b5ad7d2021-11-23 15:56:23 -080072}
73
74TEST(TimedThreadNotification, TryAcquireForNotNotifiedPositiveTimeout) {
75 TimedThreadNotification notification;
Ewout van Bekkumf0106062021-05-06 14:08:33 -070076
Ewout van Bekkum0e1b11f2021-11-04 12:09:49 -070077 // Ensure it blocks and fails when not notified for the full timeout.
Ewout van Bekkum4b5ad7d2021-11-23 15:56:23 -080078 SystemClock::time_point before = SystemClock::now();
Ewout van Bekkumf0106062021-05-06 14:08:33 -070079 EXPECT_FALSE(notification.try_acquire_for(kRoundedArbitraryDuration));
Ewout van Bekkum4b5ad7d2021-11-23 15:56:23 -080080 SystemClock::duration time_elapsed = SystemClock::now() - before;
Ewout van Bekkumf0106062021-05-06 14:08:33 -070081 EXPECT_GE(time_elapsed, kRoundedArbitraryDuration);
Ewout van Bekkum4b5ad7d2021-11-23 15:56:23 -080082}
83
84TEST(TimedThreadNotification, TryAcquireForNotNotifiedZeroLengthTimeout) {
85 TimedThreadNotification notification;
Ewout van Bekkum0e1b11f2021-11-04 12:09:49 -070086
87 // Ensure it doesn't block when a zero length duration is used.
Ewout van Bekkum4b5ad7d2021-11-23 15:56:23 -080088 SystemClock::time_point before = SystemClock::now();
Ewout van Bekkum0e1b11f2021-11-04 12:09:49 -070089 EXPECT_FALSE(notification.try_acquire_for(SystemClock::duration::zero()));
Ewout van Bekkum4b5ad7d2021-11-23 15:56:23 -080090 SystemClock::duration time_elapsed = SystemClock::now() - before;
Ewout van Bekkum0e1b11f2021-11-04 12:09:49 -070091 EXPECT_LT(time_elapsed, kRoundedArbitraryDuration);
Ewout van Bekkumf0106062021-05-06 14:08:33 -070092}
93
Ewout van Bekkum4b5ad7d2021-11-23 15:56:23 -080094TEST(TimedThreadNotification, TryAcquireForNotNotifiedNegativeTimeout) {
95 TimedThreadNotification notification;
96
97 // Ensure it doesn't block when a negative duration is used.
98 SystemClock::time_point before = SystemClock::now();
99 EXPECT_FALSE(notification.try_acquire_for(-kRoundedArbitraryDuration));
100 SystemClock::duration time_elapsed = SystemClock::now() - before;
101 EXPECT_LT(time_elapsed, kRoundedArbitraryDuration);
102}
103
104TEST(TimedThreadNotification, TryAcquireUntilNotified) {
Ewout van Bekkumf0106062021-05-06 14:08:33 -0700105 TimedThreadNotification notification;
106 notification.release();
107
Ewout van Bekkum4b5ad7d2021-11-23 15:56:23 -0800108 // Ensure it doesn't block and succeeds when notified.
Ewout van Bekkum0e1b11f2021-11-04 12:09:49 -0700109 SystemClock::time_point deadline =
Ewout van Bekkumf0106062021-05-06 14:08:33 -0700110 SystemClock::now() + kRoundedArbitraryDuration;
111 EXPECT_TRUE(notification.try_acquire_until(deadline));
112 EXPECT_LT(SystemClock::now(), deadline);
Ewout van Bekkum4b5ad7d2021-11-23 15:56:23 -0800113}
114
115TEST(TimedThreadNotification, TryAcquireUntilNotNotifiedFutureDeadline) {
116 TimedThreadNotification notification;
Ewout van Bekkumf0106062021-05-06 14:08:33 -0700117
118 // Ensure it blocks and fails when not notified.
Ewout van Bekkum4b5ad7d2021-11-23 15:56:23 -0800119 SystemClock::time_point deadline =
120 SystemClock::now() + kRoundedArbitraryDuration;
Ewout van Bekkumf0106062021-05-06 14:08:33 -0700121 EXPECT_FALSE(notification.try_acquire_until(deadline));
122 EXPECT_GE(SystemClock::now(), deadline);
Ewout van Bekkum4b5ad7d2021-11-23 15:56:23 -0800123}
124
125TEST(TimedThreadNotification, TryAcquireUntilNotNotifiedCurrentDeadline) {
126 TimedThreadNotification notification;
Ewout van Bekkum0e1b11f2021-11-04 12:09:49 -0700127
128 // Ensure it doesn't block when now is used.
Ewout van Bekkum4b5ad7d2021-11-23 15:56:23 -0800129 SystemClock::time_point deadline =
130 SystemClock::now() + kRoundedArbitraryDuration;
Ewout van Bekkum0e1b11f2021-11-04 12:09:49 -0700131 EXPECT_FALSE(notification.try_acquire_until(SystemClock::now()));
132 EXPECT_LT(SystemClock::now(), deadline);
Ewout van Bekkum4b5ad7d2021-11-23 15:56:23 -0800133}
134
135TEST(TimedThreadNotification, TryAcquireUntilNotNotifiedPastDeadline) {
136 TimedThreadNotification notification;
Ewout van Bekkum0e1b11f2021-11-04 12:09:49 -0700137
138 // Ensure it doesn't block when a timestamp in the past is used.
Ewout van Bekkum4b5ad7d2021-11-23 15:56:23 -0800139 SystemClock::time_point deadline =
140 SystemClock::now() + kRoundedArbitraryDuration;
Ewout van Bekkum0e1b11f2021-11-04 12:09:49 -0700141 EXPECT_FALSE(notification.try_acquire_until(SystemClock::now() -
142 kRoundedArbitraryDuration));
143 EXPECT_LT(SystemClock::now(), deadline);
Ewout van Bekkumf0106062021-05-06 14:08:33 -0700144}
145
146} // namespace
147} // namespace pw::sync