diff --git a/gloop/util/task/BUILD b/gloop/util/task/BUILD index ae355f36..b2c7fb85 100644 --- a/gloop/util/task/BUILD +++ b/gloop/util/task/BUILD @@ -98,8 +98,6 @@ cc_test( ":sync_task", ":task", "//gloop:gloop_test_main", - "//gloop/util/status", - "@abseil-cpp//absl/functional:bind_front", "@abseil-cpp//absl/status", "@abseil-cpp//absl/status:status_matchers", "@abseil-cpp//absl/synchronization", diff --git a/gloop/util/task/sleep_test.cc b/gloop/util/task/sleep_test.cc index 3ec20419..ac26cb9d 100644 --- a/gloop/util/task/sleep_test.cc +++ b/gloop/util/task/sleep_test.cc @@ -20,7 +20,9 @@ #include "gloop/util/task/sleep.h" -#include "absl/functional/bind_front.h" +#include +#include + #include "absl/status/status.h" #include "absl/status/status_matchers.h" #include "absl/synchronization/notification.h" @@ -36,6 +38,9 @@ namespace { using ::absl_testing::IsOk; using ::absl_testing::StatusIs; +using ::testing::AllOf; +using ::testing::Ge; +using ::testing::Le; constexpr absl::Duration kEpsilon = absl::Milliseconds(500); @@ -44,55 +49,63 @@ TEST(SleepUntil, Future) { SyncTask s; SleepUntil(t, s.task()); s.WaitIgnoresCancel(); - EXPECT_GE(absl::Now(), t); - EXPECT_LE(absl::Now(), t + kEpsilon); + EXPECT_THAT(absl::Now(), AllOf(Ge(t), Le(t + kEpsilon))); EXPECT_THAT(s.status(), IsOk()); } -TEST(SleepUntil, Past) { - const absl::Time now = absl::Now(); - SyncTask s; - SleepUntil(now - absl::Seconds(1), s.task()); - s.WaitIgnoresCancel(); - EXPECT_LE(absl::Now(), now + kEpsilon); - EXPECT_THAT(s.status(), IsOk()); -} +struct PastDeadlineTestCase { + std::string name; + std::function deadline_fn; +}; + +class SleepUntilPastTest + : public ::testing::TestWithParam {}; -TEST(SleepUntil, InfinitePast) { +TEST_P(SleepUntilPastTest, CompletesImmediately) { const absl::Time now = absl::Now(); SyncTask s; - SleepUntil(absl::InfinitePast(), s.task()); + SleepUntil(GetParam().deadline_fn(), s.task()); s.WaitIgnoresCancel(); - EXPECT_LE(absl::Now(), now + kEpsilon); + EXPECT_THAT(absl::Now(), Le(now + kEpsilon)); EXPECT_THAT(s.status(), IsOk()); } +INSTANTIATE_TEST_SUITE_P( + , SleepUntilPastTest, + ::testing::Values( + PastDeadlineTestCase{"OneSecondInPast", + [] { return absl::Now() - absl::Seconds(1); }}, + PastDeadlineTestCase{"InfinitePast", + [] { return absl::InfinitePast(); }}, + PastDeadlineTestCase{"UnixEpoch", [] { return absl::Time(); }}), + [](const ::testing::TestParamInfo& info) { + return info.param.name; + }); + TEST(SleepUntil, CancelLongSleep) { const absl::Time now = absl::Now(); SyncTask s; SleepUntil(now + absl::Seconds(1000000), s.task()); s.task()->Cancel(); s.WaitIgnoresCancel(); - EXPECT_LE(absl::Now(), now + kEpsilon); + EXPECT_THAT(absl::Now(), Le(now + kEpsilon)); EXPECT_THAT(s.status(), StatusIs(absl::StatusCode::kCancelled)); } -void CancelTask(absl::Notification* n, absl::Status* s, Task* task) { - // We cancel the task, though it is too late for the cancellation to - // have any effect. - task->Cancel(); - *s = task->status(); - n->Notify(); -} - TEST(SleepUntil, CancelDuringCallback) { absl::Notification n; absl::Status s; - Task t(absl::bind_front(CancelTask, &n, &s)); + Task t([&n, &s](Task* task) { + // We cancel the task, though it is too late for the cancellation to + // have any effect. + task->Cancel(); + s = task->status(); + n.Notify(); + }); const absl::Time now = absl::Now(); SleepUntil(now - absl::Seconds(1), &t); n.WaitForNotification(); - EXPECT_LE(absl::Now(), now + kEpsilon); + EXPECT_THAT(absl::Now(), Le(now + kEpsilon)); EXPECT_THAT(s, IsOk()); }