Skip to content

Commit

Permalink
Adding task runner/ message loop to tests that use IsolateHolder.
Browse files Browse the repository at this point in the history
For adding v8 heap memory dump provider, the gin::IsolateHolder needs
to have a task runner at initialization. The tests which don't
initiallize task runner when initialization are changed in this CL to
have either the message loop initializaed earlier or a dummy task
runner. Refer crrev.com/1088683003 for the dump provider.

BUG=476013

Review URL: https://codereview.chromium.org/1130433002

Cr-Commit-Position: refs/heads/master@{#328531}
  • Loading branch information
ssiddhartha authored and Commit bot committed May 6, 2015
1 parent a2969a3 commit 54d1944
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 13 deletions.
50 changes: 50 additions & 0 deletions content/test/test_blink_web_unit_test_support.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@
#include "base/files/file_util.h"
#include "base/files/scoped_temp_dir.h"
#include "base/path_service.h"
#include "base/single_thread_task_runner.h"
#include "base/strings/utf_string_conversions.h"
#include "base/thread_task_runner_handle.h"
#include "base/threading/platform_thread.h"
#include "components/scheduler/renderer/renderer_scheduler.h"
#include "components/scheduler/renderer/webthread_impl_for_renderer_scheduler.h"
#include "content/test/mock_webclipboard_impl.h"
Expand Down Expand Up @@ -41,6 +44,40 @@
#include "gin/v8_initializer.h"
#endif

namespace {

class DummyTaskRunner : public base::SingleThreadTaskRunner {
public:
DummyTaskRunner() : thread_id_(base::PlatformThread::CurrentId()) {}

bool PostDelayedTask(const tracked_objects::Location& from_here,
const base::Closure& task,
base::TimeDelta delay) override {
NOTREACHED();
return false;
}

bool PostNonNestableDelayedTask(const tracked_objects::Location& from_here,
const base::Closure& task,
base::TimeDelta delay) override {
NOTREACHED();
return false;
}

bool RunsTasksOnCurrentThread() const override {
return thread_id_ == base::PlatformThread::CurrentId();
}

protected:
~DummyTaskRunner() override {}

base::PlatformThreadId thread_id_;

DISALLOW_COPY_AND_ASSIGN(DummyTaskRunner);
};

} // namespace

namespace content {

TestBlinkWebUnitTestSupport::TestBlinkWebUnitTestSupport() {
Expand All @@ -55,10 +92,23 @@ TestBlinkWebUnitTestSupport::TestBlinkWebUnitTestSupport() {
gin::V8Initializer::LoadV8Snapshot();
#endif

scoped_refptr<base::SingleThreadTaskRunner> dummy_task_runner;
scoped_ptr<base::ThreadTaskRunnerHandle> dummy_task_runner_handle;
if (base::MessageLoopProxy::current()) {
renderer_scheduler_ = scheduler::RendererScheduler::Create();
web_thread_.reset(new scheduler::WebThreadImplForRendererScheduler(
renderer_scheduler_.get()));
} else {
// Dummy task runner is initialized here because the blink::initialize
// creates IsolateHolder which needs the current task runner handle. There
// should be no task posted to this task runner. The message loop is not
// created before this initialization because some tests need specific kinds
// of message loops, and their types are not known upfront. Some tests also
// create their own thread bundles or message loops, and doing the same in
// TestBlinkWebUnitTestSupport would introduce a conflict.
dummy_task_runner = make_scoped_refptr(new DummyTaskRunner());
dummy_task_runner_handle.reset(
new base::ThreadTaskRunnerHandle(dummy_task_runner));
}

blink::initialize(this);
Expand Down
2 changes: 0 additions & 2 deletions gin/modules/module_registry_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#include "gin/modules/module_registry.h"

#include "base/bind.h"
#include "base/message_loop/message_loop.h"
#include "gin/modules/module_registry_observer.h"
#include "gin/modules/module_runner_delegate.h"
#include "gin/public/context_holder.h"
Expand All @@ -25,7 +24,6 @@ struct TestHelper {
scope(runner.get()) {
}

base::MessageLoop message_loop;
ModuleRunnerDelegate delegate;
scoped_ptr<ShellRunner> runner;
Runner::Scope scope;
Expand Down
22 changes: 11 additions & 11 deletions gin/modules/timer_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -64,17 +64,17 @@ struct TestHelper {
result->GetWrapper(isolate));
}

void QuitSoon() {
loop.PostDelayedTask(FROM_HERE, base::MessageLoop::QuitWhenIdleClosure(),
base::TimeDelta::FromMilliseconds(0));
void QuitSoon(base::MessageLoop* message_loop) {
message_loop->PostDelayedTask(FROM_HERE,
base::MessageLoop::QuitWhenIdleClosure(),
base::TimeDelta::FromMilliseconds(0));
}

ShellRunnerDelegate delegate;
scoped_ptr<ShellRunner> runner;
Runner::Scope scope;
Handle<TimerModule> timer_module;
Handle<Result> result;
base::MessageLoop loop;
};

} // namespace
Expand All @@ -91,8 +91,8 @@ TEST_F(TimerUnittest, OneShot) {
helper.runner->Run(source, "script");
EXPECT_EQ(0, helper.result->count());

helper.QuitSoon();
helper.loop.Run();
helper.QuitSoon(&message_loop_);
message_loop_.Run();
EXPECT_EQ(1, helper.result->count());
}

Expand All @@ -107,8 +107,8 @@ TEST_F(TimerUnittest, OneShotCancel) {
helper.runner->Run(source, "script");
EXPECT_EQ(0, helper.result->count());

helper.QuitSoon();
helper.loop.Run();
helper.QuitSoon(&message_loop_);
message_loop_.Run();
EXPECT_EQ(0, helper.result->count());
}

Expand All @@ -128,7 +128,7 @@ TEST_F(TimerUnittest, Repeating) {
helper.runner->Run(source, "script");
EXPECT_EQ(0, helper.result->count());

helper.loop.Run();
message_loop_.Run();
EXPECT_EQ(3, helper.result->count());
}

Expand All @@ -143,9 +143,9 @@ TEST_F(TimerUnittest, TimerCallbackToDestroyedRunner) {
EXPECT_EQ(0, helper.result->count());

// Destroy runner, which should destroy the timer object we created.
helper.QuitSoon();
helper.QuitSoon(&message_loop_);
helper.runner.reset(NULL);
helper.loop.Run();
message_loop_.Run();

// Timer should not have run because it was deleted.
EXPECT_EQ(0, helper.result->count());
Expand Down
2 changes: 2 additions & 0 deletions gin/shell_runner_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "gin/shell_runner.h"

#include "base/compiler_specific.h"
#include "base/message_loop/message_loop.h"
#include "gin/array_buffer.h"
#include "gin/converter.h"
#include "gin/public/isolate_holder.h"
Expand All @@ -23,6 +24,7 @@ using v8::String;
namespace gin {

TEST(RunnerTest, Run) {
base::MessageLoop message_loop;
std::string source = "this.result = 'PASS';\n";

#ifdef V8_USE_EXTERNAL_STARTUP_DATA
Expand Down
2 changes: 2 additions & 0 deletions gin/test/v8_test.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "base/basictypes.h"
#include "base/compiler_specific.h"
#include "base/memory/scoped_ptr.h"
#include "base/message_loop/message_loop.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "v8/include/v8.h"

Expand All @@ -26,6 +27,7 @@ class V8Test : public testing::Test {
void TearDown() override;

protected:
base::MessageLoop message_loop_;
scoped_ptr<IsolateHolder> instance_;
v8::Persistent<v8::Context> context_;

Expand Down

0 comments on commit 54d1944

Please sign in to comment.