2015-11-14 01:20:07 +00:00
|
|
|
// Copyright 2015 the V8 project authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
|
|
|
#include "src/base/atomicops.h"
|
|
|
|
#include "src/base/platform/platform.h"
|
|
|
|
#include "src/cancelable-task.h"
|
2018-11-09 15:51:43 +00:00
|
|
|
#include "testing/gmock/include/gmock/gmock.h"
|
2015-11-14 01:20:07 +00:00
|
|
|
#include "testing/gtest/include/gtest/gtest.h"
|
|
|
|
|
|
|
|
namespace v8 {
|
|
|
|
namespace internal {
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2018-11-08 16:44:32 +00:00
|
|
|
using ResultType = std::atomic<CancelableTaskManager::Id>;
|
|
|
|
|
2018-11-09 15:51:43 +00:00
|
|
|
class CancelableTaskManagerTest;
|
|
|
|
|
2015-11-14 01:20:07 +00:00
|
|
|
class TestTask : public Task, public Cancelable {
|
|
|
|
public:
|
2018-11-09 15:51:43 +00:00
|
|
|
enum Mode { kDoNothing, kWaitTillCancelTriggered, kCheckNotRun };
|
2015-11-14 01:20:07 +00:00
|
|
|
|
2018-11-09 15:51:43 +00:00
|
|
|
TestTask(CancelableTaskManagerTest* test, ResultType* result, Mode mode);
|
2015-11-14 01:20:07 +00:00
|
|
|
|
2018-11-09 15:51:43 +00:00
|
|
|
// Task override.
|
|
|
|
void Run() final;
|
2015-11-14 01:20:07 +00:00
|
|
|
|
|
|
|
private:
|
2018-11-08 16:44:32 +00:00
|
|
|
ResultType* const result_;
|
|
|
|
const Mode mode_;
|
2018-11-09 15:51:43 +00:00
|
|
|
CancelableTaskManagerTest* const test_;
|
2015-11-14 01:20:07 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class SequentialRunner {
|
|
|
|
public:
|
2018-11-08 16:44:32 +00:00
|
|
|
explicit SequentialRunner(std::unique_ptr<TestTask> task)
|
|
|
|
: task_(std::move(task)), task_id_(task_->id()) {}
|
2015-11-14 01:20:07 +00:00
|
|
|
|
|
|
|
void Run() {
|
|
|
|
task_->Run();
|
2018-11-08 16:44:32 +00:00
|
|
|
task_.reset();
|
2015-11-14 01:20:07 +00:00
|
|
|
}
|
|
|
|
|
2018-11-08 16:44:32 +00:00
|
|
|
CancelableTaskManager::Id task_id() const { return task_id_; }
|
|
|
|
|
2015-11-14 01:20:07 +00:00
|
|
|
private:
|
2018-11-08 16:44:32 +00:00
|
|
|
std::unique_ptr<TestTask> task_;
|
|
|
|
const CancelableTaskManager::Id task_id_;
|
2015-11-14 01:20:07 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class ThreadedRunner final : public base::Thread {
|
|
|
|
public:
|
2018-11-08 16:44:32 +00:00
|
|
|
explicit ThreadedRunner(std::unique_ptr<TestTask> task)
|
|
|
|
: Thread(Options("runner thread")),
|
|
|
|
task_(std::move(task)),
|
|
|
|
task_id_(task_->id()) {}
|
2015-11-14 01:20:07 +00:00
|
|
|
|
2018-09-14 15:34:02 +00:00
|
|
|
void Run() override {
|
2015-11-14 01:20:07 +00:00
|
|
|
task_->Run();
|
2018-11-08 16:44:32 +00:00
|
|
|
task_.reset();
|
2015-11-14 01:20:07 +00:00
|
|
|
}
|
|
|
|
|
2018-11-08 16:44:32 +00:00
|
|
|
CancelableTaskManager::Id task_id() const { return task_id_; }
|
|
|
|
|
2015-11-14 01:20:07 +00:00
|
|
|
private:
|
2018-11-08 16:44:32 +00:00
|
|
|
std::unique_ptr<TestTask> task_;
|
|
|
|
const CancelableTaskManager::Id task_id_;
|
2015-11-14 01:20:07 +00:00
|
|
|
};
|
|
|
|
|
2018-11-08 16:44:32 +00:00
|
|
|
class CancelableTaskManagerTest : public ::testing::Test {
|
|
|
|
public:
|
|
|
|
CancelableTaskManager* manager() { return &manager_; }
|
2015-11-14 01:20:07 +00:00
|
|
|
|
2018-11-08 16:44:32 +00:00
|
|
|
std::unique_ptr<TestTask> NewTask(
|
|
|
|
ResultType* result, TestTask::Mode mode = TestTask::kDoNothing) {
|
2018-11-09 15:51:43 +00:00
|
|
|
return base::make_unique<TestTask>(this, result, mode);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CancelAndWait() {
|
|
|
|
cancel_triggered_.store(true);
|
|
|
|
manager_.CancelAndWait();
|
2018-11-08 16:44:32 +00:00
|
|
|
}
|
2015-11-14 01:20:07 +00:00
|
|
|
|
2018-11-09 15:51:43 +00:00
|
|
|
TryAbortResult TryAbortAll() {
|
|
|
|
cancel_triggered_.store(true);
|
|
|
|
return manager_.TryAbortAll();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool cancel_triggered() const { return cancel_triggered_.load(); }
|
|
|
|
|
2018-11-08 16:44:32 +00:00
|
|
|
private:
|
|
|
|
CancelableTaskManager manager_;
|
2018-11-09 15:51:43 +00:00
|
|
|
std::atomic<bool> cancel_triggered_{false};
|
2018-11-08 16:44:32 +00:00
|
|
|
};
|
2015-11-14 01:20:07 +00:00
|
|
|
|
2018-11-09 15:51:43 +00:00
|
|
|
TestTask::TestTask(CancelableTaskManagerTest* test, ResultType* result,
|
|
|
|
Mode mode)
|
|
|
|
: Cancelable(test->manager()), result_(result), mode_(mode), test_(test) {}
|
|
|
|
|
|
|
|
void TestTask::Run() {
|
|
|
|
if (!TryRun()) return;
|
|
|
|
|
|
|
|
result_->store(id());
|
|
|
|
|
|
|
|
switch (mode_) {
|
|
|
|
case kWaitTillCancelTriggered:
|
|
|
|
// Simple busy wait until the main thread tried to cancel.
|
|
|
|
while (!test_->cancel_triggered()) {
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case kCheckNotRun:
|
|
|
|
// Check that we never execute {RunInternal}.
|
|
|
|
EXPECT_TRUE(false);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-14 01:20:07 +00:00
|
|
|
} // namespace
|
|
|
|
|
2018-11-08 16:44:32 +00:00
|
|
|
TEST_F(CancelableTaskManagerTest, EmptyCancelableTaskManager) {
|
2018-11-09 15:51:43 +00:00
|
|
|
CancelAndWait();
|
2015-11-14 01:20:07 +00:00
|
|
|
}
|
|
|
|
|
2018-11-08 16:44:32 +00:00
|
|
|
TEST_F(CancelableTaskManagerTest, SequentialCancelAndWait) {
|
|
|
|
ResultType result1{0};
|
|
|
|
SequentialRunner runner1(NewTask(&result1, TestTask::kCheckNotRun));
|
|
|
|
EXPECT_EQ(0u, result1);
|
2018-11-09 15:51:43 +00:00
|
|
|
CancelAndWait();
|
2018-11-08 16:44:32 +00:00
|
|
|
EXPECT_EQ(0u, result1);
|
|
|
|
runner1.Run();
|
|
|
|
EXPECT_EQ(0u, result1);
|
2015-11-14 01:20:07 +00:00
|
|
|
}
|
|
|
|
|
2018-11-08 16:44:32 +00:00
|
|
|
TEST_F(CancelableTaskManagerTest, SequentialMultipleTasks) {
|
|
|
|
ResultType result1{0};
|
|
|
|
ResultType result2{0};
|
|
|
|
SequentialRunner runner1(NewTask(&result1));
|
|
|
|
SequentialRunner runner2(NewTask(&result2));
|
|
|
|
EXPECT_EQ(1u, runner1.task_id());
|
|
|
|
EXPECT_EQ(2u, runner2.task_id());
|
|
|
|
|
|
|
|
EXPECT_EQ(0u, result1);
|
|
|
|
runner1.Run();
|
|
|
|
EXPECT_EQ(1u, result1);
|
|
|
|
|
|
|
|
EXPECT_EQ(0u, result2);
|
|
|
|
runner2.Run();
|
|
|
|
EXPECT_EQ(2u, result2);
|
|
|
|
|
2018-11-09 15:51:43 +00:00
|
|
|
CancelAndWait();
|
2018-11-08 16:44:32 +00:00
|
|
|
EXPECT_EQ(TryAbortResult::kTaskRemoved, manager()->TryAbort(1));
|
|
|
|
EXPECT_EQ(TryAbortResult::kTaskRemoved, manager()->TryAbort(2));
|
2015-11-14 01:20:07 +00:00
|
|
|
}
|
|
|
|
|
2018-11-08 16:44:32 +00:00
|
|
|
TEST_F(CancelableTaskManagerTest, ThreadedMultipleTasksStarted) {
|
|
|
|
ResultType result1{0};
|
|
|
|
ResultType result2{0};
|
2018-11-09 15:51:43 +00:00
|
|
|
ThreadedRunner runner1(NewTask(&result1, TestTask::kWaitTillCancelTriggered));
|
|
|
|
ThreadedRunner runner2(NewTask(&result2, TestTask::kWaitTillCancelTriggered));
|
2015-11-14 01:20:07 +00:00
|
|
|
runner1.Start();
|
|
|
|
runner2.Start();
|
|
|
|
// Busy wait on result to make sure both tasks are done.
|
2018-11-08 16:44:32 +00:00
|
|
|
while (result1.load() == 0 || result2.load() == 0) {
|
2015-11-14 01:20:07 +00:00
|
|
|
}
|
2018-11-09 15:51:43 +00:00
|
|
|
CancelAndWait();
|
2015-11-14 01:20:07 +00:00
|
|
|
runner1.Join();
|
|
|
|
runner2.Join();
|
2018-11-08 16:44:32 +00:00
|
|
|
EXPECT_EQ(1u, result1);
|
|
|
|
EXPECT_EQ(2u, result2);
|
2015-11-14 01:20:07 +00:00
|
|
|
}
|
|
|
|
|
2018-11-08 16:44:32 +00:00
|
|
|
TEST_F(CancelableTaskManagerTest, ThreadedMultipleTasksNotRun) {
|
|
|
|
ResultType result1{0};
|
|
|
|
ResultType result2{0};
|
|
|
|
ThreadedRunner runner1(NewTask(&result1, TestTask::kCheckNotRun));
|
|
|
|
ThreadedRunner runner2(NewTask(&result2, TestTask::kCheckNotRun));
|
2018-11-09 15:51:43 +00:00
|
|
|
CancelAndWait();
|
2015-11-14 01:20:07 +00:00
|
|
|
// Tasks are canceled, hence the runner will bail out and not update result.
|
|
|
|
runner1.Start();
|
|
|
|
runner2.Start();
|
|
|
|
runner1.Join();
|
|
|
|
runner2.Join();
|
2018-11-08 16:44:32 +00:00
|
|
|
EXPECT_EQ(0u, result1);
|
|
|
|
EXPECT_EQ(0u, result2);
|
2015-11-14 01:20:07 +00:00
|
|
|
}
|
|
|
|
|
2018-11-08 16:44:32 +00:00
|
|
|
TEST_F(CancelableTaskManagerTest, RemoveBeforeCancelAndWait) {
|
|
|
|
ResultType result1{0};
|
|
|
|
ThreadedRunner runner1(NewTask(&result1, TestTask::kCheckNotRun));
|
|
|
|
CancelableTaskManager::Id id = runner1.task_id();
|
|
|
|
EXPECT_EQ(1u, id);
|
|
|
|
EXPECT_EQ(TryAbortResult::kTaskAborted, manager()->TryAbort(id));
|
2015-11-14 01:20:07 +00:00
|
|
|
runner1.Start();
|
|
|
|
runner1.Join();
|
2018-11-09 15:51:43 +00:00
|
|
|
CancelAndWait();
|
2018-11-08 16:44:32 +00:00
|
|
|
EXPECT_EQ(0u, result1);
|
2015-11-14 01:20:07 +00:00
|
|
|
}
|
|
|
|
|
2018-11-08 16:44:32 +00:00
|
|
|
TEST_F(CancelableTaskManagerTest, RemoveAfterCancelAndWait) {
|
|
|
|
ResultType result1{0};
|
|
|
|
ThreadedRunner runner1(NewTask(&result1));
|
|
|
|
CancelableTaskManager::Id id = runner1.task_id();
|
|
|
|
EXPECT_EQ(1u, id);
|
2015-11-14 01:20:07 +00:00
|
|
|
runner1.Start();
|
|
|
|
runner1.Join();
|
2018-11-09 15:51:43 +00:00
|
|
|
CancelAndWait();
|
2018-11-08 16:44:32 +00:00
|
|
|
EXPECT_EQ(TryAbortResult::kTaskRemoved, manager()->TryAbort(id));
|
|
|
|
EXPECT_EQ(1u, result1);
|
2015-11-14 01:20:07 +00:00
|
|
|
}
|
|
|
|
|
2018-11-08 16:44:32 +00:00
|
|
|
TEST_F(CancelableTaskManagerTest, RemoveUnmanagedId) {
|
|
|
|
EXPECT_EQ(TryAbortResult::kTaskRemoved, manager()->TryAbort(1));
|
|
|
|
EXPECT_EQ(TryAbortResult::kTaskRemoved, manager()->TryAbort(2));
|
2018-11-09 15:51:43 +00:00
|
|
|
CancelAndWait();
|
2018-11-08 16:44:32 +00:00
|
|
|
EXPECT_EQ(TryAbortResult::kTaskRemoved, manager()->TryAbort(1));
|
|
|
|
EXPECT_EQ(TryAbortResult::kTaskRemoved, manager()->TryAbort(3));
|
2015-11-14 01:20:07 +00:00
|
|
|
}
|
|
|
|
|
2018-11-08 16:44:32 +00:00
|
|
|
TEST_F(CancelableTaskManagerTest, EmptyTryAbortAll) {
|
2018-11-09 15:51:43 +00:00
|
|
|
EXPECT_EQ(TryAbortResult::kTaskRemoved, TryAbortAll());
|
2017-01-04 13:45:56 +00:00
|
|
|
}
|
|
|
|
|
2018-11-08 16:44:32 +00:00
|
|
|
TEST_F(CancelableTaskManagerTest, ThreadedMultipleTasksNotRunTryAbortAll) {
|
|
|
|
ResultType result1{0};
|
|
|
|
ResultType result2{0};
|
|
|
|
ThreadedRunner runner1(NewTask(&result1, TestTask::kCheckNotRun));
|
|
|
|
ThreadedRunner runner2(NewTask(&result2, TestTask::kCheckNotRun));
|
2018-11-09 15:51:43 +00:00
|
|
|
EXPECT_EQ(TryAbortResult::kTaskAborted, TryAbortAll());
|
2017-01-04 13:45:56 +00:00
|
|
|
// Tasks are canceled, hence the runner will bail out and not update result.
|
|
|
|
runner1.Start();
|
|
|
|
runner2.Start();
|
|
|
|
runner1.Join();
|
|
|
|
runner2.Join();
|
2018-11-08 16:44:32 +00:00
|
|
|
EXPECT_EQ(0u, result1);
|
|
|
|
EXPECT_EQ(0u, result2);
|
2017-01-04 13:45:56 +00:00
|
|
|
}
|
|
|
|
|
2018-11-08 16:44:32 +00:00
|
|
|
TEST_F(CancelableTaskManagerTest, ThreadedMultipleTasksStartedTryAbortAll) {
|
|
|
|
ResultType result1{0};
|
|
|
|
ResultType result2{0};
|
2018-11-09 15:51:43 +00:00
|
|
|
ThreadedRunner runner1(NewTask(&result1, TestTask::kWaitTillCancelTriggered));
|
|
|
|
ThreadedRunner runner2(NewTask(&result2, TestTask::kWaitTillCancelTriggered));
|
2017-01-04 13:45:56 +00:00
|
|
|
runner1.Start();
|
|
|
|
// Busy wait on result to make sure task1 is done.
|
2018-11-08 16:44:32 +00:00
|
|
|
while (result1.load() == 0) {
|
2017-01-04 13:45:56 +00:00
|
|
|
}
|
2018-11-09 15:51:43 +00:00
|
|
|
// If the task saw that we triggered the cancel and finished *before* the
|
|
|
|
// actual cancel happened, we get {kTaskAborted}. Otherwise, we get
|
|
|
|
// {kTaskRunning}.
|
|
|
|
EXPECT_THAT(TryAbortAll(),
|
|
|
|
testing::AnyOf(testing::Eq(TryAbortResult::kTaskAborted),
|
|
|
|
testing::Eq(TryAbortResult::kTaskRunning)));
|
2017-01-04 13:45:56 +00:00
|
|
|
runner2.Start();
|
|
|
|
runner1.Join();
|
|
|
|
runner2.Join();
|
2018-11-08 16:44:32 +00:00
|
|
|
EXPECT_EQ(1u, result1);
|
|
|
|
EXPECT_EQ(0u, result2);
|
2017-01-04 13:45:56 +00:00
|
|
|
}
|
|
|
|
|
2015-11-14 01:20:07 +00:00
|
|
|
} // namespace internal
|
|
|
|
} // namespace v8
|