pw_{sync,thread}: Split up facade unit test cases
Splits up large unit test cases into individual tests.
Change-Id: I699b7b36967a3ec5114c14069661928f49aa81cd
Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/70533
Reviewed-by: Wyatt Hepler <hepler@google.com>
Commit-Queue: Ewout van Bekkum <ewout@google.com>
Pigweed-Auto-Submit: Ewout van Bekkum <ewout@google.com>
diff --git a/pw_sync/binary_semaphore_facade_test.cc b/pw_sync/binary_semaphore_facade_test.cc
index be06250..7f9ce30 100644
--- a/pw_sync/binary_semaphore_facade_test.cc
+++ b/pw_sync/binary_semaphore_facade_test.cc
@@ -78,58 +78,87 @@
EXPECT_FALSE(release_semaphore.try_acquire());
}
-TEST(BinarySemaphore, TryAcquireFor) {
+TEST(BinarySemaphore, TryAcquireForFull) {
BinarySemaphore semaphore;
semaphore.release();
+ // Ensure it doesn't block and succeeds if not empty.
SystemClock::time_point before = SystemClock::now();
EXPECT_TRUE(semaphore.try_acquire_for(kRoundedArbitraryDuration));
SystemClock::duration time_elapsed = SystemClock::now() - before;
EXPECT_LT(time_elapsed, kRoundedArbitraryDuration);
+}
+
+TEST(BinarySemaphore, TryAcquireForEmptyPositiveTimeout) {
+ BinarySemaphore semaphore;
// Ensure it blocks and fails when empty.
- before = SystemClock::now();
+ SystemClock::time_point before = SystemClock::now();
EXPECT_FALSE(semaphore.try_acquire_for(kRoundedArbitraryDuration));
- time_elapsed = SystemClock::now() - before;
+ SystemClock::duration time_elapsed = SystemClock::now() - before;
EXPECT_GE(time_elapsed, kRoundedArbitraryDuration);
+}
+
+TEST(BinarySemaphore, TryAcquireForEmptyZeroLengthTimeout) {
+ BinarySemaphore semaphore;
// Ensure it doesn't block and fails when empty and a zero length duration is
// used.
- before = SystemClock::now();
+ SystemClock::time_point before = SystemClock::now();
EXPECT_FALSE(semaphore.try_acquire_for(SystemClock::duration::zero()));
- time_elapsed = SystemClock::now() - before;
- EXPECT_LT(time_elapsed, kRoundedArbitraryDuration);
-
- // Ensure it doesn't block and fails when empty and a negative duration is
- // used.
- before = SystemClock::now();
- EXPECT_FALSE(semaphore.try_acquire_for(-kRoundedArbitraryDuration));
- time_elapsed = SystemClock::now() - before;
+ SystemClock::duration time_elapsed = SystemClock::now() - before;
EXPECT_LT(time_elapsed, kRoundedArbitraryDuration);
}
-TEST(BinarySemaphore, TryAcquireUntil) {
+TEST(BinarySemaphore, TryAcquireForEmptyNegativeTimeout) {
+ BinarySemaphore semaphore;
+
+ // Ensure it doesn't block and fails when empty and a negative duration is
+ // used.
+ SystemClock::time_point before = SystemClock::now();
+ EXPECT_FALSE(semaphore.try_acquire_for(-kRoundedArbitraryDuration));
+ SystemClock::duration time_elapsed = SystemClock::now() - before;
+ EXPECT_LT(time_elapsed, kRoundedArbitraryDuration);
+}
+
+TEST(BinarySemaphore, TryAcquireUntilFull) {
BinarySemaphore semaphore;
semaphore.release();
+ // Ensure it doesn't block and succeeds if not empty.
SystemClock::time_point deadline =
SystemClock::now() + kRoundedArbitraryDuration;
EXPECT_TRUE(semaphore.try_acquire_until(deadline));
EXPECT_LT(SystemClock::now(), deadline);
+}
+
+TEST(BinarySemaphore, TryAcquireUntilEmptyFutureDeadline) {
+ BinarySemaphore semaphore;
// Ensure it blocks and fails when empty.
- deadline = SystemClock::now() + kRoundedArbitraryDuration;
+ SystemClock::time_point deadline =
+ SystemClock::now() + kRoundedArbitraryDuration;
EXPECT_FALSE(semaphore.try_acquire_until(deadline));
EXPECT_GE(SystemClock::now(), deadline);
+}
+
+TEST(BinarySemaphore, TryAcquireUntilEmptyCurrentDeadline) {
+ BinarySemaphore semaphore;
// Ensure it doesn't block and fails when empty and now is used.
- deadline = SystemClock::now() + kRoundedArbitraryDuration;
+ SystemClock::time_point deadline =
+ SystemClock::now() + kRoundedArbitraryDuration;
EXPECT_FALSE(semaphore.try_acquire_until(SystemClock::now()));
EXPECT_LT(SystemClock::now(), deadline);
+}
+
+TEST(BinarySemaphore, TryAcquireUntilEmptyPastDeadline) {
+ BinarySemaphore semaphore;
// Ensure it doesn't block and fails when empty and a timestamp in the past is
// used.
- deadline = SystemClock::now() + kRoundedArbitraryDuration;
+ SystemClock::time_point deadline =
+ SystemClock::now() + kRoundedArbitraryDuration;
EXPECT_FALSE(semaphore.try_acquire_until(SystemClock::now() -
kRoundedArbitraryDuration));
EXPECT_LT(SystemClock::now(), deadline);
@@ -149,45 +178,58 @@
EXPECT_FALSE(pw_sync_BinarySemaphore_CallTryAcquire(&semaphore));
}
-TEST(BinarySemaphore, TryAcquireForInC) {
+TEST(BinarySemaphore, TryAcquireForFullInC) {
BinarySemaphore semaphore;
pw_sync_BinarySemaphore_CallRelease(&semaphore);
+ // Ensure it doesn't block and succeeds if not empty.
pw_chrono_SystemClock_TimePoint before = pw_chrono_SystemClock_Now();
ASSERT_TRUE(pw_sync_BinarySemaphore_CallTryAcquireFor(
&semaphore, kRoundedArbitraryDurationInC));
pw_chrono_SystemClock_Duration time_elapsed =
pw_chrono_SystemClock_TimeElapsed(before, pw_chrono_SystemClock_Now());
EXPECT_LT(time_elapsed.ticks, kRoundedArbitraryDurationInC.ticks);
+}
+
+TEST(BinarySemaphore, TryAcquireForEmptyPositiveTimeoutInC) {
+ BinarySemaphore semaphore;
// Ensure it blocks and fails when empty.
- before = pw_chrono_SystemClock_Now();
+ pw_chrono_SystemClock_TimePoint before = pw_chrono_SystemClock_Now();
EXPECT_FALSE(pw_sync_BinarySemaphore_CallTryAcquireFor(
&semaphore, kRoundedArbitraryDurationInC));
- time_elapsed =
+ pw_chrono_SystemClock_Duration time_elapsed =
pw_chrono_SystemClock_TimeElapsed(before, pw_chrono_SystemClock_Now());
EXPECT_GE(time_elapsed.ticks, kRoundedArbitraryDurationInC.ticks);
+}
+
+TEST(BinarySemaphore, TryAcquireForEmptyZeroLengthTimeoutInC) {
+ BinarySemaphore semaphore;
// Ensure it doesn't block and fails when empty and a zero length duration is
// used.
- before = pw_chrono_SystemClock_Now();
+ pw_chrono_SystemClock_TimePoint before = pw_chrono_SystemClock_Now();
EXPECT_FALSE(pw_sync_BinarySemaphore_CallTryAcquireFor(
&semaphore, PW_SYSTEM_CLOCK_MS(0)));
- time_elapsed =
- pw_chrono_SystemClock_TimeElapsed(before, pw_chrono_SystemClock_Now());
- EXPECT_LT(time_elapsed.ticks, kRoundedArbitraryDurationInC.ticks);
-
- // Ensure it doesn't block and fails when empty and a negative duration is
- // used.
- before = pw_chrono_SystemClock_Now();
- EXPECT_FALSE(pw_sync_BinarySemaphore_CallTryAcquireFor(
- &semaphore, PW_SYSTEM_CLOCK_MS(-kRoundedArbitraryDurationInC.ticks)));
- time_elapsed =
+ pw_chrono_SystemClock_Duration time_elapsed =
pw_chrono_SystemClock_TimeElapsed(before, pw_chrono_SystemClock_Now());
EXPECT_LT(time_elapsed.ticks, kRoundedArbitraryDurationInC.ticks);
}
-TEST(BinarySemaphore, TryAcquireUntilInC) {
+TEST(BinarySemaphore, TryAcquireForEmptyNegativeTimeoutInC) {
+ BinarySemaphore semaphore;
+
+ // Ensure it doesn't block and fails when empty and a negative duration is
+ // used.
+ pw_chrono_SystemClock_TimePoint before = pw_chrono_SystemClock_Now();
+ EXPECT_FALSE(pw_sync_BinarySemaphore_CallTryAcquireFor(
+ &semaphore, PW_SYSTEM_CLOCK_MS(-kRoundedArbitraryDurationInC.ticks)));
+ pw_chrono_SystemClock_Duration time_elapsed =
+ pw_chrono_SystemClock_TimeElapsed(before, pw_chrono_SystemClock_Now());
+ EXPECT_LT(time_elapsed.ticks, kRoundedArbitraryDurationInC.ticks);
+}
+
+TEST(BinarySemaphore, TryAcquireUntilFullInC) {
BinarySemaphore semaphore;
pw_sync_BinarySemaphore_CallRelease(&semaphore);
@@ -199,8 +241,13 @@
pw_sync_BinarySemaphore_CallTryAcquireUntil(&semaphore, deadline));
EXPECT_LT(pw_chrono_SystemClock_Now().duration_since_epoch.ticks,
deadline.duration_since_epoch.ticks);
+}
+
+TEST(BinarySemaphore, TryAcquireUntilEmptyFutureDeadlineInC) {
+ BinarySemaphore semaphore;
// Ensure it blocks and fails when empty.
+ pw_chrono_SystemClock_TimePoint deadline;
deadline.duration_since_epoch.ticks =
pw_chrono_SystemClock_Now().duration_since_epoch.ticks +
kRoundedArbitraryDurationInC.ticks;
@@ -208,8 +255,13 @@
pw_sync_BinarySemaphore_CallTryAcquireUntil(&semaphore, deadline));
EXPECT_GE(pw_chrono_SystemClock_Now().duration_since_epoch.ticks,
deadline.duration_since_epoch.ticks);
+}
+
+TEST(BinarySemaphore, TryAcquireUntilEmptyCurrentDeadlineInC) {
+ BinarySemaphore semaphore;
// Ensure it doesn't block and fails when empty and now is used.
+ pw_chrono_SystemClock_TimePoint deadline;
deadline.duration_since_epoch.ticks =
pw_chrono_SystemClock_Now().duration_since_epoch.ticks +
kRoundedArbitraryDurationInC.ticks;
@@ -217,9 +269,14 @@
&semaphore, pw_chrono_SystemClock_Now()));
EXPECT_LT(pw_chrono_SystemClock_Now().duration_since_epoch.ticks,
deadline.duration_since_epoch.ticks);
+}
+
+TEST(BinarySemaphore, TryAcquireUntilEmptyPastDeadlineInC) {
+ BinarySemaphore semaphore;
// Ensure it doesn't block and fails when empty and a timestamp in the past is
// used.
+ pw_chrono_SystemClock_TimePoint deadline;
deadline.duration_since_epoch.ticks =
pw_chrono_SystemClock_Now().duration_since_epoch.ticks +
kRoundedArbitraryDurationInC.ticks;
diff --git a/pw_sync/counting_semaphore_facade_test.cc b/pw_sync/counting_semaphore_facade_test.cc
index 510f8ed..2fcc983 100644
--- a/pw_sync/counting_semaphore_facade_test.cc
+++ b/pw_sync/counting_semaphore_facade_test.cc
@@ -97,58 +97,86 @@
EXPECT_FALSE(semaphore.try_acquire());
}
-TEST(CountingSemaphore, TryAcquireFor) {
+TEST(CountingSemaphore, TryAcquireForFull) {
CountingSemaphore semaphore;
semaphore.release();
+ // Ensure it doesn't block and succeeds if not empty.
SystemClock::time_point before = SystemClock::now();
EXPECT_TRUE(semaphore.try_acquire_for(kRoundedArbitraryDuration));
SystemClock::duration time_elapsed = SystemClock::now() - before;
EXPECT_LT(time_elapsed, kRoundedArbitraryDuration);
+}
+
+TEST(CountingSemaphore, TryAcquireForEmptyPositiveTimeout) {
+ CountingSemaphore semaphore;
// Ensure it blocks and fails when empty.
- before = SystemClock::now();
+ SystemClock::time_point before = SystemClock::now();
EXPECT_FALSE(semaphore.try_acquire_for(kRoundedArbitraryDuration));
- time_elapsed = SystemClock::now() - before;
+ SystemClock::duration time_elapsed = SystemClock::now() - before;
EXPECT_GE(time_elapsed, kRoundedArbitraryDuration);
+}
+
+TEST(CountingSemaphore, TryAcquireForEmptyZeroLengthTimeout) {
+ CountingSemaphore semaphore;
// Ensure it doesn't block and fails when empty and a zero length duration is
// used.
- before = SystemClock::now();
+ SystemClock::time_point before = SystemClock::now();
EXPECT_FALSE(semaphore.try_acquire_for(SystemClock::duration::zero()));
- time_elapsed = SystemClock::now() - before;
- EXPECT_LT(time_elapsed, kRoundedArbitraryDuration);
-
- // Ensure it doesn't block and fails when empty and a negative duration is
- // used.
- before = SystemClock::now();
- EXPECT_FALSE(semaphore.try_acquire_for(-kRoundedArbitraryDuration));
- time_elapsed = SystemClock::now() - before;
+ SystemClock::duration time_elapsed = SystemClock::now() - before;
EXPECT_LT(time_elapsed, kRoundedArbitraryDuration);
}
-TEST(CountingSemaphore, TryAcquireUntil) {
+TEST(CountingSemaphore, TryAcquireForEmptyNegativeTimeout) {
+ CountingSemaphore semaphore;
+
+ // Ensure it doesn't block and fails when empty and a negative duration is
+ // used.
+ SystemClock::time_point before = SystemClock::now();
+ EXPECT_FALSE(semaphore.try_acquire_for(-kRoundedArbitraryDuration));
+ SystemClock::duration time_elapsed = SystemClock::now() - before;
+ EXPECT_LT(time_elapsed, kRoundedArbitraryDuration);
+}
+
+TEST(CountingSemaphore, TryAcquireUntilFull) {
CountingSemaphore semaphore;
semaphore.release();
+ // Ensure it doesn't block and succeeds if not empty.
SystemClock::time_point deadline =
SystemClock::now() + kRoundedArbitraryDuration;
EXPECT_TRUE(semaphore.try_acquire_until(deadline));
EXPECT_LT(SystemClock::now(), deadline);
+}
+
+TEST(CountingSemaphore, TryAcquireUntilEmptyFutureDeadline) {
+ CountingSemaphore semaphore;
// Ensure it blocks and fails when empty.
- deadline = SystemClock::now() + kRoundedArbitraryDuration;
+ SystemClock::time_point deadline =
+ SystemClock::now() + kRoundedArbitraryDuration;
EXPECT_FALSE(semaphore.try_acquire_until(deadline));
EXPECT_GE(SystemClock::now(), deadline);
+}
+
+TEST(CountingSemaphore, TryAcquireUntilEmptyCurrentDeadline) {
+ CountingSemaphore semaphore;
// Ensure it doesn't block and fails when empty and now is used.
- deadline = SystemClock::now() + kRoundedArbitraryDuration;
+ SystemClock::time_point deadline =
+ SystemClock::now() + kRoundedArbitraryDuration;
EXPECT_FALSE(semaphore.try_acquire_until(SystemClock::now()));
EXPECT_LT(SystemClock::now(), deadline);
+}
+TEST(CountingSemaphore, TryAcquireUntilEmptyPastDeadline) {
+ CountingSemaphore semaphore;
// Ensure it doesn't block and fails when empty and a timestamp in the past is
// used.
- deadline = SystemClock::now() + kRoundedArbitraryDuration;
+ SystemClock::time_point deadline =
+ SystemClock::now() + kRoundedArbitraryDuration;
EXPECT_FALSE(semaphore.try_acquire_until(SystemClock::now() -
kRoundedArbitraryDuration));
EXPECT_LT(SystemClock::now(), deadline);
@@ -180,48 +208,62 @@
EXPECT_FALSE(pw_sync_CountingSemaphore_CallTryAcquire(&semaphore));
}
-TEST(CountingSemaphore, TryAcquireForInC) {
+TEST(CountingSemaphore, TryAcquireForFullInC) {
CountingSemaphore semaphore;
pw_sync_CountingSemaphore_CallRelease(&semaphore);
+ // Ensure it doesn't block and succeeds if not empty.
pw_chrono_SystemClock_TimePoint before = pw_chrono_SystemClock_Now();
ASSERT_TRUE(pw_sync_CountingSemaphore_CallTryAcquireFor(
&semaphore, kRoundedArbitraryDurationInC));
pw_chrono_SystemClock_Duration time_elapsed =
pw_chrono_SystemClock_TimeElapsed(before, pw_chrono_SystemClock_Now());
EXPECT_LT(time_elapsed.ticks, kRoundedArbitraryDurationInC.ticks);
+}
+
+TEST(CountingSemaphore, TryAcquireForEmptyPositiveTimeoutInC) {
+ CountingSemaphore semaphore;
// Ensure it blocks and fails when empty.
- before = pw_chrono_SystemClock_Now();
+ pw_chrono_SystemClock_TimePoint before = pw_chrono_SystemClock_Now();
EXPECT_FALSE(pw_sync_CountingSemaphore_CallTryAcquireFor(
&semaphore, kRoundedArbitraryDurationInC));
- time_elapsed =
+ pw_chrono_SystemClock_Duration time_elapsed =
pw_chrono_SystemClock_TimeElapsed(before, pw_chrono_SystemClock_Now());
EXPECT_GE(time_elapsed.ticks, kRoundedArbitraryDurationInC.ticks);
+}
+
+TEST(CountingSemaphore, TryAcquireForEmptyZeroLengthTimeoutInC) {
+ CountingSemaphore semaphore;
// Ensure it doesn't block and fails when empty and a zero length duration is
// used.
- before = pw_chrono_SystemClock_Now();
+ pw_chrono_SystemClock_TimePoint before = pw_chrono_SystemClock_Now();
EXPECT_FALSE(pw_sync_CountingSemaphore_CallTryAcquireFor(
&semaphore, PW_SYSTEM_CLOCK_MS(0)));
- time_elapsed =
- pw_chrono_SystemClock_TimeElapsed(before, pw_chrono_SystemClock_Now());
- EXPECT_LT(time_elapsed.ticks, kRoundedArbitraryDurationInC.ticks);
-
- // Ensure it doesn't block and fails when empty and a negative duration is
- // used.
- before = pw_chrono_SystemClock_Now();
- EXPECT_FALSE(pw_sync_CountingSemaphore_CallTryAcquireFor(
- &semaphore, PW_SYSTEM_CLOCK_MS(-kRoundedArbitraryDurationInC.ticks)));
- time_elapsed =
+ pw_chrono_SystemClock_Duration time_elapsed =
pw_chrono_SystemClock_TimeElapsed(before, pw_chrono_SystemClock_Now());
EXPECT_LT(time_elapsed.ticks, kRoundedArbitraryDurationInC.ticks);
}
-TEST(CountingSemaphore, TryAcquireUntilInC) {
+TEST(CountingSemaphore, TryAcquireForEmptyNegativeTimeoutInC) {
+ CountingSemaphore semaphore;
+
+ // Ensure it doesn't block and fails when empty and a negative duration is
+ // used.
+ pw_chrono_SystemClock_TimePoint before = pw_chrono_SystemClock_Now();
+ EXPECT_FALSE(pw_sync_CountingSemaphore_CallTryAcquireFor(
+ &semaphore, PW_SYSTEM_CLOCK_MS(-kRoundedArbitraryDurationInC.ticks)));
+ pw_chrono_SystemClock_Duration time_elapsed =
+ pw_chrono_SystemClock_TimeElapsed(before, pw_chrono_SystemClock_Now());
+ EXPECT_LT(time_elapsed.ticks, kRoundedArbitraryDurationInC.ticks);
+}
+
+TEST(CountingSemaphore, TryAcquireUntilFullInC) {
CountingSemaphore semaphore;
pw_sync_CountingSemaphore_CallRelease(&semaphore);
+ // Ensure it doesn't block and succeeds if not empty.
pw_chrono_SystemClock_TimePoint deadline;
deadline.duration_since_epoch.ticks =
pw_chrono_SystemClock_Now().duration_since_epoch.ticks +
@@ -230,8 +272,13 @@
pw_sync_CountingSemaphore_CallTryAcquireUntil(&semaphore, deadline));
EXPECT_LT(pw_chrono_SystemClock_Now().duration_since_epoch.ticks,
deadline.duration_since_epoch.ticks);
+}
+
+TEST(CountingSemaphore, TryAcquireUntilEmptyFutureDeadlineInC) {
+ CountingSemaphore semaphore;
// Ensure it blocks and fails when empty.
+ pw_chrono_SystemClock_TimePoint deadline;
deadline.duration_since_epoch.ticks =
pw_chrono_SystemClock_Now().duration_since_epoch.ticks +
kRoundedArbitraryDurationInC.ticks;
@@ -239,8 +286,13 @@
pw_sync_CountingSemaphore_CallTryAcquireUntil(&semaphore, deadline));
EXPECT_GE(pw_chrono_SystemClock_Now().duration_since_epoch.ticks,
deadline.duration_since_epoch.ticks);
+}
+
+TEST(CountingSemaphore, TryAcquireUntilEmptyCurrentDeadlineInC) {
+ CountingSemaphore semaphore;
// Ensure it doesn't block and fails when empty and now is used.
+ pw_chrono_SystemClock_TimePoint deadline;
deadline.duration_since_epoch.ticks =
pw_chrono_SystemClock_Now().duration_since_epoch.ticks +
kRoundedArbitraryDurationInC.ticks;
@@ -248,9 +300,14 @@
&semaphore, pw_chrono_SystemClock_Now()));
EXPECT_LT(pw_chrono_SystemClock_Now().duration_since_epoch.ticks,
deadline.duration_since_epoch.ticks);
+}
+
+TEST(CountingSemaphore, TryAcquireUntilEmptyPastDeadlineInC) {
+ CountingSemaphore semaphore;
// Ensure it doesn't block and fails when empty and a timestamp in the past is
// used.
+ pw_chrono_SystemClock_TimePoint deadline;
deadline.duration_since_epoch.ticks =
pw_chrono_SystemClock_Now().duration_since_epoch.ticks +
kRoundedArbitraryDurationInC.ticks;
diff --git a/pw_sync/timed_thread_notification_facade_test.cc b/pw_sync/timed_thread_notification_facade_test.cc
index 1922a79..84bb415 100644
--- a/pw_sync/timed_thread_notification_facade_test.cc
+++ b/pw_sync/timed_thread_notification_facade_test.cc
@@ -60,55 +60,84 @@
EXPECT_FALSE(raise_notification.try_acquire());
}
-TEST(TimedThreadNotification, TryAcquireFor) {
+TEST(TimedThreadNotification, TryAcquireForNotified) {
TimedThreadNotification notification;
notification.release();
+ // Ensure it doesn't block and succeeds when notified.
SystemClock::time_point before = SystemClock::now();
EXPECT_TRUE(notification.try_acquire_for(kRoundedArbitraryDuration));
SystemClock::duration time_elapsed = SystemClock::now() - before;
EXPECT_LT(time_elapsed, kRoundedArbitraryDuration);
+}
+
+TEST(TimedThreadNotification, TryAcquireForNotNotifiedPositiveTimeout) {
+ TimedThreadNotification notification;
// Ensure it blocks and fails when not notified for the full timeout.
- before = SystemClock::now();
+ SystemClock::time_point before = SystemClock::now();
EXPECT_FALSE(notification.try_acquire_for(kRoundedArbitraryDuration));
- time_elapsed = SystemClock::now() - before;
+ SystemClock::duration time_elapsed = SystemClock::now() - before;
EXPECT_GE(time_elapsed, kRoundedArbitraryDuration);
+}
+
+TEST(TimedThreadNotification, TryAcquireForNotNotifiedZeroLengthTimeout) {
+ TimedThreadNotification notification;
// Ensure it doesn't block when a zero length duration is used.
- before = SystemClock::now();
+ SystemClock::time_point before = SystemClock::now();
EXPECT_FALSE(notification.try_acquire_for(SystemClock::duration::zero()));
- time_elapsed = SystemClock::now() - before;
- EXPECT_LT(time_elapsed, kRoundedArbitraryDuration);
-
- // Ensure it doesn't block when a negative duration is used.
- before = SystemClock::now();
- EXPECT_FALSE(notification.try_acquire_for(-kRoundedArbitraryDuration));
- time_elapsed = SystemClock::now() - before;
+ SystemClock::duration time_elapsed = SystemClock::now() - before;
EXPECT_LT(time_elapsed, kRoundedArbitraryDuration);
}
-TEST(TimedThreadNotification, tryAcquireUntil) {
+TEST(TimedThreadNotification, TryAcquireForNotNotifiedNegativeTimeout) {
+ TimedThreadNotification notification;
+
+ // Ensure it doesn't block when a negative duration is used.
+ SystemClock::time_point before = SystemClock::now();
+ EXPECT_FALSE(notification.try_acquire_for(-kRoundedArbitraryDuration));
+ SystemClock::duration time_elapsed = SystemClock::now() - before;
+ EXPECT_LT(time_elapsed, kRoundedArbitraryDuration);
+}
+
+TEST(TimedThreadNotification, TryAcquireUntilNotified) {
TimedThreadNotification notification;
notification.release();
+ // Ensure it doesn't block and succeeds when notified.
SystemClock::time_point deadline =
SystemClock::now() + kRoundedArbitraryDuration;
EXPECT_TRUE(notification.try_acquire_until(deadline));
EXPECT_LT(SystemClock::now(), deadline);
+}
+
+TEST(TimedThreadNotification, TryAcquireUntilNotNotifiedFutureDeadline) {
+ TimedThreadNotification notification;
// Ensure it blocks and fails when not notified.
- deadline = SystemClock::now() + kRoundedArbitraryDuration;
+ SystemClock::time_point deadline =
+ SystemClock::now() + kRoundedArbitraryDuration;
EXPECT_FALSE(notification.try_acquire_until(deadline));
EXPECT_GE(SystemClock::now(), deadline);
+}
+
+TEST(TimedThreadNotification, TryAcquireUntilNotNotifiedCurrentDeadline) {
+ TimedThreadNotification notification;
// Ensure it doesn't block when now is used.
- deadline = SystemClock::now() + kRoundedArbitraryDuration;
+ SystemClock::time_point deadline =
+ SystemClock::now() + kRoundedArbitraryDuration;
EXPECT_FALSE(notification.try_acquire_until(SystemClock::now()));
EXPECT_LT(SystemClock::now(), deadline);
+}
+
+TEST(TimedThreadNotification, TryAcquireUntilNotNotifiedPastDeadline) {
+ TimedThreadNotification notification;
// Ensure it doesn't block when a timestamp in the past is used.
- deadline = SystemClock::now() + kRoundedArbitraryDuration;
+ SystemClock::time_point deadline =
+ SystemClock::now() + kRoundedArbitraryDuration;
EXPECT_FALSE(notification.try_acquire_until(SystemClock::now() -
kRoundedArbitraryDuration));
EXPECT_LT(SystemClock::now(), deadline);
diff --git a/pw_thread/sleep_facade_test.cc b/pw_thread/sleep_facade_test.cc
index 42f4b8c..ca83880 100644
--- a/pw_thread/sleep_facade_test.cc
+++ b/pw_thread/sleep_facade_test.cc
@@ -41,7 +41,7 @@
constexpr pw_chrono_SystemClock_Duration kRoundedArbitraryDurationInC =
PW_SYSTEM_CLOCK_MS(42);
-TEST(Sleep, SleepFor) {
+TEST(Sleep, SleepForPositiveDuration) {
// Ensure we are in a thread context, meaning we are permitted to sleep.
ASSERT_NE(get_id(), thread::Id());
@@ -49,21 +49,31 @@
sleep_for(kRoundedArbitraryDuration);
SystemClock::duration time_elapsed = SystemClock::now() - before;
EXPECT_GE(time_elapsed, kRoundedArbitraryDuration);
+}
+
+TEST(Sleep, SleepForZeroLengthDuration) {
+ // Ensure we are in a thread context, meaning we are permitted to sleep.
+ ASSERT_NE(get_id(), thread::Id());
// Ensure it doesn't sleep when a zero length duration is used.
- before = SystemClock::now();
+ SystemClock::time_point before = SystemClock::now();
sleep_for(SystemClock::duration::zero());
- time_elapsed = SystemClock::now() - before;
- EXPECT_LT(time_elapsed, kRoundedArbitraryDuration);
-
- // Ensure it doesn't sleep when a negative duration is used.
- before = SystemClock::now();
- sleep_for(-kRoundedArbitraryDuration);
- time_elapsed = SystemClock::now() - before;
+ SystemClock::duration time_elapsed = SystemClock::now() - before;
EXPECT_LT(time_elapsed, kRoundedArbitraryDuration);
}
-TEST(Sleep, SleepUntil) {
+TEST(Sleep, SleepForNegativeDuration) {
+ // Ensure we are in a thread context, meaning we are permitted to sleep.
+ ASSERT_NE(get_id(), thread::Id());
+
+ // Ensure it doesn't sleep when a negative duration is used.
+ SystemClock::time_point before = SystemClock::now();
+ sleep_for(-kRoundedArbitraryDuration);
+ SystemClock::duration time_elapsed = SystemClock::now() - before;
+ EXPECT_LT(time_elapsed, kRoundedArbitraryDuration);
+}
+
+TEST(Sleep, SleepUntilFutureWakeupTime) {
// Ensure we are in a thread context, meaning we are permitted to sleep.
ASSERT_NE(get_id(), thread::Id());
@@ -71,19 +81,31 @@
SystemClock::now() + kRoundedArbitraryDuration;
sleep_until(deadline);
EXPECT_GE(SystemClock::now(), deadline);
+}
+
+TEST(Sleep, SleepUntilCurrentWakeupTime) {
+ // Ensure we are in a thread context, meaning we are permitted to sleep.
+ ASSERT_NE(get_id(), thread::Id());
// Ensure it doesn't sleep when now is used.
- deadline = SystemClock::now() + kRoundedArbitraryDuration;
+ SystemClock::time_point deadline =
+ SystemClock::now() + kRoundedArbitraryDuration;
sleep_until(SystemClock::now());
EXPECT_LT(SystemClock::now(), deadline);
+}
+
+TEST(Sleep, SleepUntilPastWakeupTime) {
+ // Ensure we are in a thread context, meaning we are permitted to sleep.
+ ASSERT_NE(get_id(), thread::Id());
// Ensure it doesn't sleep when a timestamp in the past is used.
- deadline = SystemClock::now() + kRoundedArbitraryDuration;
+ SystemClock::time_point deadline =
+ SystemClock::now() + kRoundedArbitraryDuration;
sleep_until(SystemClock::now() - kRoundedArbitraryDuration);
EXPECT_LT(SystemClock::now(), deadline);
}
-TEST(Sleep, SleepForInC) {
+TEST(Sleep, SleepForPositiveDurationInC) {
// Ensure we are in a thread context, meaning we are permitted to sleep.
ASSERT_NE(get_id(), thread::Id());
@@ -92,24 +114,34 @@
pw_chrono_SystemClock_Duration time_elapsed =
pw_chrono_SystemClock_TimeElapsed(before, pw_chrono_SystemClock_Now());
EXPECT_GE(time_elapsed.ticks, kRoundedArbitraryDurationInC.ticks);
+}
+
+TEST(Sleep, SleepForZeroLengthDurationInC) {
+ // Ensure we are in a thread context, meaning we are permitted to sleep.
+ ASSERT_NE(get_id(), thread::Id());
// Ensure it doesn't sleep when a zero length duration is used.
- before = pw_chrono_SystemClock_Now();
+ pw_chrono_SystemClock_TimePoint before = pw_chrono_SystemClock_Now();
pw_this_thread_SleepFor(PW_SYSTEM_CLOCK_MS(0));
- time_elapsed =
- pw_chrono_SystemClock_TimeElapsed(before, pw_chrono_SystemClock_Now());
- EXPECT_LT(time_elapsed.ticks, kRoundedArbitraryDurationInC.ticks);
-
- // Ensure it doesn't sleep when a negative duration is used.
- before = pw_chrono_SystemClock_Now();
- pw_this_thread_SleepFor(
- PW_SYSTEM_CLOCK_MS(-kRoundedArbitraryDurationInC.ticks));
- time_elapsed =
+ pw_chrono_SystemClock_Duration time_elapsed =
pw_chrono_SystemClock_TimeElapsed(before, pw_chrono_SystemClock_Now());
EXPECT_LT(time_elapsed.ticks, kRoundedArbitraryDurationInC.ticks);
}
-TEST(Sleep, SleepUntilInC) {
+TEST(Sleep, SleepForNegativeDurationInC) {
+ // Ensure we are in a thread context, meaning we are permitted to sleep.
+ ASSERT_NE(get_id(), thread::Id());
+
+ // Ensure it doesn't sleep when a negative duration is used.
+ pw_chrono_SystemClock_TimePoint before = pw_chrono_SystemClock_Now();
+ pw_this_thread_SleepFor(
+ PW_SYSTEM_CLOCK_MS(-kRoundedArbitraryDurationInC.ticks));
+ pw_chrono_SystemClock_Duration time_elapsed =
+ pw_chrono_SystemClock_TimeElapsed(before, pw_chrono_SystemClock_Now());
+ EXPECT_LT(time_elapsed.ticks, kRoundedArbitraryDurationInC.ticks);
+}
+
+TEST(Sleep, SleepUntilFutureWakeupTimeInC) {
// Ensure we are in a thread context, meaning we are permitted to sleep.
ASSERT_NE(get_id(), thread::Id());
@@ -120,16 +152,28 @@
pw_this_thread_CallSleepUntil(deadline);
EXPECT_GE(pw_chrono_SystemClock_Now().duration_since_epoch.ticks,
deadline.duration_since_epoch.ticks);
+}
+
+TEST(Sleep, SleepUntilCurrentWakeupTimeInC) {
+ // Ensure we are in a thread context, meaning we are permitted to sleep.
+ ASSERT_NE(get_id(), thread::Id());
// Ensure it doesn't sleep when now is used.
+ pw_chrono_SystemClock_TimePoint deadline;
deadline.duration_since_epoch.ticks =
pw_chrono_SystemClock_Now().duration_since_epoch.ticks +
kRoundedArbitraryDurationInC.ticks;
pw_this_thread_CallSleepUntil(pw_chrono_SystemClock_Now());
EXPECT_LT(pw_chrono_SystemClock_Now().duration_since_epoch.ticks,
deadline.duration_since_epoch.ticks);
+}
+
+TEST(Sleep, SleepUntilPastWakeupTimeInC) {
+ // Ensure we are in a thread context, meaning we are permitted to sleep.
+ ASSERT_NE(get_id(), thread::Id());
// Ensure it doesn't sleep when a timestamp in the past is used.
+ pw_chrono_SystemClock_TimePoint deadline;
deadline.duration_since_epoch.ticks =
pw_chrono_SystemClock_Now().duration_since_epoch.ticks +
kRoundedArbitraryDurationInC.ticks;