• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "src/libplatform/default-platform.h"
6 #include "testing/gmock/include/gmock/gmock.h"
7 
8 using testing::InSequence;
9 using testing::StrictMock;
10 
11 namespace v8 {
12 namespace platform {
13 
14 namespace {
15 
16 struct MockTask : public Task {
~MockTaskv8::platform::__anon0e853c670111::MockTask17   virtual ~MockTask() { Die(); }
18   MOCK_METHOD0(Run, void());
19   MOCK_METHOD0(Die, void());
20 };
21 
22 
23 class DefaultPlatformWithMockTime : public DefaultPlatform {
24  public:
DefaultPlatformWithMockTime()25   DefaultPlatformWithMockTime() : time_(0) {}
MonotonicallyIncreasingTime()26   double MonotonicallyIncreasingTime() override { return time_; }
IncreaseTime(double seconds)27   void IncreaseTime(double seconds) { time_ += seconds; }
28 
29  private:
30   double time_;
31 };
32 
33 }  // namespace
34 
35 
TEST(DefaultPlatformTest,PumpMessageLoop)36 TEST(DefaultPlatformTest, PumpMessageLoop) {
37   InSequence s;
38 
39   int dummy;
40   Isolate* isolate = reinterpret_cast<Isolate*>(&dummy);
41 
42   DefaultPlatform platform;
43   EXPECT_FALSE(platform.PumpMessageLoop(isolate));
44 
45   StrictMock<MockTask>* task = new StrictMock<MockTask>;
46   platform.CallOnForegroundThread(isolate, task);
47   EXPECT_CALL(*task, Run());
48   EXPECT_CALL(*task, Die());
49   EXPECT_TRUE(platform.PumpMessageLoop(isolate));
50   EXPECT_FALSE(platform.PumpMessageLoop(isolate));
51 }
52 
53 
TEST(DefaultPlatformTest,PumpMessageLoopDelayed)54 TEST(DefaultPlatformTest, PumpMessageLoopDelayed) {
55   InSequence s;
56 
57   int dummy;
58   Isolate* isolate = reinterpret_cast<Isolate*>(&dummy);
59 
60   DefaultPlatformWithMockTime platform;
61   EXPECT_FALSE(platform.PumpMessageLoop(isolate));
62 
63   StrictMock<MockTask>* task1 = new StrictMock<MockTask>;
64   StrictMock<MockTask>* task2 = new StrictMock<MockTask>;
65   platform.CallDelayedOnForegroundThread(isolate, task2, 100);
66   platform.CallDelayedOnForegroundThread(isolate, task1, 10);
67 
68   EXPECT_FALSE(platform.PumpMessageLoop(isolate));
69 
70   platform.IncreaseTime(11);
71   EXPECT_CALL(*task1, Run());
72   EXPECT_CALL(*task1, Die());
73   EXPECT_TRUE(platform.PumpMessageLoop(isolate));
74 
75   EXPECT_FALSE(platform.PumpMessageLoop(isolate));
76 
77   platform.IncreaseTime(90);
78   EXPECT_CALL(*task2, Run());
79   EXPECT_CALL(*task2, Die());
80   EXPECT_TRUE(platform.PumpMessageLoop(isolate));
81 }
82 
83 
TEST(DefaultPlatformTest,PumpMessageLoopNoStarvation)84 TEST(DefaultPlatformTest, PumpMessageLoopNoStarvation) {
85   InSequence s;
86 
87   int dummy;
88   Isolate* isolate = reinterpret_cast<Isolate*>(&dummy);
89 
90   DefaultPlatformWithMockTime platform;
91   EXPECT_FALSE(platform.PumpMessageLoop(isolate));
92 
93   StrictMock<MockTask>* task1 = new StrictMock<MockTask>;
94   StrictMock<MockTask>* task2 = new StrictMock<MockTask>;
95   StrictMock<MockTask>* task3 = new StrictMock<MockTask>;
96   platform.CallOnForegroundThread(isolate, task1);
97   platform.CallDelayedOnForegroundThread(isolate, task2, 10);
98   platform.IncreaseTime(11);
99 
100   EXPECT_CALL(*task1, Run());
101   EXPECT_CALL(*task1, Die());
102   EXPECT_TRUE(platform.PumpMessageLoop(isolate));
103 
104   platform.CallOnForegroundThread(isolate, task3);
105 
106   EXPECT_CALL(*task2, Run());
107   EXPECT_CALL(*task2, Die());
108   EXPECT_TRUE(platform.PumpMessageLoop(isolate));
109   EXPECT_CALL(*task3, Run());
110   EXPECT_CALL(*task3, Die());
111   EXPECT_TRUE(platform.PumpMessageLoop(isolate));
112 }
113 
114 
TEST(DefaultPlatformTest,PendingDelayedTasksAreDestroyedOnShutdown)115 TEST(DefaultPlatformTest, PendingDelayedTasksAreDestroyedOnShutdown) {
116   InSequence s;
117 
118   int dummy;
119   Isolate* isolate = reinterpret_cast<Isolate*>(&dummy);
120 
121   {
122     DefaultPlatformWithMockTime platform;
123     StrictMock<MockTask>* task = new StrictMock<MockTask>;
124     platform.CallDelayedOnForegroundThread(isolate, task, 10);
125     EXPECT_CALL(*task, Die());
126   }
127 }
128 
129 
130 }  // namespace platform
131 }  // namespace v8
132