Merge branch 'google3' up to cl/358328147.

This commit is contained in:
hai007
2021-02-19 11:34:27 -08:00
6 changed files with 137 additions and 5 deletions
@@ -18,6 +18,7 @@
#include <functional>
#include "platform/base/exception.h"
#include "platform/public/count_down_latch.h"
#include "gtest/gtest.h"
#include "absl/synchronization/mutex.h"
#include "absl/time/clock.h"
@@ -95,6 +96,20 @@ TEST(ScheduledExecutorTest, CanCancel) {
EXPECT_EQ(value, 0);
}
TEST(ScheduledExecutorTest, CanCancelTwice) {
ScheduledExecutor executor;
std::atomic_int value = 0;
Cancelable cancelable =
executor.Schedule([&value]() { value += 1; }, kShortDelay);
EXPECT_EQ(value, 0);
cancelable.Cancel();
cancelable.Cancel();
absl::SleepFor(kLongDelay);
EXPECT_EQ(value, 0);
}
TEST(ScheduledExecutorTest, FailToCancel) {
absl::Mutex mutex;
absl::CondVar cond;
@@ -118,5 +133,47 @@ TEST(ScheduledExecutorTest, FailToCancel) {
EXPECT_EQ(value, 1);
}
TEST(ScheduledExecutorTest,
CancelWhileRunning_TaskCompletesBeforeCancelReturns) {
CountDownLatch start_latch(1);
ScheduledExecutor executor;
std::atomic_int value = 0;
// A task that takes a little bit of time to complete
Cancelable cancelable = executor.Schedule(
[&start_latch, &value]() {
start_latch.CountDown();
absl::SleepFor(kLongDelay);
value += 1;
},
absl::ZeroDuration());
start_latch.Await();
cancelable.Cancel();
EXPECT_EQ(value, 1);
}
TEST(ScheduledExecutorTest,
CancelTwiceWhileRunning_TaskCompletesBeforeCancelReturns) {
CountDownLatch start_latch(1);
ScheduledExecutor executor;
std::atomic_int value = 0;
// A task that takes a little bit of time to complete
Cancelable cancelable = executor.Schedule(
[&start_latch, &value]() {
start_latch.CountDown();
absl::SleepFor(kLongDelay);
value += 1;
},
absl::ZeroDuration());
start_latch.Await();
cancelable.Cancel();
cancelable.Cancel();
EXPECT_EQ(value, 1);
}
} // namespace nearby
} // namespace location