Commit 4edcff90 authored by bmeurer@chromium.org's avatar bmeurer@chromium.org

Refactor libplatform unit tests to use GTest/GMock.

Also migrate them to src.

BUG=v8:3489
LOG=n
R=svenpanne@chromium.org

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

git-svn-id: https://v8.googlecode.com/svn/branches/bleeding_edge@23541 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
parent 6cd03e7f
......@@ -11,6 +11,7 @@
'../samples/samples.gyp:*',
'../src/base/base.gyp:*',
'../src/d8.gyp:d8',
'../src/libplatform/libplatform.gyp:libplatform-unittests',
'../test/cctest/cctest.gyp:*',
'../test/compiler-unittests/compiler-unittests.gyp:*',
'../test/heap-unittests/heap-unittests.gyp:*',
......
// 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"
#include "testing/gmock/include/gmock/gmock.h"
using testing::InSequence;
using testing::StrictMock;
namespace v8 {
namespace platform {
namespace {
struct MockTask : public Task {
virtual ~MockTask() { Die(); }
MOCK_METHOD0(Run, void());
MOCK_METHOD0(Die, void());
};
} // namespace
TEST(DefaultPlatformTest, PumpMessageLoop) {
InSequence s;
int dummy;
Isolate* isolate = reinterpret_cast<Isolate*>(&dummy);
DefaultPlatform platform;
EXPECT_FALSE(platform.PumpMessageLoop(isolate));
StrictMock<MockTask>* task = new StrictMock<MockTask>;
platform.CallOnForegroundThread(isolate, task);
EXPECT_CALL(*task, Run());
EXPECT_CALL(*task, Die());
EXPECT_TRUE(platform.PumpMessageLoop(isolate));
EXPECT_FALSE(platform.PumpMessageLoop(isolate));
}
} // namespace platform
} // namespace v8
# 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.
{
'variables': {
'v8_code': 1,
},
'includes': ['../../build/toolchain.gypi', '../../build/features.gypi'],
'targets': [
{
'target_name': 'libplatform-unittests',
'type': 'executable',
'dependencies': [
'../../testing/gtest.gyp:gtest',
'../../testing/gmock.gyp:gmock',
'../../testing/gmock.gyp:gmock_main',
'../../tools/gyp/v8.gyp:v8_libplatform',
],
'include_dirs': [
'../..',
],
'sources': [ ### gcmole(all) ###
'default-platform-unittest.cc',
'task-queue-unittest.cc',
'worker-thread-unittest.cc',
],
'conditions': [
['os_posix == 1', {
# TODO(svenpanne): This is a temporary work-around to fix the warnings
# that show up because we use -std=gnu++0x instead of -std=c++11.
'cflags!': [
'-pedantic',
],
}],
],
},
],
}
// 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 "include/v8-platform.h"
#include "src/base/platform/platform.h"
#include "src/libplatform/task-queue.h"
#include "testing/gmock/include/gmock/gmock.h"
using testing::InSequence;
using testing::IsNull;
using testing::StrictMock;
namespace v8 {
namespace platform {
namespace {
struct MockTask : public Task {
MOCK_METHOD0(Run, void());
};
class TaskQueueThread V8_FINAL : public base::Thread {
public:
explicit TaskQueueThread(TaskQueue* queue)
: Thread(Options("libplatform TaskQueueThread")), queue_(queue) {}
virtual void Run() V8_OVERRIDE { EXPECT_THAT(queue_->GetNext(), IsNull()); }
private:
TaskQueue* queue_;
};
} // namespace
TEST(TaskQueueTest, Basic) {
TaskQueue queue;
MockTask task;
queue.Append(&task);
EXPECT_EQ(&task, queue.GetNext());
queue.Terminate();
EXPECT_THAT(queue.GetNext(), IsNull());
}
TEST(TaskQueueTest, TerminateMultipleReaders) {
TaskQueue queue;
TaskQueueThread thread1(&queue);
TaskQueueThread thread2(&queue);
thread1.Start();
thread2.Start();
queue.Terminate();
thread1.Join();
thread2.Join();
}
} // namespace platform
} // namespace v8
// 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 "include/v8-platform.h"
#include "src/libplatform/task-queue.h"
#include "src/libplatform/worker-thread.h"
#include "testing/gmock/include/gmock/gmock.h"
using testing::InSequence;
using testing::IsNull;
using testing::StrictMock;
namespace v8 {
namespace platform {
namespace {
struct MockTask : public Task {
virtual ~MockTask() { Die(); }
MOCK_METHOD0(Run, void());
MOCK_METHOD0(Die, void());
};
} // namespace
TEST(WorkerThreadTest, Basic) {
static const size_t kNumTasks = 10;
TaskQueue queue;
for (size_t i = 0; i < kNumTasks; ++i) {
InSequence s;
StrictMock<MockTask>* task = new StrictMock<MockTask>;
EXPECT_CALL(*task, Run());
EXPECT_CALL(*task, Die());
queue.Append(task);
}
WorkerThread thread1(&queue);
WorkerThread thread2(&queue);
// TaskQueue DCHECKS that it's empty in its destructor.
queue.Terminate();
}
} // namespace platform
} // namespace v8
......@@ -127,9 +127,6 @@
'test-heap.cc',
'test-heap-profiler.cc',
'test-hydrogen-types.cc',
'test-libplatform-default-platform.cc',
'test-libplatform-task-queue.cc',
'test-libplatform-worker-thread.cc',
'test-list.cc',
'test-liveedit.cc',
'test-lockers.cc',
......
// 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/v8.h"
#include "src/libplatform/default-platform.h"
#include "test/cctest/cctest.h"
#include "test/cctest/test-libplatform.h"
using namespace v8::internal;
using namespace v8::platform;
TEST(DefaultPlatformMessagePump) {
TaskCounter task_counter;
DefaultPlatform platform;
TestTask* task = new TestTask(&task_counter, true);
CHECK(!platform.PumpMessageLoop(CcTest::isolate()));
platform.CallOnForegroundThread(CcTest::isolate(), task);
CHECK_EQ(1, task_counter.GetCount());
CHECK(platform.PumpMessageLoop(CcTest::isolate()));
CHECK_EQ(0, task_counter.GetCount());
CHECK(!platform.PumpMessageLoop(CcTest::isolate()));
}
// Copyright 2013 the V8 project authors. All rights reserved.
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following
// disclaimer in the documentation and/or other materials provided
// with the distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived
// from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "src/v8.h"
#include "src/libplatform/task-queue.h"
#include "test/cctest/cctest.h"
#include "test/cctest/test-libplatform.h"
using namespace v8::internal;
using namespace v8::platform;
TEST(TaskQueueBasic) {
TaskCounter task_counter;
TaskQueue queue;
TestTask* task = new TestTask(&task_counter);
queue.Append(task);
CHECK_EQ(1, task_counter.GetCount());
CHECK_EQ(task, queue.GetNext());
delete task;
CHECK_EQ(0, task_counter.GetCount());
queue.Terminate();
CHECK_EQ(NULL, queue.GetNext());
}
class ReadQueueTask : public TestTask {
public:
ReadQueueTask(TaskCounter* task_counter, TaskQueue* queue)
: TestTask(task_counter, true), queue_(queue) {}
virtual ~ReadQueueTask() {}
virtual void Run() V8_OVERRIDE {
TestTask::Run();
CHECK_EQ(NULL, queue_->GetNext());
}
private:
TaskQueue* queue_;
DISALLOW_COPY_AND_ASSIGN(ReadQueueTask);
};
TEST(TaskQueueTerminateMultipleReaders) {
TaskQueue queue;
TaskCounter task_counter;
ReadQueueTask* read1 = new ReadQueueTask(&task_counter, &queue);
ReadQueueTask* read2 = new ReadQueueTask(&task_counter, &queue);
TestWorkerThread thread1(read1);
TestWorkerThread thread2(read2);
thread1.Start();
thread2.Start();
CHECK_EQ(2, task_counter.GetCount());
thread1.Signal();
thread2.Signal();
queue.Terminate();
thread1.Join();
thread2.Join();
CHECK_EQ(0, task_counter.GetCount());
}
// Copyright 2013 the V8 project authors. All rights reserved.
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following
// disclaimer in the documentation and/or other materials provided
// with the distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived
// from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "src/v8.h"
#include "src/libplatform/task-queue.h"
#include "src/libplatform/worker-thread.h"
#include "test/cctest/cctest.h"
#include "test/cctest/test-libplatform.h"
using namespace v8::internal;
using namespace v8::platform;
TEST(WorkerThread) {
TaskQueue queue;
TaskCounter task_counter;
TestTask* task1 = new TestTask(&task_counter, true);
TestTask* task2 = new TestTask(&task_counter, true);
TestTask* task3 = new TestTask(&task_counter, true);
TestTask* task4 = new TestTask(&task_counter, true);
WorkerThread* thread1 = new WorkerThread(&queue);
WorkerThread* thread2 = new WorkerThread(&queue);
CHECK_EQ(4, task_counter.GetCount());
queue.Append(task1);
queue.Append(task2);
queue.Append(task3);
queue.Append(task4);
// TaskQueue DCHECKs that it is empty in its destructor.
queue.Terminate();
delete thread1;
delete thread2;
CHECK_EQ(0, task_counter.GetCount());
}
// Copyright 2013 the V8 project authors. All rights reserved.
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following
// disclaimer in the documentation and/or other materials provided
// with the distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived
// from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#ifndef TEST_LIBPLATFORM_H_
#define TEST_LIBPLATFORM_H_
#include "src/v8.h"
#include "test/cctest/cctest.h"
using namespace v8::internal;
using namespace v8::platform;
class TaskCounter {
public:
TaskCounter() : counter_(0) {}
~TaskCounter() { CHECK_EQ(0, counter_); }
int GetCount() const {
v8::base::LockGuard<v8::base::Mutex> guard(&lock_);
return counter_;
}
void Inc() {
v8::base::LockGuard<v8::base::Mutex> guard(&lock_);
++counter_;
}
void Dec() {
v8::base::LockGuard<v8::base::Mutex> guard(&lock_);
--counter_;
}
private:
mutable v8::base::Mutex lock_;
int counter_;
DISALLOW_COPY_AND_ASSIGN(TaskCounter);
};
class TestTask : public v8::Task {
public:
TestTask(TaskCounter* task_counter, bool expected_to_run)
: task_counter_(task_counter),
expected_to_run_(expected_to_run),
executed_(false) {
task_counter_->Inc();
}
explicit TestTask(TaskCounter* task_counter)
: task_counter_(task_counter), expected_to_run_(false), executed_(false) {
task_counter_->Inc();
}
virtual ~TestTask() {
CHECK_EQ(expected_to_run_, executed_);
task_counter_->Dec();
}
// v8::Task implementation.
virtual void Run() V8_OVERRIDE { executed_ = true; }
private:
TaskCounter* task_counter_;
bool expected_to_run_;
bool executed_;
DISALLOW_COPY_AND_ASSIGN(TestTask);
};
class TestWorkerThread : public v8::base::Thread {
public:
explicit TestWorkerThread(v8::Task* task)
: Thread(Options("libplatform TestWorkerThread")),
semaphore_(0),
task_(task) {}
virtual ~TestWorkerThread() {}
void Signal() { semaphore_.Signal(); }
// Thread implementation.
virtual void Run() V8_OVERRIDE {
semaphore_.Wait();
if (task_) {
task_->Run();
delete task_;
}
}
private:
v8::base::Semaphore semaphore_;
v8::Task* task_;
DISALLOW_COPY_AND_ASSIGN(TestWorkerThread);
};
#endif // TEST_LIBPLATFORM_H_
# 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.
[
]
# 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.
import os
import shutil
from testrunner.local import commands
from testrunner.local import testsuite
from testrunner.local import utils
from testrunner.objects import testcase
class LibplatformUnitTestsSuite(testsuite.TestSuite):
def __init__(self, name, root):
super(LibplatformUnitTestsSuite, self).__init__(name, root)
def ListTests(self, context):
shell = os.path.abspath(os.path.join(context.shell_dir, self.shell()))
if utils.IsWindows():
shell += ".exe"
output = commands.Execute(context.command_prefix +
[shell, "--gtest_list_tests"] +
context.extra_flags)
if output.exit_code != 0:
print output.stdout
print output.stderr
return []
tests = []
test_case = ''
for line in output.stdout.splitlines():
test_desc = line.strip().split()[0]
if test_desc.endswith('.'):
test_case = test_desc
elif test_case and test_desc:
test = testcase.TestCase(self, test_case + test_desc, dependency=None)
tests.append(test)
tests.sort()
return tests
def GetFlagsForTestCase(self, testcase, context):
return (testcase.flags + ["--gtest_filter=" + testcase.path] +
["--gtest_random_seed=%s" % context.random_seed] +
["--gtest_print_time=0"] +
context.mode_flags)
def shell(self):
return "libplatform-unittests"
def GetSuite(name, root):
return LibplatformUnitTestsSuite(name, root)
......@@ -237,7 +237,6 @@ class CppLintProcessor(SourceFileProcessor):
def GetPathsToSearch(self):
return ['src', 'include', 'samples',
join('test', 'base-unittests'),
join('test', 'cctest'),
join('test', 'compiler-unittests'),
join('test', 'heap-unittests'),
......
......@@ -52,7 +52,8 @@ from testrunner.objects import context
ARCH_GUESS = utils.DefaultArch()
DEFAULT_TESTS = ["mjsunit", "fuzz-natives", "base-unittests",
"cctest", "compiler-unittests", "heap-unittests",
"runtime-unittests", "message", "preparser"]
"libplatform-unittests", "runtime-unittests",
"message", "preparser"]
TIMEOUT_DEFAULT = 60
TIMEOUT_SCALEFACTOR = {"debug" : 4,
"release" : 1 }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment