| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license | 4 * Use of this source code is governed by a BSD-style license |
| 5 * that can be found in the LICENSE file in the root of the source | 5 * that can be found in the LICENSE file in the root of the source |
| 6 * tree. An additional intellectual property rights grant can be found | 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ | 9 */ |
| 10 | 10 |
| 11 #include <memory> | 11 #include <memory> |
| 12 #include <utility> | 12 #include <utility> |
| 13 | 13 |
| 14 #include "testing/gmock/include/gmock/gmock.h" | 14 #include "testing/gmock/include/gmock/gmock.h" |
| 15 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
| 16 #include "webrtc/base/task_queue.h" | 16 #include "webrtc/base/task_queue.h" |
| 17 #include "webrtc/base/timeutils.h" | 17 #include "webrtc/base/timeutils.h" |
| 18 #include "webrtc/modules/include/module.h" | 18 #include "webrtc/modules/include/module.h" |
| 19 #include "webrtc/modules/utility/source/process_thread_impl.h" | 19 #include "webrtc/modules/utility/source/process_thread_impl.h" |
| 20 | 20 |
| 21 namespace webrtc { | 21 namespace webrtc { |
| 22 | 22 |
| 23 using ::testing::_; | 23 using ::testing::_; |
| 24 using ::testing::DoAll; | 24 using ::testing::DoAll; |
| 25 using ::testing::InSequence; | 25 using ::testing::InSequence; |
| 26 using ::testing::Invoke; | 26 using ::testing::Invoke; |
| 27 using ::testing::Return; | 27 using ::testing::Return; |
| 28 using ::testing::SetArgPointee; | 28 using ::testing::SetArgPointee; |
| 29 | 29 |
| 30 // The length of time, in milliseconds, to wait for an event to become signaled. |
| 31 // Set to a fairly large value as there is quite a bit of variation on some |
| 32 // Windows bots. |
| 33 static const int kEventWaitTimeout = 500; |
| 34 |
| 30 class MockModule : public Module { | 35 class MockModule : public Module { |
| 31 public: | 36 public: |
| 32 MOCK_METHOD0(TimeUntilNextProcess, int64_t()); | 37 MOCK_METHOD0(TimeUntilNextProcess, int64_t()); |
| 33 MOCK_METHOD0(Process, void()); | 38 MOCK_METHOD0(Process, void()); |
| 34 MOCK_METHOD1(ProcessThreadAttached, void(ProcessThread*)); | 39 MOCK_METHOD1(ProcessThreadAttached, void(ProcessThread*)); |
| 35 }; | 40 }; |
| 36 | 41 |
| 37 class RaiseEventTask : public rtc::QueuedTask { | 42 class RaiseEventTask : public rtc::QueuedTask { |
| 38 public: | 43 public: |
| 39 RaiseEventTask(EventWrapper* event) : event_(event) {} | 44 RaiseEventTask(EventWrapper* event) : event_(event) {} |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 80 std::unique_ptr<EventWrapper> event(EventWrapper::Create()); | 85 std::unique_ptr<EventWrapper> event(EventWrapper::Create()); |
| 81 | 86 |
| 82 MockModule module; | 87 MockModule module; |
| 83 EXPECT_CALL(module, TimeUntilNextProcess()).WillRepeatedly(Return(0)); | 88 EXPECT_CALL(module, TimeUntilNextProcess()).WillRepeatedly(Return(0)); |
| 84 EXPECT_CALL(module, Process()) | 89 EXPECT_CALL(module, Process()) |
| 85 .WillOnce(DoAll(SetEvent(event.get()), Return())) | 90 .WillOnce(DoAll(SetEvent(event.get()), Return())) |
| 86 .WillRepeatedly(Return()); | 91 .WillRepeatedly(Return()); |
| 87 EXPECT_CALL(module, ProcessThreadAttached(&thread)).Times(1); | 92 EXPECT_CALL(module, ProcessThreadAttached(&thread)).Times(1); |
| 88 | 93 |
| 89 thread.RegisterModule(&module); | 94 thread.RegisterModule(&module); |
| 90 EXPECT_EQ(kEventSignaled, event->Wait(100)); | 95 EXPECT_EQ(kEventSignaled, event->Wait(kEventWaitTimeout)); |
| 91 | 96 |
| 92 EXPECT_CALL(module, ProcessThreadAttached(nullptr)).Times(1); | 97 EXPECT_CALL(module, ProcessThreadAttached(nullptr)).Times(1); |
| 93 thread.Stop(); | 98 thread.Stop(); |
| 94 } | 99 } |
| 95 | 100 |
| 96 // Same as ProcessCall except the module is registered before the | 101 // Same as ProcessCall except the module is registered before the |
| 97 // call to Start(). | 102 // call to Start(). |
| 98 TEST(ProcessThreadImpl, ProcessCall2) { | 103 TEST(ProcessThreadImpl, ProcessCall2) { |
| 99 ProcessThreadImpl thread("ProcessThread"); | 104 ProcessThreadImpl thread("ProcessThread"); |
| 100 std::unique_ptr<EventWrapper> event(EventWrapper::Create()); | 105 std::unique_ptr<EventWrapper> event(EventWrapper::Create()); |
| 101 | 106 |
| 102 MockModule module; | 107 MockModule module; |
| 103 EXPECT_CALL(module, TimeUntilNextProcess()).WillRepeatedly(Return(0)); | 108 EXPECT_CALL(module, TimeUntilNextProcess()).WillRepeatedly(Return(0)); |
| 104 EXPECT_CALL(module, Process()) | 109 EXPECT_CALL(module, Process()) |
| 105 .WillOnce(DoAll(SetEvent(event.get()), Return())) | 110 .WillOnce(DoAll(SetEvent(event.get()), Return())) |
| 106 .WillRepeatedly(Return()); | 111 .WillRepeatedly(Return()); |
| 107 | 112 |
| 108 thread.RegisterModule(&module); | 113 thread.RegisterModule(&module); |
| 109 | 114 |
| 110 EXPECT_CALL(module, ProcessThreadAttached(&thread)).Times(1); | 115 EXPECT_CALL(module, ProcessThreadAttached(&thread)).Times(1); |
| 111 thread.Start(); | 116 thread.Start(); |
| 112 EXPECT_EQ(kEventSignaled, event->Wait(100)); | 117 EXPECT_EQ(kEventSignaled, event->Wait(kEventWaitTimeout)); |
| 113 | 118 |
| 114 EXPECT_CALL(module, ProcessThreadAttached(nullptr)).Times(1); | 119 EXPECT_CALL(module, ProcessThreadAttached(nullptr)).Times(1); |
| 115 thread.Stop(); | 120 thread.Stop(); |
| 116 } | 121 } |
| 117 | 122 |
| 118 // Tests setting up a module for callbacks and then unregister that module. | 123 // Tests setting up a module for callbacks and then unregister that module. |
| 119 // After unregistration, we should not receive any further callbacks. | 124 // After unregistration, we should not receive any further callbacks. |
| 120 TEST(ProcessThreadImpl, Deregister) { | 125 TEST(ProcessThreadImpl, Deregister) { |
| 121 ProcessThreadImpl thread("ProcessThread"); | 126 ProcessThreadImpl thread("ProcessThread"); |
| 122 std::unique_ptr<EventWrapper> event(EventWrapper::Create()); | 127 std::unique_ptr<EventWrapper> event(EventWrapper::Create()); |
| 123 | 128 |
| 124 int process_count = 0; | 129 int process_count = 0; |
| 125 MockModule module; | 130 MockModule module; |
| 126 EXPECT_CALL(module, TimeUntilNextProcess()).WillRepeatedly(Return(0)); | 131 EXPECT_CALL(module, TimeUntilNextProcess()).WillRepeatedly(Return(0)); |
| 127 EXPECT_CALL(module, Process()) | 132 EXPECT_CALL(module, Process()) |
| 128 .WillOnce(DoAll(SetEvent(event.get()), | 133 .WillOnce(DoAll(SetEvent(event.get()), |
| 129 Increment(&process_count), | 134 Increment(&process_count), |
| 130 Return())) | 135 Return())) |
| 131 .WillRepeatedly(DoAll(Increment(&process_count), Return())); | 136 .WillRepeatedly(DoAll(Increment(&process_count), Return())); |
| 132 | 137 |
| 133 thread.RegisterModule(&module); | 138 thread.RegisterModule(&module); |
| 134 | 139 |
| 135 EXPECT_CALL(module, ProcessThreadAttached(&thread)).Times(1); | 140 EXPECT_CALL(module, ProcessThreadAttached(&thread)).Times(1); |
| 136 thread.Start(); | 141 thread.Start(); |
| 137 | 142 |
| 138 EXPECT_EQ(kEventSignaled, event->Wait(100)); | 143 EXPECT_EQ(kEventSignaled, event->Wait(kEventWaitTimeout)); |
| 139 | 144 |
| 140 EXPECT_CALL(module, ProcessThreadAttached(nullptr)).Times(1); | 145 EXPECT_CALL(module, ProcessThreadAttached(nullptr)).Times(1); |
| 141 thread.DeRegisterModule(&module); | 146 thread.DeRegisterModule(&module); |
| 142 | 147 |
| 143 EXPECT_GE(process_count, 1); | 148 EXPECT_GE(process_count, 1); |
| 144 int count_after_deregister = process_count; | 149 int count_after_deregister = process_count; |
| 145 | 150 |
| 146 // We shouldn't get any more callbacks. | 151 // We shouldn't get any more callbacks. |
| 147 EXPECT_EQ(kEventTimeout, event->Wait(20)); | 152 EXPECT_EQ(kEventTimeout, event->Wait(20)); |
| 148 EXPECT_EQ(count_after_deregister, process_count); | 153 EXPECT_EQ(count_after_deregister, process_count); |
| (...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 274 Return(1000))) | 279 Return(1000))) |
| 275 .WillOnce(Return(1000)); | 280 .WillOnce(Return(1000)); |
| 276 EXPECT_CALL(module, Process()) | 281 EXPECT_CALL(module, Process()) |
| 277 .WillOnce( | 282 .WillOnce( |
| 278 DoAll(SetTimestamp(&called_time), SetEvent(called.get()), Return())) | 283 DoAll(SetTimestamp(&called_time), SetEvent(called.get()), Return())) |
| 279 .WillRepeatedly(Return()); | 284 .WillRepeatedly(Return()); |
| 280 | 285 |
| 281 EXPECT_CALL(module, ProcessThreadAttached(&thread)).Times(1); | 286 EXPECT_CALL(module, ProcessThreadAttached(&thread)).Times(1); |
| 282 thread.RegisterModule(&module); | 287 thread.RegisterModule(&module); |
| 283 | 288 |
| 284 EXPECT_EQ(kEventSignaled, started->Wait(100)); | 289 EXPECT_EQ(kEventSignaled, started->Wait(kEventWaitTimeout)); |
| 285 thread.WakeUp(&module); | 290 thread.WakeUp(&module); |
| 286 EXPECT_EQ(kEventSignaled, called->Wait(100)); | 291 EXPECT_EQ(kEventSignaled, called->Wait(kEventWaitTimeout)); |
| 287 | 292 |
| 288 EXPECT_CALL(module, ProcessThreadAttached(nullptr)).Times(1); | 293 EXPECT_CALL(module, ProcessThreadAttached(nullptr)).Times(1); |
| 289 thread.Stop(); | 294 thread.Stop(); |
| 290 | 295 |
| 291 EXPECT_GE(called_time, start_time); | 296 EXPECT_GE(called_time, start_time); |
| 292 uint32_t diff = called_time - start_time; | 297 uint32_t diff = called_time - start_time; |
| 293 // We should have been called back much quicker than 1sec. | 298 // We should have been called back much quicker than 1sec. |
| 294 EXPECT_LE(diff, 100u); | 299 EXPECT_LE(diff, 100u); |
| 295 } | 300 } |
| 296 | 301 |
| 297 // Tests that we can post a task that gets run straight away on the worker | 302 // Tests that we can post a task that gets run straight away on the worker |
| 298 // thread. | 303 // thread. |
| 299 TEST(ProcessThreadImpl, PostTask) { | 304 TEST(ProcessThreadImpl, PostTask) { |
| 300 ProcessThreadImpl thread("ProcessThread"); | 305 ProcessThreadImpl thread("ProcessThread"); |
| 301 std::unique_ptr<EventWrapper> task_ran(EventWrapper::Create()); | 306 std::unique_ptr<EventWrapper> task_ran(EventWrapper::Create()); |
| 302 std::unique_ptr<RaiseEventTask> task(new RaiseEventTask(task_ran.get())); | 307 std::unique_ptr<RaiseEventTask> task(new RaiseEventTask(task_ran.get())); |
| 303 thread.Start(); | 308 thread.Start(); |
| 304 thread.PostTask(std::move(task)); | 309 thread.PostTask(std::move(task)); |
| 305 EXPECT_EQ(kEventSignaled, task_ran->Wait(100)); | 310 EXPECT_EQ(kEventSignaled, task_ran->Wait(kEventWaitTimeout)); |
| 306 thread.Stop(); | 311 thread.Stop(); |
| 307 } | 312 } |
| 308 | 313 |
| 309 } // namespace webrtc | 314 } // namespace webrtc |
| OLD | NEW |