| OLD | NEW |
| 1 // Copyright 2014 the V8 project authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "src/libplatform/default-platform.h" | 5 #include "src/libplatform/default-platform.h" |
| 6 #include "testing/gmock/include/gmock/gmock.h" | 6 #include "testing/gmock/include/gmock/gmock.h" |
| 7 | 7 |
| 8 using testing::InSequence; | 8 using testing::InSequence; |
| 9 using testing::StrictMock; | 9 using testing::StrictMock; |
| 10 | 10 |
| 11 namespace v8 { | 11 namespace v8 { |
| 12 namespace platform { | 12 namespace platform { |
| 13 | 13 |
| 14 namespace { | 14 namespace { |
| 15 | 15 |
| 16 struct MockTask : public Task { | 16 struct MockTask : public Task { |
| 17 virtual ~MockTask() { Die(); } | 17 virtual ~MockTask() { Die(); } |
| 18 MOCK_METHOD0(Run, void()); | 18 MOCK_METHOD0(Run, void()); |
| 19 MOCK_METHOD0(Die, void()); | 19 MOCK_METHOD0(Die, void()); |
| 20 }; | 20 }; |
| 21 | 21 |
| 22 |
| 23 class DefaultPlatformWithMockTime : public DefaultPlatform { |
| 24 public: |
| 25 DefaultPlatformWithMockTime() : time_(0) {} |
| 26 double MonotonicallyIncreasingTime() override { return time_; } |
| 27 void IncreaseTime(double seconds) { time_ += seconds; } |
| 28 |
| 29 private: |
| 30 double time_; |
| 31 }; |
| 32 |
| 22 } // namespace | 33 } // namespace |
| 23 | 34 |
| 24 | 35 |
| 25 TEST(DefaultPlatformTest, PumpMessageLoop) { | 36 TEST(DefaultPlatformTest, PumpMessageLoop) { |
| 26 InSequence s; | 37 InSequence s; |
| 27 | 38 |
| 28 int dummy; | 39 int dummy; |
| 29 Isolate* isolate = reinterpret_cast<Isolate*>(&dummy); | 40 Isolate* isolate = reinterpret_cast<Isolate*>(&dummy); |
| 30 | 41 |
| 31 DefaultPlatform platform; | 42 DefaultPlatform platform; |
| 32 EXPECT_FALSE(platform.PumpMessageLoop(isolate)); | 43 EXPECT_FALSE(platform.PumpMessageLoop(isolate)); |
| 33 | 44 |
| 34 StrictMock<MockTask>* task = new StrictMock<MockTask>; | 45 StrictMock<MockTask>* task = new StrictMock<MockTask>; |
| 35 platform.CallOnForegroundThread(isolate, task); | 46 platform.CallOnForegroundThread(isolate, task); |
| 36 EXPECT_CALL(*task, Run()); | 47 EXPECT_CALL(*task, Run()); |
| 37 EXPECT_CALL(*task, Die()); | 48 EXPECT_CALL(*task, Die()); |
| 38 EXPECT_TRUE(platform.PumpMessageLoop(isolate)); | 49 EXPECT_TRUE(platform.PumpMessageLoop(isolate)); |
| 39 EXPECT_FALSE(platform.PumpMessageLoop(isolate)); | 50 EXPECT_FALSE(platform.PumpMessageLoop(isolate)); |
| 40 } | 51 } |
| 41 | 52 |
| 53 |
| 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 |
| 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 |
| 42 } // namespace platform | 114 } // namespace platform |
| 43 } // namespace v8 | 115 } // namespace v8 |
| OLD | NEW |