mirror of
https://github.com/kidfromjupiter/nearby.git
synced 2026-09-15 15:16:12 -04:00
Signed-off-by: Alexey Polyudov <apolyudov@google.com> Change-Id: I9850950db8bd84f0904ea1a413151887f52098cf
109 lines
2.8 KiB
C++
109 lines
2.8 KiB
C++
#include "platform/public/scheduled_executor.h"
|
|
|
|
#include <atomic>
|
|
#include <functional>
|
|
|
|
#include "platform/base/exception.h"
|
|
#include "gtest/gtest.h"
|
|
#include "absl/synchronization/mutex.h"
|
|
#include "absl/time/clock.h"
|
|
#include "absl/time/time.h"
|
|
|
|
namespace location {
|
|
namespace nearby {
|
|
|
|
// kShortDelay must be significant enough to guarantee that OS under heavy load
|
|
// should be able to execute the non-blocking test paths within this time.
|
|
absl::Duration kShortDelay = absl::Milliseconds(100);
|
|
|
|
// kLongDelay must be long enough to make sure that under OS under heavy load
|
|
// will let kShortDelay fire and jobs scheduled before the kLongDelay fires.
|
|
absl::Duration kLongDelay = 10 * kShortDelay;
|
|
|
|
TEST(ScheduledExecutorTest, ConsructorDestructorWorks) {
|
|
ScheduledExecutor executor;
|
|
}
|
|
|
|
TEST(ScheduledExecutorTest, CanExecute) {
|
|
absl::Mutex mutex;
|
|
absl::CondVar cond;
|
|
std::atomic_bool done = false;
|
|
ScheduledExecutor executor;
|
|
executor.Execute([&done, &cond]() {
|
|
done = true;
|
|
cond.SignalAll();
|
|
});
|
|
{
|
|
absl::MutexLock lock(&mutex);
|
|
if (!done) {
|
|
cond.WaitWithTimeout(&mutex, kLongDelay);
|
|
}
|
|
}
|
|
EXPECT_TRUE(done);
|
|
}
|
|
|
|
TEST(ScheduledExecutorTest, CanSchedule) {
|
|
ScheduledExecutor executor;
|
|
std::atomic_int value = 0;
|
|
absl::Mutex mutex;
|
|
absl::CondVar cond;
|
|
// schedule job due in kLongDelay.
|
|
executor.Schedule(
|
|
[&value, &cond]() {
|
|
EXPECT_EQ(value, 1);
|
|
value = 5;
|
|
cond.Signal();
|
|
},
|
|
kLongDelay);
|
|
// schedule job due in kShortDelay; must fire before the first one.
|
|
executor.Schedule(
|
|
[&value]() {
|
|
EXPECT_EQ(value, 0);
|
|
value = 1;
|
|
},
|
|
kShortDelay);
|
|
{
|
|
// wait for the final job to unblock us; wait longer than kLongDelay.
|
|
absl::MutexLock lock(&mutex);
|
|
cond.WaitWithTimeout(&mutex, 2 * kLongDelay);
|
|
}
|
|
EXPECT_EQ(value, 5);
|
|
}
|
|
|
|
TEST(ScheduledExecutorTest, CanCancel) {
|
|
ScheduledExecutor executor;
|
|
std::atomic_int value = 0;
|
|
Cancelable cancelable =
|
|
executor.Schedule([&value]() { value += 1; }, kShortDelay);
|
|
EXPECT_EQ(value, 0);
|
|
EXPECT_TRUE(cancelable.Cancel());
|
|
absl::SleepFor(kLongDelay);
|
|
EXPECT_EQ(value, 0);
|
|
}
|
|
|
|
TEST(ScheduledExecutorTest, FailToCancel) {
|
|
absl::Mutex mutex;
|
|
absl::CondVar cond;
|
|
ScheduledExecutor executor;
|
|
std::atomic_int value = 0;
|
|
// Schedule job in kShortDelay, which will we will attempt to cancel later.
|
|
Cancelable cancelable =
|
|
executor.Schedule([&value]() { value += 1; }, kShortDelay);
|
|
// schedule another job to test results of the first one, in kLongDelay.
|
|
executor.Schedule(
|
|
[&cancelable, &cond]() {
|
|
EXPECT_FALSE(cancelable.Cancel());
|
|
// Wake up main thread.
|
|
cond.Signal();
|
|
},
|
|
kLongDelay);
|
|
{
|
|
absl::MutexLock lock(&mutex);
|
|
cond.Wait(&mutex);
|
|
}
|
|
EXPECT_EQ(value, 1);
|
|
}
|
|
|
|
} // namespace nearby
|
|
} // namespace location
|