2014-09-01 07:13:55 +00:00
|
|
|
// Copyright 2014 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/libplatform/default-platform.h"
|
2017-11-15 12:07:22 +00:00
|
|
|
#include "src/base/platform/semaphore.h"
|
2017-08-25 07:12:51 +00:00
|
|
|
#include "src/base/platform/time.h"
|
2014-09-01 07:13:55 +00:00
|
|
|
#include "testing/gmock/include/gmock/gmock.h"
|
|
|
|
|
|
|
|
using testing::InSequence;
|
|
|
|
using testing::StrictMock;
|
|
|
|
|
|
|
|
namespace v8 {
|
|
|
|
namespace platform {
|
2017-09-28 17:55:52 +00:00
|
|
|
namespace default_platform_unittest {
|
2014-09-01 07:13:55 +00:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
struct MockTask : public Task {
|
2018-09-14 15:59:45 +00:00
|
|
|
// See issue v8:8185
|
|
|
|
~MockTask() /* override */ { Die(); }
|
2020-06-10 11:08:40 +00:00
|
|
|
MOCK_METHOD(void, Run, (), (override));
|
|
|
|
MOCK_METHOD(void, Die, ());
|
2014-09-01 07:13:55 +00:00
|
|
|
};
|
|
|
|
|
2017-01-03 08:41:40 +00:00
|
|
|
struct MockIdleTask : public IdleTask {
|
2018-09-14 15:59:45 +00:00
|
|
|
// See issue v8:8185
|
|
|
|
~MockIdleTask() /* override */ { Die(); }
|
2020-06-10 11:08:40 +00:00
|
|
|
MOCK_METHOD(void, Run, (double deadline_in_seconds), (override));
|
|
|
|
MOCK_METHOD(void, Die, ());
|
2017-01-03 08:41:40 +00:00
|
|
|
};
|
2015-06-17 12:09:34 +00:00
|
|
|
|
|
|
|
class DefaultPlatformWithMockTime : public DefaultPlatform {
|
|
|
|
public:
|
2020-05-05 16:43:53 +00:00
|
|
|
explicit DefaultPlatformWithMockTime(int thread_pool_size = 0)
|
|
|
|
: DefaultPlatform(thread_pool_size, IdleTaskSupport::kEnabled, nullptr) {
|
2017-11-15 12:07:22 +00:00
|
|
|
mock_time_ = 0.0;
|
|
|
|
SetTimeFunctionForTesting([]() { return mock_time_; });
|
2017-08-25 07:12:51 +00:00
|
|
|
}
|
2017-11-15 12:07:22 +00:00
|
|
|
void IncreaseTime(double seconds) { mock_time_ += seconds; }
|
2015-06-17 12:09:34 +00:00
|
|
|
|
|
|
|
private:
|
2017-11-15 12:07:22 +00:00
|
|
|
static double mock_time_;
|
2015-06-17 12:09:34 +00:00
|
|
|
};
|
|
|
|
|
2017-11-15 12:07:22 +00:00
|
|
|
double DefaultPlatformWithMockTime::mock_time_ = 0.0;
|
|
|
|
|
2019-10-21 12:55:31 +00:00
|
|
|
template <typename Platform>
|
|
|
|
class PlatformTest : public ::testing::Test {
|
|
|
|
public:
|
|
|
|
Isolate* isolate() { return reinterpret_cast<Isolate*>(dummy_); }
|
2019-10-18 13:50:42 +00:00
|
|
|
|
2019-10-21 12:55:31 +00:00
|
|
|
Platform* platform() { return &platform_; }
|
|
|
|
|
|
|
|
std::shared_ptr<TaskRunner> task_runner() {
|
|
|
|
if (!task_runner_) {
|
|
|
|
task_runner_ = platform_.GetForegroundTaskRunner(isolate());
|
|
|
|
}
|
|
|
|
DCHECK_NOT_NULL(task_runner_);
|
|
|
|
return task_runner_;
|
|
|
|
}
|
2019-10-18 13:50:42 +00:00
|
|
|
|
2019-10-21 12:55:31 +00:00
|
|
|
// These methods take ownership of the task. Tests might still reference them,
|
|
|
|
// if the tasks are expected to still exist.
|
|
|
|
void CallOnForegroundThread(Task* task) {
|
|
|
|
task_runner()->PostTask(std::unique_ptr<Task>(task));
|
|
|
|
}
|
2020-01-24 00:03:09 +00:00
|
|
|
void CallNonNestableOnForegroundThread(Task* task) {
|
|
|
|
task_runner()->PostNonNestableTask(std::unique_ptr<Task>(task));
|
|
|
|
}
|
2019-10-21 12:55:31 +00:00
|
|
|
void CallDelayedOnForegroundThread(Task* task, double delay_in_seconds) {
|
|
|
|
task_runner()->PostDelayedTask(std::unique_ptr<Task>(task),
|
|
|
|
delay_in_seconds);
|
|
|
|
}
|
|
|
|
void CallIdleOnForegroundThread(IdleTask* task) {
|
|
|
|
task_runner()->PostIdleTask(std::unique_ptr<IdleTask>(task));
|
|
|
|
}
|
2019-10-18 13:50:42 +00:00
|
|
|
|
2019-10-21 12:55:31 +00:00
|
|
|
bool PumpMessageLoop() { return platform_.PumpMessageLoop(isolate()); }
|
2019-10-18 13:50:42 +00:00
|
|
|
|
2019-10-21 12:55:31 +00:00
|
|
|
private:
|
|
|
|
Platform platform_;
|
|
|
|
InSequence in_sequence_;
|
|
|
|
std::shared_ptr<TaskRunner> task_runner_;
|
|
|
|
|
|
|
|
int dummy_ = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
class DefaultPlatformTest : public PlatformTest<DefaultPlatform> {};
|
|
|
|
class DefaultPlatformTestWithMockTime
|
|
|
|
: public PlatformTest<DefaultPlatformWithMockTime> {};
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
TEST_F(DefaultPlatformTest, PumpMessageLoop) {
|
|
|
|
EXPECT_FALSE(platform()->PumpMessageLoop(isolate()));
|
2014-09-01 07:13:55 +00:00
|
|
|
|
|
|
|
StrictMock<MockTask>* task = new StrictMock<MockTask>;
|
2019-10-21 12:55:31 +00:00
|
|
|
CallOnForegroundThread(task);
|
2014-09-01 07:13:55 +00:00
|
|
|
EXPECT_CALL(*task, Run());
|
|
|
|
EXPECT_CALL(*task, Die());
|
2019-10-21 12:55:31 +00:00
|
|
|
EXPECT_TRUE(PumpMessageLoop());
|
|
|
|
EXPECT_FALSE(PumpMessageLoop());
|
2014-09-01 07:13:55 +00:00
|
|
|
}
|
|
|
|
|
2019-10-21 12:55:31 +00:00
|
|
|
TEST_F(DefaultPlatformTest, PumpMessageLoopWithTaskRunner) {
|
2017-11-15 12:07:22 +00:00
|
|
|
std::shared_ptr<TaskRunner> taskrunner =
|
2019-10-21 12:55:31 +00:00
|
|
|
platform()->GetForegroundTaskRunner(isolate());
|
|
|
|
EXPECT_FALSE(PumpMessageLoop());
|
2017-11-15 12:07:22 +00:00
|
|
|
|
|
|
|
StrictMock<MockTask>* task = new StrictMock<MockTask>;
|
|
|
|
taskrunner->PostTask(std::unique_ptr<Task>(task));
|
|
|
|
EXPECT_CALL(*task, Run());
|
|
|
|
EXPECT_CALL(*task, Die());
|
2019-10-21 12:55:31 +00:00
|
|
|
EXPECT_TRUE(PumpMessageLoop());
|
|
|
|
EXPECT_FALSE(PumpMessageLoop());
|
2019-10-21 11:37:20 +00:00
|
|
|
}
|
|
|
|
|
2020-01-24 00:03:09 +00:00
|
|
|
TEST_F(DefaultPlatformTest, PumpMessageLoopNested) {
|
|
|
|
EXPECT_FALSE(PumpMessageLoop());
|
|
|
|
|
|
|
|
StrictMock<MockTask>* nestable_task1 = new StrictMock<MockTask>;
|
|
|
|
StrictMock<MockTask>* non_nestable_task2 = new StrictMock<MockTask>;
|
|
|
|
StrictMock<MockTask>* nestable_task3 = new StrictMock<MockTask>;
|
|
|
|
StrictMock<MockTask>* non_nestable_task4 = new StrictMock<MockTask>;
|
|
|
|
CallOnForegroundThread(nestable_task1);
|
|
|
|
CallNonNestableOnForegroundThread(non_nestable_task2);
|
|
|
|
CallOnForegroundThread(nestable_task3);
|
|
|
|
CallNonNestableOnForegroundThread(non_nestable_task4);
|
|
|
|
|
|
|
|
// Nestable tasks are FIFO; non-nestable tasks are FIFO. A task being
|
|
|
|
// non-nestable may cause it to be executed later, but not earlier.
|
|
|
|
EXPECT_CALL(*nestable_task1, Run).WillOnce([this]() {
|
|
|
|
EXPECT_TRUE(PumpMessageLoop());
|
|
|
|
});
|
|
|
|
EXPECT_CALL(*nestable_task3, Run());
|
|
|
|
EXPECT_CALL(*nestable_task3, Die());
|
|
|
|
EXPECT_CALL(*nestable_task1, Die());
|
|
|
|
EXPECT_TRUE(PumpMessageLoop());
|
|
|
|
EXPECT_CALL(*non_nestable_task2, Run());
|
|
|
|
EXPECT_CALL(*non_nestable_task2, Die());
|
|
|
|
EXPECT_TRUE(PumpMessageLoop());
|
|
|
|
EXPECT_CALL(*non_nestable_task4, Run());
|
|
|
|
EXPECT_CALL(*non_nestable_task4, Die());
|
|
|
|
EXPECT_TRUE(PumpMessageLoop());
|
|
|
|
|
|
|
|
EXPECT_FALSE(PumpMessageLoop());
|
|
|
|
}
|
|
|
|
|
2019-10-21 12:55:31 +00:00
|
|
|
TEST_F(DefaultPlatformTestWithMockTime, PumpMessageLoopDelayed) {
|
|
|
|
EXPECT_FALSE(PumpMessageLoop());
|
2019-10-21 11:37:20 +00:00
|
|
|
|
|
|
|
StrictMock<MockTask>* task1 = new StrictMock<MockTask>;
|
|
|
|
StrictMock<MockTask>* task2 = new StrictMock<MockTask>;
|
2019-10-21 12:55:31 +00:00
|
|
|
CallDelayedOnForegroundThread(task2, 100);
|
|
|
|
CallDelayedOnForegroundThread(task1, 10);
|
2019-10-21 11:37:20 +00:00
|
|
|
|
2019-10-21 12:55:31 +00:00
|
|
|
EXPECT_FALSE(PumpMessageLoop());
|
2019-10-21 11:37:20 +00:00
|
|
|
|
2019-10-21 12:55:31 +00:00
|
|
|
platform()->IncreaseTime(11);
|
2019-10-21 11:37:20 +00:00
|
|
|
EXPECT_CALL(*task1, Run());
|
|
|
|
EXPECT_CALL(*task1, Die());
|
2019-10-21 12:55:31 +00:00
|
|
|
EXPECT_TRUE(PumpMessageLoop());
|
2019-10-21 11:37:20 +00:00
|
|
|
|
2019-10-21 12:55:31 +00:00
|
|
|
EXPECT_FALSE(PumpMessageLoop());
|
2019-10-21 11:37:20 +00:00
|
|
|
|
2019-10-21 12:55:31 +00:00
|
|
|
platform()->IncreaseTime(90);
|
2019-10-21 11:37:20 +00:00
|
|
|
EXPECT_CALL(*task2, Run());
|
|
|
|
EXPECT_CALL(*task2, Die());
|
2019-10-21 12:55:31 +00:00
|
|
|
EXPECT_TRUE(PumpMessageLoop());
|
2015-06-17 12:09:34 +00:00
|
|
|
}
|
|
|
|
|
2019-10-21 12:55:31 +00:00
|
|
|
TEST_F(DefaultPlatformTestWithMockTime, PumpMessageLoopNoStarvation) {
|
|
|
|
EXPECT_FALSE(PumpMessageLoop());
|
2015-06-17 12:09:34 +00:00
|
|
|
|
|
|
|
StrictMock<MockTask>* task1 = new StrictMock<MockTask>;
|
|
|
|
StrictMock<MockTask>* task2 = new StrictMock<MockTask>;
|
|
|
|
StrictMock<MockTask>* task3 = new StrictMock<MockTask>;
|
2019-10-21 12:55:31 +00:00
|
|
|
CallOnForegroundThread(task1);
|
|
|
|
CallDelayedOnForegroundThread(task2, 10);
|
|
|
|
platform()->IncreaseTime(11);
|
2015-06-17 12:09:34 +00:00
|
|
|
|
|
|
|
EXPECT_CALL(*task1, Run());
|
|
|
|
EXPECT_CALL(*task1, Die());
|
2019-10-21 12:55:31 +00:00
|
|
|
EXPECT_TRUE(PumpMessageLoop());
|
2015-06-17 12:09:34 +00:00
|
|
|
|
2019-10-21 12:55:31 +00:00
|
|
|
CallOnForegroundThread(task3);
|
2015-06-17 12:09:34 +00:00
|
|
|
|
|
|
|
EXPECT_CALL(*task2, Run());
|
|
|
|
EXPECT_CALL(*task2, Die());
|
2019-10-21 12:55:31 +00:00
|
|
|
EXPECT_TRUE(PumpMessageLoop());
|
2015-06-17 12:09:34 +00:00
|
|
|
EXPECT_CALL(*task3, Run());
|
|
|
|
EXPECT_CALL(*task3, Die());
|
2019-10-21 12:55:31 +00:00
|
|
|
EXPECT_TRUE(PumpMessageLoop());
|
2015-06-17 12:09:34 +00:00
|
|
|
}
|
|
|
|
|
2019-10-21 12:55:31 +00:00
|
|
|
TEST_F(DefaultPlatformTestWithMockTime,
|
|
|
|
PendingDelayedTasksAreDestroyedOnShutdown) {
|
|
|
|
StrictMock<MockTask>* task = new StrictMock<MockTask>;
|
|
|
|
CallDelayedOnForegroundThread(task, 10);
|
|
|
|
EXPECT_CALL(*task, Die());
|
2015-07-02 14:37:23 +00:00
|
|
|
}
|
|
|
|
|
2019-10-21 12:55:31 +00:00
|
|
|
TEST_F(DefaultPlatformTestWithMockTime, RunIdleTasks) {
|
2017-01-03 08:41:40 +00:00
|
|
|
StrictMock<MockIdleTask>* task = new StrictMock<MockIdleTask>;
|
2019-10-21 12:55:31 +00:00
|
|
|
CallIdleOnForegroundThread(task);
|
2017-01-03 08:41:40 +00:00
|
|
|
EXPECT_CALL(*task, Run(42.0 + 23.0));
|
|
|
|
EXPECT_CALL(*task, Die());
|
2019-10-21 12:55:31 +00:00
|
|
|
platform()->IncreaseTime(23.0);
|
|
|
|
platform()->RunIdleTasks(isolate(), 42.0);
|
2017-01-03 08:41:40 +00:00
|
|
|
}
|
|
|
|
|
2019-10-21 12:55:31 +00:00
|
|
|
TEST_F(DefaultPlatformTestWithMockTime,
|
|
|
|
PendingIdleTasksAreDestroyedOnShutdown) {
|
2017-11-15 12:07:22 +00:00
|
|
|
StrictMock<MockIdleTask>* task = new StrictMock<MockIdleTask>;
|
2019-10-21 12:55:31 +00:00
|
|
|
CallIdleOnForegroundThread(task);
|
2017-11-15 12:07:22 +00:00
|
|
|
EXPECT_CALL(*task, Die());
|
2017-01-03 08:41:40 +00:00
|
|
|
}
|
2015-07-02 14:37:23 +00:00
|
|
|
|
2017-11-15 12:07:22 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
class TestBackgroundTask : public Task {
|
|
|
|
public:
|
|
|
|
explicit TestBackgroundTask(base::Semaphore* sem, bool* executed)
|
|
|
|
: sem_(sem), executed_(executed) {}
|
|
|
|
|
2018-09-14 15:59:45 +00:00
|
|
|
~TestBackgroundTask() override { Die(); }
|
2020-06-10 11:08:40 +00:00
|
|
|
MOCK_METHOD(void, Die, ());
|
2017-11-15 12:07:22 +00:00
|
|
|
|
2018-09-14 15:59:45 +00:00
|
|
|
void Run() override {
|
2017-11-15 12:07:22 +00:00
|
|
|
*executed_ = true;
|
|
|
|
sem_->Signal();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
base::Semaphore* sem_;
|
|
|
|
bool* executed_;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2019-10-21 12:55:31 +00:00
|
|
|
TEST(CustomDefaultPlatformTest, RunBackgroundTask) {
|
2020-05-05 16:43:53 +00:00
|
|
|
DefaultPlatform platform(1);
|
2017-11-15 12:07:22 +00:00
|
|
|
|
|
|
|
base::Semaphore sem(0);
|
|
|
|
bool task_executed = false;
|
|
|
|
StrictMock<TestBackgroundTask>* task =
|
|
|
|
new StrictMock<TestBackgroundTask>(&sem, &task_executed);
|
|
|
|
EXPECT_CALL(*task, Die());
|
2018-04-30 16:52:06 +00:00
|
|
|
platform.CallOnWorkerThread(std::unique_ptr<Task>(task));
|
2017-11-15 12:07:22 +00:00
|
|
|
EXPECT_TRUE(sem.WaitFor(base::TimeDelta::FromSeconds(1)));
|
|
|
|
EXPECT_TRUE(task_executed);
|
|
|
|
}
|
|
|
|
|
2019-10-21 12:55:31 +00:00
|
|
|
TEST(CustomDefaultPlatformTest, PostForegroundTaskAfterPlatformTermination) {
|
2017-11-15 12:07:22 +00:00
|
|
|
std::shared_ptr<TaskRunner> foreground_taskrunner;
|
|
|
|
{
|
2020-05-05 16:43:53 +00:00
|
|
|
DefaultPlatformWithMockTime platform(1);
|
2018-04-30 16:52:06 +00:00
|
|
|
|
2017-11-15 12:07:22 +00:00
|
|
|
int dummy;
|
|
|
|
Isolate* isolate = reinterpret_cast<Isolate*>(&dummy);
|
|
|
|
|
|
|
|
foreground_taskrunner = platform.GetForegroundTaskRunner(isolate);
|
|
|
|
}
|
2018-04-30 16:52:06 +00:00
|
|
|
// It should still be possible to post foreground tasks, even when the
|
|
|
|
// platform does not exist anymore.
|
2017-11-15 12:07:22 +00:00
|
|
|
StrictMock<MockTask>* task1 = new StrictMock<MockTask>;
|
|
|
|
EXPECT_CALL(*task1, Die());
|
|
|
|
foreground_taskrunner->PostTask(std::unique_ptr<Task>(task1));
|
|
|
|
|
|
|
|
StrictMock<MockTask>* task2 = new StrictMock<MockTask>;
|
|
|
|
EXPECT_CALL(*task2, Die());
|
|
|
|
foreground_taskrunner->PostDelayedTask(std::unique_ptr<Task>(task2), 10);
|
|
|
|
|
|
|
|
StrictMock<MockIdleTask>* task3 = new StrictMock<MockIdleTask>;
|
|
|
|
EXPECT_CALL(*task3, Die());
|
|
|
|
foreground_taskrunner->PostIdleTask(std::unique_ptr<IdleTask>(task3));
|
|
|
|
}
|
|
|
|
|
2017-09-28 17:55:52 +00:00
|
|
|
} // namespace default_platform_unittest
|
2014-09-01 07:13:55 +00:00
|
|
|
} // namespace platform
|
|
|
|
} // namespace v8
|