Chromium Code Reviews| Index: test/unittests/compiler-dispatcher/compiler-dispatcher-unittest.cc |
| diff --git a/test/unittests/compiler-dispatcher/compiler-dispatcher-unittest.cc b/test/unittests/compiler-dispatcher/compiler-dispatcher-unittest.cc |
| index 3f84b6859936c5f31528df3b65819ea1c2705ec0..4acd5171027a1cf86d1abda64e577148d6e886a1 100644 |
| --- a/test/unittests/compiler-dispatcher/compiler-dispatcher-unittest.cc |
| +++ b/test/unittests/compiler-dispatcher/compiler-dispatcher-unittest.cc |
| @@ -4,6 +4,7 @@ |
| #include <memory> |
| +#include "include/v8-platform.h" |
| #include "src/compiler-dispatcher/compiler-dispatcher.h" |
| #include "src/flags.h" |
| #include "src/handles.h" |
| @@ -17,14 +18,67 @@ namespace internal { |
| typedef TestWithContext CompilerDispatcherTest; |
| +namespace { |
| + |
| +class MockPlatform : public v8::Platform { |
| + public: |
| + MockPlatform() : task_(nullptr), time_(0.0) {} |
| + ~MockPlatform() override = default; |
| + |
| + void CallOnBackgroundThread(Task* task, |
| + ExpectedRuntime expected_runtime) override { |
| + UNREACHABLE(); |
| + } |
| + |
| + void CallOnForegroundThread(v8::Isolate* isolate, Task* task) override { |
| + UNREACHABLE(); |
| + } |
| + |
| + void CallDelayedOnForegroundThread(v8::Isolate* isolate, Task* task, |
| + double delay_in_seconds) override { |
| + UNREACHABLE(); |
| + } |
| + |
| + void CallIdleOnForegroundThread(v8::Isolate* isolate, |
| + IdleTask* task) override { |
| + task_ = task; |
| + } |
| + |
| + bool IdleTasksEnabled(v8::Isolate* isolate) override { return true; } |
| + |
| + double MonotonicallyIncreasingTime() override { return time_; } |
| + |
| + void RunIdleTask(double deadline_in_seconds) { |
| + ASSERT_TRUE(task_ != nullptr); |
| + IdleTask* task = task_; |
| + task_ = nullptr; |
| + task->Run(deadline_in_seconds); |
| + delete task; |
| + } |
| + |
| + bool IdleTaskPending() const { return !!task_; } |
| + |
| + void set_time(double time) { time_ = time; } |
| + |
| + private: |
| + IdleTask* task_; |
| + double time_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(MockPlatform); |
| +}; |
| + |
| +} // namespace |
| + |
| TEST_F(CompilerDispatcherTest, Construct) { |
| + std::unique_ptr<MockPlatform> platform(new MockPlatform); |
|
vogelheim
2016/12/12 17:51:32
[Here & below:] Why not just have stack allocated
jochen (gone - plz use gerrit)
2016/12/15 13:54:34
done
|
| std::unique_ptr<CompilerDispatcher> dispatcher( |
| - new CompilerDispatcher(i_isolate(), FLAG_stack_size)); |
| + new CompilerDispatcher(i_isolate(), platform.get(), FLAG_stack_size)); |
| } |
| TEST_F(CompilerDispatcherTest, IsEnqueued) { |
| + std::unique_ptr<MockPlatform> platform(new MockPlatform); |
| std::unique_ptr<CompilerDispatcher> dispatcher( |
| - new CompilerDispatcher(i_isolate(), FLAG_stack_size)); |
| + new CompilerDispatcher(i_isolate(), platform.get(), FLAG_stack_size)); |
| const char script[] = |
| "function g() { var y = 1; function f(x) { return x * y }; return f; } " |
| @@ -40,8 +94,9 @@ TEST_F(CompilerDispatcherTest, IsEnqueued) { |
| } |
| TEST_F(CompilerDispatcherTest, FinishNow) { |
| + std::unique_ptr<MockPlatform> platform(new MockPlatform); |
| std::unique_ptr<CompilerDispatcher> dispatcher( |
| - new CompilerDispatcher(i_isolate(), FLAG_stack_size)); |
| + new CompilerDispatcher(i_isolate(), platform.get(), FLAG_stack_size)); |
| const char script[] = |
| "function g() { var y = 1; function f(x) { return x * y }; return f; } " |
| @@ -57,5 +112,58 @@ TEST_F(CompilerDispatcherTest, FinishNow) { |
| ASSERT_TRUE(shared->HasBaselineCode()); |
| } |
| +TEST_F(CompilerDispatcherTest, IdleTask) { |
| + std::unique_ptr<MockPlatform> platform(new MockPlatform); |
| + std::unique_ptr<CompilerDispatcher> dispatcher( |
| + new CompilerDispatcher(i_isolate(), platform.get(), FLAG_stack_size)); |
| + |
| + const char script[] = |
| + "function g() { var y = 1; function f(x) { return x * y }; return f; } " |
| + "g();"; |
| + Handle<JSFunction> f = Handle<JSFunction>::cast(RunJS(isolate(), script)); |
| + Handle<SharedFunctionInfo> shared(f->shared(), i_isolate()); |
| + |
| + ASSERT_FALSE(platform->IdleTaskPending()); |
| + ASSERT_TRUE(dispatcher->Enqueue(shared)); |
| + ASSERT_TRUE(platform->IdleTaskPending()); |
| + |
| + // Since time doesn't progress on the MockPlatform, this is enough idle time |
| + // to finish compiling the function. |
| + platform->RunIdleTask(1000.0); |
| + |
| + ASSERT_FALSE(dispatcher->IsEnqueued(shared)); |
| + ASSERT_TRUE(shared->HasBaselineCode()); |
| +} |
| + |
| +TEST_F(CompilerDispatcherTest, IdleTaskException) { |
| + std::unique_ptr<MockPlatform> platform(new MockPlatform); |
| + std::unique_ptr<CompilerDispatcher> dispatcher( |
| + new CompilerDispatcher(i_isolate(), platform.get(), 50)); |
| + |
| + std::string script("function g() { function f(x) { var a = "); |
| + for (int i = 0; i < 1000; i++) { |
| + script += "'x' + "; |
| + } |
| + script += " 'x'; }; return f; } g();"; |
| + Handle<JSFunction> f = |
| + Handle<JSFunction>::cast(RunJS(isolate(), script.c_str())); |
| + Handle<SharedFunctionInfo> shared(f->shared(), i_isolate()); |
| + |
| + ASSERT_FALSE(platform->IdleTaskPending()); |
| + ASSERT_TRUE(dispatcher->Enqueue(shared)); |
| + ASSERT_TRUE(platform->IdleTaskPending()); |
| + |
| + // Idle tasks shouldn't leave exceptions behind. |
| + v8::TryCatch try_catch(isolate()); |
| + |
| + // Since time doesn't progress on the MockPlatform, this is enough idle time |
| + // to finish compiling the function. |
| + platform->RunIdleTask(1000.0); |
| + |
| + ASSERT_FALSE(dispatcher->IsEnqueued(shared)); |
| + ASSERT_FALSE(shared->HasBaselineCode()); |
| + ASSERT_FALSE(try_catch.HasCaught()); |
| +} |
| + |
|
marja
2016/12/13 09:09:01
Can you add the following tests
Test 1:
- Do some
|
| } // namespace internal |
| } // namespace v8 |