Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions gloop/util/task/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
63 changes: 38 additions & 25 deletions gloop/util/task/sleep_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@

#include "gloop/util/task/sleep.h"

#include "absl/functional/bind_front.h"
#include <functional>
#include <string>

#include "absl/status/status.h"
#include "absl/status/status_matchers.h"
#include "absl/synchronization/notification.h"
Expand All @@ -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);

Expand All @@ -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<absl::Time()> deadline_fn;
};

class SleepUntilPastTest
: public ::testing::TestWithParam<PastDeadlineTestCase> {};

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<PastDeadlineTestCase>& 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());
}

Expand Down
Loading