Line data Source code
1 : // Copyright 2014 the V8 project authors. All rights reserved.
2 : // Use of this source code is governed by a BSD-style license that can be
3 : // found in the LICENSE file.
4 :
5 : #include "include/v8-platform.h"
6 : #include "src/libplatform/task-queue.h"
7 : #include "src/libplatform/worker-thread.h"
8 : #include "testing/gmock/include/gmock/gmock.h"
9 :
10 : using testing::InSequence;
11 : using testing::IsNull;
12 : using testing::StrictMock;
13 :
14 : namespace v8 {
15 : namespace platform {
16 :
17 : namespace {
18 :
19 11 : struct MockTask : public Task {
20 : // See issue v8:8185
21 33 : ~MockTask() /* override */ { Die(); }
22 22 : MOCK_METHOD0(Run, void());
23 22 : MOCK_METHOD0(Die, void());
24 : };
25 :
26 : } // namespace
27 :
28 : // Needs to be in v8::platform due to BlockUntilQueueEmptyForTesting
29 : // being private.
30 15443 : TEST(WorkerThreadTest, PostSingleTask) {
31 2 : TaskQueue queue;
32 2 : WorkerThread thread1(&queue);
33 2 : WorkerThread thread2(&queue);
34 :
35 2 : InSequence s;
36 1 : std::unique_ptr<StrictMock<MockTask>> task(new StrictMock<MockTask>);
37 2 : EXPECT_CALL(*task.get(), Run());
38 2 : EXPECT_CALL(*task.get(), Die());
39 2 : queue.Append(std::move(task));
40 :
41 : // The next call should not time out.
42 1 : queue.BlockUntilQueueEmptyForTesting();
43 1 : queue.Terminate();
44 1 : }
45 :
46 : namespace worker_thread_unittest {
47 :
48 15443 : TEST(WorkerThreadTest, Basic) {
49 : static const size_t kNumTasks = 10;
50 :
51 2 : TaskQueue queue;
52 21 : for (size_t i = 0; i < kNumTasks; ++i) {
53 20 : InSequence s;
54 10 : std::unique_ptr<StrictMock<MockTask>> task(new StrictMock<MockTask>);
55 20 : EXPECT_CALL(*task.get(), Run());
56 20 : EXPECT_CALL(*task.get(), Die());
57 20 : queue.Append(std::move(task));
58 : }
59 :
60 2 : WorkerThread thread1(&queue);
61 2 : WorkerThread thread2(&queue);
62 :
63 : // TaskQueue DCHECKS that it's empty in its destructor.
64 1 : queue.Terminate();
65 1 : }
66 :
67 : } // namespace worker_thread_unittest
68 : } // namespace platform
69 9264 : } // namespace v8
|