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 "webrtc/base/location.h" |
14 #include "webrtc/base/task_queue.h" | 15 #include "webrtc/base/task_queue.h" |
15 #include "webrtc/base/timeutils.h" | 16 #include "webrtc/base/timeutils.h" |
16 #include "webrtc/modules/include/module.h" | 17 #include "webrtc/modules/include/module.h" |
17 #include "webrtc/modules/utility/source/process_thread_impl.h" | 18 #include "webrtc/modules/utility/source/process_thread_impl.h" |
18 #include "webrtc/test/gmock.h" | 19 #include "webrtc/test/gmock.h" |
19 #include "webrtc/test/gtest.h" | 20 #include "webrtc/test/gtest.h" |
20 | 21 |
21 namespace webrtc { | 22 namespace webrtc { |
22 | 23 |
23 using ::testing::_; | 24 using ::testing::_; |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
86 | 87 |
87 MockModule module; | 88 MockModule module; |
88 EXPECT_CALL(module, TimeUntilNextProcess()) | 89 EXPECT_CALL(module, TimeUntilNextProcess()) |
89 .WillOnce(Return(0)) | 90 .WillOnce(Return(0)) |
90 .WillRepeatedly(Return(1)); | 91 .WillRepeatedly(Return(1)); |
91 EXPECT_CALL(module, Process()) | 92 EXPECT_CALL(module, Process()) |
92 .WillOnce(DoAll(SetEvent(event.get()), Return())) | 93 .WillOnce(DoAll(SetEvent(event.get()), Return())) |
93 .WillRepeatedly(Return()); | 94 .WillRepeatedly(Return()); |
94 EXPECT_CALL(module, ProcessThreadAttached(&thread)).Times(1); | 95 EXPECT_CALL(module, ProcessThreadAttached(&thread)).Times(1); |
95 | 96 |
96 thread.RegisterModule(&module); | 97 thread.RegisterModule(&module, RTC_FROM_HERE); |
97 EXPECT_EQ(kEventSignaled, event->Wait(kEventWaitTimeout)); | 98 EXPECT_EQ(kEventSignaled, event->Wait(kEventWaitTimeout)); |
98 | 99 |
99 EXPECT_CALL(module, ProcessThreadAttached(nullptr)).Times(1); | 100 EXPECT_CALL(module, ProcessThreadAttached(nullptr)).Times(1); |
100 thread.Stop(); | 101 thread.Stop(); |
101 } | 102 } |
102 | 103 |
103 // Same as ProcessCall except the module is registered before the | 104 // Same as ProcessCall except the module is registered before the |
104 // call to Start(). | 105 // call to Start(). |
105 TEST(ProcessThreadImpl, ProcessCall2) { | 106 TEST(ProcessThreadImpl, ProcessCall2) { |
106 ProcessThreadImpl thread("ProcessThread"); | 107 ProcessThreadImpl thread("ProcessThread"); |
107 std::unique_ptr<EventWrapper> event(EventWrapper::Create()); | 108 std::unique_ptr<EventWrapper> event(EventWrapper::Create()); |
108 | 109 |
109 MockModule module; | 110 MockModule module; |
110 EXPECT_CALL(module, TimeUntilNextProcess()) | 111 EXPECT_CALL(module, TimeUntilNextProcess()) |
111 .WillOnce(Return(0)) | 112 .WillOnce(Return(0)) |
112 .WillRepeatedly(Return(1)); | 113 .WillRepeatedly(Return(1)); |
113 EXPECT_CALL(module, Process()) | 114 EXPECT_CALL(module, Process()) |
114 .WillOnce(DoAll(SetEvent(event.get()), Return())) | 115 .WillOnce(DoAll(SetEvent(event.get()), Return())) |
115 .WillRepeatedly(Return()); | 116 .WillRepeatedly(Return()); |
116 | 117 |
117 thread.RegisterModule(&module); | 118 thread.RegisterModule(&module, RTC_FROM_HERE); |
118 | 119 |
119 EXPECT_CALL(module, ProcessThreadAttached(&thread)).Times(1); | 120 EXPECT_CALL(module, ProcessThreadAttached(&thread)).Times(1); |
120 thread.Start(); | 121 thread.Start(); |
121 EXPECT_EQ(kEventSignaled, event->Wait(kEventWaitTimeout)); | 122 EXPECT_EQ(kEventSignaled, event->Wait(kEventWaitTimeout)); |
122 | 123 |
123 EXPECT_CALL(module, ProcessThreadAttached(nullptr)).Times(1); | 124 EXPECT_CALL(module, ProcessThreadAttached(nullptr)).Times(1); |
124 thread.Stop(); | 125 thread.Stop(); |
125 } | 126 } |
126 | 127 |
127 // Tests setting up a module for callbacks and then unregister that module. | 128 // Tests setting up a module for callbacks and then unregister that module. |
128 // After unregistration, we should not receive any further callbacks. | 129 // After unregistration, we should not receive any further callbacks. |
129 TEST(ProcessThreadImpl, Deregister) { | 130 TEST(ProcessThreadImpl, Deregister) { |
130 ProcessThreadImpl thread("ProcessThread"); | 131 ProcessThreadImpl thread("ProcessThread"); |
131 std::unique_ptr<EventWrapper> event(EventWrapper::Create()); | 132 std::unique_ptr<EventWrapper> event(EventWrapper::Create()); |
132 | 133 |
133 int process_count = 0; | 134 int process_count = 0; |
134 MockModule module; | 135 MockModule module; |
135 EXPECT_CALL(module, TimeUntilNextProcess()) | 136 EXPECT_CALL(module, TimeUntilNextProcess()) |
136 .WillOnce(Return(0)) | 137 .WillOnce(Return(0)) |
137 .WillRepeatedly(Return(1)); | 138 .WillRepeatedly(Return(1)); |
138 EXPECT_CALL(module, Process()) | 139 EXPECT_CALL(module, Process()) |
139 .WillOnce(DoAll(SetEvent(event.get()), | 140 .WillOnce(DoAll(SetEvent(event.get()), |
140 Increment(&process_count), | 141 Increment(&process_count), |
141 Return())) | 142 Return())) |
142 .WillRepeatedly(DoAll(Increment(&process_count), Return())); | 143 .WillRepeatedly(DoAll(Increment(&process_count), Return())); |
143 | 144 |
144 thread.RegisterModule(&module); | 145 thread.RegisterModule(&module, RTC_FROM_HERE); |
145 | 146 |
146 EXPECT_CALL(module, ProcessThreadAttached(&thread)).Times(1); | 147 EXPECT_CALL(module, ProcessThreadAttached(&thread)).Times(1); |
147 thread.Start(); | 148 thread.Start(); |
148 | 149 |
149 EXPECT_EQ(kEventSignaled, event->Wait(kEventWaitTimeout)); | 150 EXPECT_EQ(kEventSignaled, event->Wait(kEventWaitTimeout)); |
150 | 151 |
151 EXPECT_CALL(module, ProcessThreadAttached(nullptr)).Times(1); | 152 EXPECT_CALL(module, ProcessThreadAttached(nullptr)).Times(1); |
152 thread.DeRegisterModule(&module); | 153 thread.DeRegisterModule(&module); |
153 | 154 |
154 EXPECT_GE(process_count, 1); | 155 EXPECT_GE(process_count, 1); |
(...skipping 21 matching lines...) Expand all Loading... |
176 .WillOnce(DoAll(SetTimestamp(&start_time), | 177 .WillOnce(DoAll(SetTimestamp(&start_time), |
177 Return(milliseconds))) | 178 Return(milliseconds))) |
178 .WillRepeatedly(Return(milliseconds)); | 179 .WillRepeatedly(Return(milliseconds)); |
179 EXPECT_CALL(module, Process()) | 180 EXPECT_CALL(module, Process()) |
180 .WillOnce(DoAll(SetTimestamp(&called_time), | 181 .WillOnce(DoAll(SetTimestamp(&called_time), |
181 SetEvent(event.get()), | 182 SetEvent(event.get()), |
182 Return())) | 183 Return())) |
183 .WillRepeatedly(Return()); | 184 .WillRepeatedly(Return()); |
184 | 185 |
185 EXPECT_CALL(module, ProcessThreadAttached(&thread)).Times(1); | 186 EXPECT_CALL(module, ProcessThreadAttached(&thread)).Times(1); |
186 thread.RegisterModule(&module); | 187 thread.RegisterModule(&module, RTC_FROM_HERE); |
187 | 188 |
188 // Add a buffer of 50ms due to slowness of some trybots | 189 // Add a buffer of 50ms due to slowness of some trybots |
189 // (e.g. win_drmemory_light) | 190 // (e.g. win_drmemory_light) |
190 EXPECT_EQ(kEventSignaled, event->Wait(milliseconds + 50)); | 191 EXPECT_EQ(kEventSignaled, event->Wait(milliseconds + 50)); |
191 | 192 |
192 EXPECT_CALL(module, ProcessThreadAttached(nullptr)).Times(1); | 193 EXPECT_CALL(module, ProcessThreadAttached(nullptr)).Times(1); |
193 thread.Stop(); | 194 thread.Stop(); |
194 | 195 |
195 ASSERT_GT(start_time, 0); | 196 ASSERT_GT(start_time, 0); |
196 ASSERT_GT(called_time, 0); | 197 ASSERT_GT(called_time, 0); |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
237 MockModule module; | 238 MockModule module; |
238 int callback_count = 0; | 239 int callback_count = 0; |
239 // Ask for a callback after 20ms. | 240 // Ask for a callback after 20ms. |
240 EXPECT_CALL(module, TimeUntilNextProcess()) | 241 EXPECT_CALL(module, TimeUntilNextProcess()) |
241 .WillRepeatedly(Return(20)); | 242 .WillRepeatedly(Return(20)); |
242 EXPECT_CALL(module, Process()) | 243 EXPECT_CALL(module, Process()) |
243 .WillRepeatedly(DoAll(Increment(&callback_count), | 244 .WillRepeatedly(DoAll(Increment(&callback_count), |
244 Return())); | 245 Return())); |
245 | 246 |
246 EXPECT_CALL(module, ProcessThreadAttached(&thread)).Times(1); | 247 EXPECT_CALL(module, ProcessThreadAttached(&thread)).Times(1); |
247 thread.RegisterModule(&module); | 248 thread.RegisterModule(&module, RTC_FROM_HERE); |
248 | 249 |
249 EXPECT_EQ(kEventTimeout, event->Wait(1000)); | 250 EXPECT_EQ(kEventTimeout, event->Wait(1000)); |
250 | 251 |
251 EXPECT_CALL(module, ProcessThreadAttached(nullptr)).Times(1); | 252 EXPECT_CALL(module, ProcessThreadAttached(nullptr)).Times(1); |
252 thread.Stop(); | 253 thread.Stop(); |
253 | 254 |
254 printf("Callback count: %i\n", callback_count); | 255 printf("Callback count: %i\n", callback_count); |
255 // Check that we got called back up to 50 times. | 256 // Check that we got called back up to 50 times. |
256 // Some of the try bots run on slow virtual machines, so the lower bound | 257 // Some of the try bots run on slow virtual machines, so the lower bound |
257 // is much more relaxed to avoid flakiness. | 258 // is much more relaxed to avoid flakiness. |
(...skipping 25 matching lines...) Expand all Loading... |
283 .WillOnce(DoAll(SetTimestamp(&start_time), | 284 .WillOnce(DoAll(SetTimestamp(&start_time), |
284 SetEvent(started.get()), | 285 SetEvent(started.get()), |
285 Return(1000))) | 286 Return(1000))) |
286 .WillOnce(Return(1000)); | 287 .WillOnce(Return(1000)); |
287 EXPECT_CALL(module, Process()) | 288 EXPECT_CALL(module, Process()) |
288 .WillOnce( | 289 .WillOnce( |
289 DoAll(SetTimestamp(&called_time), SetEvent(called.get()), Return())) | 290 DoAll(SetTimestamp(&called_time), SetEvent(called.get()), Return())) |
290 .WillRepeatedly(Return()); | 291 .WillRepeatedly(Return()); |
291 | 292 |
292 EXPECT_CALL(module, ProcessThreadAttached(&thread)).Times(1); | 293 EXPECT_CALL(module, ProcessThreadAttached(&thread)).Times(1); |
293 thread.RegisterModule(&module); | 294 thread.RegisterModule(&module, RTC_FROM_HERE); |
294 | 295 |
295 EXPECT_EQ(kEventSignaled, started->Wait(kEventWaitTimeout)); | 296 EXPECT_EQ(kEventSignaled, started->Wait(kEventWaitTimeout)); |
296 thread.WakeUp(&module); | 297 thread.WakeUp(&module); |
297 EXPECT_EQ(kEventSignaled, called->Wait(kEventWaitTimeout)); | 298 EXPECT_EQ(kEventSignaled, called->Wait(kEventWaitTimeout)); |
298 | 299 |
299 EXPECT_CALL(module, ProcessThreadAttached(nullptr)).Times(1); | 300 EXPECT_CALL(module, ProcessThreadAttached(nullptr)).Times(1); |
300 thread.Stop(); | 301 thread.Stop(); |
301 | 302 |
302 EXPECT_GE(called_time, start_time); | 303 EXPECT_GE(called_time, start_time); |
303 uint32_t diff = called_time - start_time; | 304 uint32_t diff = called_time - start_time; |
304 // We should have been called back much quicker than 1sec. | 305 // We should have been called back much quicker than 1sec. |
305 EXPECT_LE(diff, 100u); | 306 EXPECT_LE(diff, 100u); |
306 } | 307 } |
307 | 308 |
308 // Tests that we can post a task that gets run straight away on the worker | 309 // Tests that we can post a task that gets run straight away on the worker |
309 // thread. | 310 // thread. |
310 TEST(ProcessThreadImpl, PostTask) { | 311 TEST(ProcessThreadImpl, PostTask) { |
311 ProcessThreadImpl thread("ProcessThread"); | 312 ProcessThreadImpl thread("ProcessThread"); |
312 std::unique_ptr<EventWrapper> task_ran(EventWrapper::Create()); | 313 std::unique_ptr<EventWrapper> task_ran(EventWrapper::Create()); |
313 std::unique_ptr<RaiseEventTask> task(new RaiseEventTask(task_ran.get())); | 314 std::unique_ptr<RaiseEventTask> task(new RaiseEventTask(task_ran.get())); |
314 thread.Start(); | 315 thread.Start(); |
315 thread.PostTask(std::move(task)); | 316 thread.PostTask(std::move(task)); |
316 EXPECT_EQ(kEventSignaled, task_ran->Wait(kEventWaitTimeout)); | 317 EXPECT_EQ(kEventSignaled, task_ran->Wait(kEventWaitTimeout)); |
317 thread.Stop(); | 318 thread.Stop(); |
318 } | 319 } |
319 | 320 |
320 } // namespace webrtc | 321 } // namespace webrtc |
OLD | NEW |