Ewout van Bekkum | f010606 | 2021-05-06 14:08:33 -0700 | [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_sync/timed_thread_notification.h" |
| 20 | |
| 21 | using pw::chrono::SystemClock; |
| 22 | using namespace std::chrono_literals; |
| 23 | |
| 24 | namespace pw::sync { |
| 25 | namespace { |
| 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. |
| 30 | constexpr SystemClock::duration kRoundedArbitraryDuration = |
| 31 | SystemClock::for_at_least(42ms); |
| 32 | |
| 33 | TEST(TimedThreadNotification, EmptyInitialState) { |
| 34 | TimedThreadNotification notification; |
| 35 | EXPECT_FALSE(notification.try_acquire()); |
| 36 | } |
| 37 | |
| 38 | // TODO(pwbug/291): Add real concurrency tests. |
| 39 | |
| 40 | TEST(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 | |
| 49 | TimedThreadNotification empty_initial_notification; |
| 50 | TEST(TimedThreadNotification, EmptyInitialStateStatic) { |
| 51 | EXPECT_FALSE(empty_initial_notification.try_acquire()); |
| 52 | } |
| 53 | |
| 54 | TimedThreadNotification raise_notification; |
| 55 | TEST(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 Bekkum | 4b5ad7d | 2021-11-23 15:56:23 -0800 | [diff] [blame] | 63 | TEST(TimedThreadNotification, TryAcquireForNotified) { |
Ewout van Bekkum | f010606 | 2021-05-06 14:08:33 -0700 | [diff] [blame] | 64 | TimedThreadNotification notification; |
| 65 | notification.release(); |
| 66 | |
Ewout van Bekkum | 4b5ad7d | 2021-11-23 15:56:23 -0800 | [diff] [blame] | 67 | // Ensure it doesn't block and succeeds when notified. |
Ewout van Bekkum | f010606 | 2021-05-06 14:08:33 -0700 | [diff] [blame] | 68 | 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 Bekkum | 4b5ad7d | 2021-11-23 15:56:23 -0800 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | TEST(TimedThreadNotification, TryAcquireForNotNotifiedPositiveTimeout) { |
| 75 | TimedThreadNotification notification; |
Ewout van Bekkum | f010606 | 2021-05-06 14:08:33 -0700 | [diff] [blame] | 76 | |
Ewout van Bekkum | 0e1b11f | 2021-11-04 12:09:49 -0700 | [diff] [blame] | 77 | // Ensure it blocks and fails when not notified for the full timeout. |
Ewout van Bekkum | 4b5ad7d | 2021-11-23 15:56:23 -0800 | [diff] [blame] | 78 | SystemClock::time_point before = SystemClock::now(); |
Ewout van Bekkum | f010606 | 2021-05-06 14:08:33 -0700 | [diff] [blame] | 79 | EXPECT_FALSE(notification.try_acquire_for(kRoundedArbitraryDuration)); |
Ewout van Bekkum | 4b5ad7d | 2021-11-23 15:56:23 -0800 | [diff] [blame] | 80 | SystemClock::duration time_elapsed = SystemClock::now() - before; |
Ewout van Bekkum | f010606 | 2021-05-06 14:08:33 -0700 | [diff] [blame] | 81 | EXPECT_GE(time_elapsed, kRoundedArbitraryDuration); |
Ewout van Bekkum | 4b5ad7d | 2021-11-23 15:56:23 -0800 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | TEST(TimedThreadNotification, TryAcquireForNotNotifiedZeroLengthTimeout) { |
| 85 | TimedThreadNotification notification; |
Ewout van Bekkum | 0e1b11f | 2021-11-04 12:09:49 -0700 | [diff] [blame] | 86 | |
| 87 | // Ensure it doesn't block when a zero length duration is used. |
Ewout van Bekkum | 4b5ad7d | 2021-11-23 15:56:23 -0800 | [diff] [blame] | 88 | SystemClock::time_point before = SystemClock::now(); |
Ewout van Bekkum | 0e1b11f | 2021-11-04 12:09:49 -0700 | [diff] [blame] | 89 | EXPECT_FALSE(notification.try_acquire_for(SystemClock::duration::zero())); |
Ewout van Bekkum | 4b5ad7d | 2021-11-23 15:56:23 -0800 | [diff] [blame] | 90 | SystemClock::duration time_elapsed = SystemClock::now() - before; |
Ewout van Bekkum | 0e1b11f | 2021-11-04 12:09:49 -0700 | [diff] [blame] | 91 | EXPECT_LT(time_elapsed, kRoundedArbitraryDuration); |
Ewout van Bekkum | f010606 | 2021-05-06 14:08:33 -0700 | [diff] [blame] | 92 | } |
| 93 | |
Ewout van Bekkum | 4b5ad7d | 2021-11-23 15:56:23 -0800 | [diff] [blame] | 94 | TEST(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 | |
| 104 | TEST(TimedThreadNotification, TryAcquireUntilNotified) { |
Ewout van Bekkum | f010606 | 2021-05-06 14:08:33 -0700 | [diff] [blame] | 105 | TimedThreadNotification notification; |
| 106 | notification.release(); |
| 107 | |
Ewout van Bekkum | 4b5ad7d | 2021-11-23 15:56:23 -0800 | [diff] [blame] | 108 | // Ensure it doesn't block and succeeds when notified. |
Ewout van Bekkum | 0e1b11f | 2021-11-04 12:09:49 -0700 | [diff] [blame] | 109 | SystemClock::time_point deadline = |
Ewout van Bekkum | f010606 | 2021-05-06 14:08:33 -0700 | [diff] [blame] | 110 | SystemClock::now() + kRoundedArbitraryDuration; |
| 111 | EXPECT_TRUE(notification.try_acquire_until(deadline)); |
| 112 | EXPECT_LT(SystemClock::now(), deadline); |
Ewout van Bekkum | 4b5ad7d | 2021-11-23 15:56:23 -0800 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | TEST(TimedThreadNotification, TryAcquireUntilNotNotifiedFutureDeadline) { |
| 116 | TimedThreadNotification notification; |
Ewout van Bekkum | f010606 | 2021-05-06 14:08:33 -0700 | [diff] [blame] | 117 | |
| 118 | // Ensure it blocks and fails when not notified. |
Ewout van Bekkum | 4b5ad7d | 2021-11-23 15:56:23 -0800 | [diff] [blame] | 119 | SystemClock::time_point deadline = |
| 120 | SystemClock::now() + kRoundedArbitraryDuration; |
Ewout van Bekkum | f010606 | 2021-05-06 14:08:33 -0700 | [diff] [blame] | 121 | EXPECT_FALSE(notification.try_acquire_until(deadline)); |
| 122 | EXPECT_GE(SystemClock::now(), deadline); |
Ewout van Bekkum | 4b5ad7d | 2021-11-23 15:56:23 -0800 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | TEST(TimedThreadNotification, TryAcquireUntilNotNotifiedCurrentDeadline) { |
| 126 | TimedThreadNotification notification; |
Ewout van Bekkum | 0e1b11f | 2021-11-04 12:09:49 -0700 | [diff] [blame] | 127 | |
| 128 | // Ensure it doesn't block when now is used. |
Ewout van Bekkum | 4b5ad7d | 2021-11-23 15:56:23 -0800 | [diff] [blame] | 129 | SystemClock::time_point deadline = |
| 130 | SystemClock::now() + kRoundedArbitraryDuration; |
Ewout van Bekkum | 0e1b11f | 2021-11-04 12:09:49 -0700 | [diff] [blame] | 131 | EXPECT_FALSE(notification.try_acquire_until(SystemClock::now())); |
| 132 | EXPECT_LT(SystemClock::now(), deadline); |
Ewout van Bekkum | 4b5ad7d | 2021-11-23 15:56:23 -0800 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | TEST(TimedThreadNotification, TryAcquireUntilNotNotifiedPastDeadline) { |
| 136 | TimedThreadNotification notification; |
Ewout van Bekkum | 0e1b11f | 2021-11-04 12:09:49 -0700 | [diff] [blame] | 137 | |
| 138 | // Ensure it doesn't block when a timestamp in the past is used. |
Ewout van Bekkum | 4b5ad7d | 2021-11-23 15:56:23 -0800 | [diff] [blame] | 139 | SystemClock::time_point deadline = |
| 140 | SystemClock::now() + kRoundedArbitraryDuration; |
Ewout van Bekkum | 0e1b11f | 2021-11-04 12:09:49 -0700 | [diff] [blame] | 141 | EXPECT_FALSE(notification.try_acquire_until(SystemClock::now() - |
| 142 | kRoundedArbitraryDuration)); |
| 143 | EXPECT_LT(SystemClock::now(), deadline); |
Ewout van Bekkum | f010606 | 2021-05-06 14:08:33 -0700 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | } // namespace |
| 147 | } // namespace pw::sync |