tommi@webrtc.org | 0c3e12b | 2015-02-06 09:44:12 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
| 3 | * |
| 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 |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
kwiberg | 22feaa3 | 2016-03-17 09:17:43 -0700 | [diff] [blame] | 11 | #include <memory> |
kwiberg | 0eb15ed | 2015-12-17 03:04:15 -0800 | [diff] [blame] | 12 | #include <utility> |
| 13 | |
Danil Chapovalov | 471783f | 2019-03-11 14:26:02 +0100 | [diff] [blame] | 14 | #include "api/task_queue/queued_task.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 15 | #include "modules/include/module.h" |
| 16 | #include "modules/utility/source/process_thread_impl.h" |
| 17 | #include "rtc_base/location.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 18 | #include "rtc_base/time_utils.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 19 | #include "test/gmock.h" |
| 20 | #include "test/gtest.h" |
tommi@webrtc.org | 0c3e12b | 2015-02-06 09:44:12 +0000 | [diff] [blame] | 21 | |
| 22 | namespace webrtc { |
| 23 | |
| 24 | using ::testing::_; |
| 25 | using ::testing::DoAll; |
| 26 | using ::testing::InSequence; |
| 27 | using ::testing::Invoke; |
| 28 | using ::testing::Return; |
| 29 | using ::testing::SetArgPointee; |
| 30 | |
skvlad | b460fd8 | 2016-09-06 14:34:32 -0700 | [diff] [blame] | 31 | // The length of time, in milliseconds, to wait for an event to become signaled. |
| 32 | // Set to a fairly large value as there is quite a bit of variation on some |
| 33 | // Windows bots. |
| 34 | static const int kEventWaitTimeout = 500; |
| 35 | |
tommi@webrtc.org | 0c3e12b | 2015-02-06 09:44:12 +0000 | [diff] [blame] | 36 | class MockModule : public Module { |
| 37 | public: |
| 38 | MOCK_METHOD0(TimeUntilNextProcess, int64_t()); |
pbos | a26ac92 | 2016-02-25 04:50:01 -0800 | [diff] [blame] | 39 | MOCK_METHOD0(Process, void()); |
tommi@webrtc.org | 3985f01 | 2015-02-27 13:36:34 +0000 | [diff] [blame] | 40 | MOCK_METHOD1(ProcessThreadAttached, void(ProcessThread*)); |
tommi@webrtc.org | 0c3e12b | 2015-02-06 09:44:12 +0000 | [diff] [blame] | 41 | }; |
| 42 | |
Danil Chapovalov | 471783f | 2019-03-11 14:26:02 +0100 | [diff] [blame] | 43 | class RaiseEventTask : public QueuedTask { |
tommi@webrtc.org | 0305448 | 2015-03-05 13:13:42 +0000 | [diff] [blame] | 44 | public: |
Niels Möller | 2c16cc6 | 2018-10-29 09:47:51 +0100 | [diff] [blame] | 45 | RaiseEventTask(rtc::Event* event) : event_(event) {} |
tommi | 435f98b | 2016-05-28 14:57:15 -0700 | [diff] [blame] | 46 | bool Run() override { |
| 47 | event_->Set(); |
| 48 | return true; |
| 49 | } |
tommi@webrtc.org | 0305448 | 2015-03-05 13:13:42 +0000 | [diff] [blame] | 50 | |
| 51 | private: |
Niels Möller | 2c16cc6 | 2018-10-29 09:47:51 +0100 | [diff] [blame] | 52 | rtc::Event* event_; |
tommi@webrtc.org | 0305448 | 2015-03-05 13:13:42 +0000 | [diff] [blame] | 53 | }; |
| 54 | |
tommi@webrtc.org | 0c3e12b | 2015-02-06 09:44:12 +0000 | [diff] [blame] | 55 | ACTION_P(SetEvent, event) { |
| 56 | event->Set(); |
| 57 | } |
| 58 | |
| 59 | ACTION_P(Increment, counter) { |
| 60 | ++(*counter); |
| 61 | } |
| 62 | |
| 63 | ACTION_P(SetTimestamp, ptr) { |
Niels Möller | d28db7f | 2016-05-10 16:31:47 +0200 | [diff] [blame] | 64 | *ptr = rtc::TimeMillis(); |
tommi@webrtc.org | 0c3e12b | 2015-02-06 09:44:12 +0000 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | TEST(ProcessThreadImpl, StartStop) { |
stefan | 847855b | 2015-09-11 09:52:15 -0700 | [diff] [blame] | 68 | ProcessThreadImpl thread("ProcessThread"); |
tommi@webrtc.org | 3985f01 | 2015-02-27 13:36:34 +0000 | [diff] [blame] | 69 | thread.Start(); |
| 70 | thread.Stop(); |
tommi@webrtc.org | 0c3e12b | 2015-02-06 09:44:12 +0000 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | TEST(ProcessThreadImpl, MultipleStartStop) { |
stefan | 847855b | 2015-09-11 09:52:15 -0700 | [diff] [blame] | 74 | ProcessThreadImpl thread("ProcessThread"); |
tommi@webrtc.org | 0c3e12b | 2015-02-06 09:44:12 +0000 | [diff] [blame] | 75 | for (int i = 0; i < 5; ++i) { |
tommi@webrtc.org | 3985f01 | 2015-02-27 13:36:34 +0000 | [diff] [blame] | 76 | thread.Start(); |
| 77 | thread.Stop(); |
tommi@webrtc.org | 0c3e12b | 2015-02-06 09:44:12 +0000 | [diff] [blame] | 78 | } |
| 79 | } |
| 80 | |
| 81 | // Verifies that we get at least call back to Process() on the worker thread. |
| 82 | TEST(ProcessThreadImpl, ProcessCall) { |
stefan | 847855b | 2015-09-11 09:52:15 -0700 | [diff] [blame] | 83 | ProcessThreadImpl thread("ProcessThread"); |
tommi@webrtc.org | 3985f01 | 2015-02-27 13:36:34 +0000 | [diff] [blame] | 84 | thread.Start(); |
tommi@webrtc.org | 0c3e12b | 2015-02-06 09:44:12 +0000 | [diff] [blame] | 85 | |
Niels Möller | c572ff3 | 2018-11-07 08:43:50 +0100 | [diff] [blame] | 86 | rtc::Event event; |
tommi@webrtc.org | 0c3e12b | 2015-02-06 09:44:12 +0000 | [diff] [blame] | 87 | |
| 88 | MockModule module; |
tommi | 82ead60 | 2017-02-19 16:09:55 -0800 | [diff] [blame] | 89 | EXPECT_CALL(module, TimeUntilNextProcess()) |
| 90 | .WillOnce(Return(0)) |
| 91 | .WillRepeatedly(Return(1)); |
tommi@webrtc.org | 0c3e12b | 2015-02-06 09:44:12 +0000 | [diff] [blame] | 92 | EXPECT_CALL(module, Process()) |
Niels Möller | 2c16cc6 | 2018-10-29 09:47:51 +0100 | [diff] [blame] | 93 | .WillOnce(DoAll(SetEvent(&event), Return())) |
pbos | a26ac92 | 2016-02-25 04:50:01 -0800 | [diff] [blame] | 94 | .WillRepeatedly(Return()); |
tommi@webrtc.org | 3985f01 | 2015-02-27 13:36:34 +0000 | [diff] [blame] | 95 | EXPECT_CALL(module, ProcessThreadAttached(&thread)).Times(1); |
tommi@webrtc.org | 0c3e12b | 2015-02-06 09:44:12 +0000 | [diff] [blame] | 96 | |
tommi | dea489f | 2017-03-03 03:20:24 -0800 | [diff] [blame] | 97 | thread.RegisterModule(&module, RTC_FROM_HERE); |
Niels Möller | 2c16cc6 | 2018-10-29 09:47:51 +0100 | [diff] [blame] | 98 | EXPECT_TRUE(event.Wait(kEventWaitTimeout)); |
tommi@webrtc.org | 3985f01 | 2015-02-27 13:36:34 +0000 | [diff] [blame] | 99 | |
| 100 | EXPECT_CALL(module, ProcessThreadAttached(nullptr)).Times(1); |
| 101 | thread.Stop(); |
tommi@webrtc.org | 0c3e12b | 2015-02-06 09:44:12 +0000 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | // Same as ProcessCall except the module is registered before the |
| 105 | // call to Start(). |
| 106 | TEST(ProcessThreadImpl, ProcessCall2) { |
stefan | 847855b | 2015-09-11 09:52:15 -0700 | [diff] [blame] | 107 | ProcessThreadImpl thread("ProcessThread"); |
Niels Möller | c572ff3 | 2018-11-07 08:43:50 +0100 | [diff] [blame] | 108 | rtc::Event event; |
tommi@webrtc.org | 0c3e12b | 2015-02-06 09:44:12 +0000 | [diff] [blame] | 109 | |
| 110 | MockModule module; |
tommi | 82ead60 | 2017-02-19 16:09:55 -0800 | [diff] [blame] | 111 | EXPECT_CALL(module, TimeUntilNextProcess()) |
| 112 | .WillOnce(Return(0)) |
| 113 | .WillRepeatedly(Return(1)); |
tommi@webrtc.org | 0c3e12b | 2015-02-06 09:44:12 +0000 | [diff] [blame] | 114 | EXPECT_CALL(module, Process()) |
Niels Möller | 2c16cc6 | 2018-10-29 09:47:51 +0100 | [diff] [blame] | 115 | .WillOnce(DoAll(SetEvent(&event), Return())) |
pbos | a26ac92 | 2016-02-25 04:50:01 -0800 | [diff] [blame] | 116 | .WillRepeatedly(Return()); |
tommi@webrtc.org | 0c3e12b | 2015-02-06 09:44:12 +0000 | [diff] [blame] | 117 | |
tommi | dea489f | 2017-03-03 03:20:24 -0800 | [diff] [blame] | 118 | thread.RegisterModule(&module, RTC_FROM_HERE); |
tommi@webrtc.org | 3985f01 | 2015-02-27 13:36:34 +0000 | [diff] [blame] | 119 | |
| 120 | EXPECT_CALL(module, ProcessThreadAttached(&thread)).Times(1); |
| 121 | thread.Start(); |
Niels Möller | 2c16cc6 | 2018-10-29 09:47:51 +0100 | [diff] [blame] | 122 | EXPECT_TRUE(event.Wait(kEventWaitTimeout)); |
tommi@webrtc.org | 3985f01 | 2015-02-27 13:36:34 +0000 | [diff] [blame] | 123 | |
| 124 | EXPECT_CALL(module, ProcessThreadAttached(nullptr)).Times(1); |
| 125 | thread.Stop(); |
tommi@webrtc.org | 0c3e12b | 2015-02-06 09:44:12 +0000 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | // Tests setting up a module for callbacks and then unregister that module. |
| 129 | // After unregistration, we should not receive any further callbacks. |
| 130 | TEST(ProcessThreadImpl, Deregister) { |
stefan | 847855b | 2015-09-11 09:52:15 -0700 | [diff] [blame] | 131 | ProcessThreadImpl thread("ProcessThread"); |
Niels Möller | c572ff3 | 2018-11-07 08:43:50 +0100 | [diff] [blame] | 132 | rtc::Event event; |
tommi@webrtc.org | 0c3e12b | 2015-02-06 09:44:12 +0000 | [diff] [blame] | 133 | |
| 134 | int process_count = 0; |
| 135 | MockModule module; |
tommi | 82ead60 | 2017-02-19 16:09:55 -0800 | [diff] [blame] | 136 | EXPECT_CALL(module, TimeUntilNextProcess()) |
| 137 | .WillOnce(Return(0)) |
| 138 | .WillRepeatedly(Return(1)); |
tommi@webrtc.org | 0c3e12b | 2015-02-06 09:44:12 +0000 | [diff] [blame] | 139 | EXPECT_CALL(module, Process()) |
Niels Möller | 2c16cc6 | 2018-10-29 09:47:51 +0100 | [diff] [blame] | 140 | .WillOnce(DoAll(SetEvent(&event), Increment(&process_count), Return())) |
pbos | a26ac92 | 2016-02-25 04:50:01 -0800 | [diff] [blame] | 141 | .WillRepeatedly(DoAll(Increment(&process_count), Return())); |
tommi@webrtc.org | 0c3e12b | 2015-02-06 09:44:12 +0000 | [diff] [blame] | 142 | |
tommi | dea489f | 2017-03-03 03:20:24 -0800 | [diff] [blame] | 143 | thread.RegisterModule(&module, RTC_FROM_HERE); |
tommi@webrtc.org | 3985f01 | 2015-02-27 13:36:34 +0000 | [diff] [blame] | 144 | |
| 145 | EXPECT_CALL(module, ProcessThreadAttached(&thread)).Times(1); |
| 146 | thread.Start(); |
tommi@webrtc.org | 0c3e12b | 2015-02-06 09:44:12 +0000 | [diff] [blame] | 147 | |
Niels Möller | 2c16cc6 | 2018-10-29 09:47:51 +0100 | [diff] [blame] | 148 | EXPECT_TRUE(event.Wait(kEventWaitTimeout)); |
tommi@webrtc.org | 3985f01 | 2015-02-27 13:36:34 +0000 | [diff] [blame] | 149 | |
| 150 | EXPECT_CALL(module, ProcessThreadAttached(nullptr)).Times(1); |
| 151 | thread.DeRegisterModule(&module); |
| 152 | |
tommi@webrtc.org | 0c3e12b | 2015-02-06 09:44:12 +0000 | [diff] [blame] | 153 | EXPECT_GE(process_count, 1); |
| 154 | int count_after_deregister = process_count; |
| 155 | |
| 156 | // We shouldn't get any more callbacks. |
Niels Möller | 2c16cc6 | 2018-10-29 09:47:51 +0100 | [diff] [blame] | 157 | EXPECT_FALSE(event.Wait(20)); |
tommi@webrtc.org | 0c3e12b | 2015-02-06 09:44:12 +0000 | [diff] [blame] | 158 | EXPECT_EQ(count_after_deregister, process_count); |
tommi@webrtc.org | 3985f01 | 2015-02-27 13:36:34 +0000 | [diff] [blame] | 159 | thread.Stop(); |
tommi@webrtc.org | 0c3e12b | 2015-02-06 09:44:12 +0000 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | // Helper function for testing receiving a callback after a certain amount of |
| 163 | // time. There's some variance of timing built into it to reduce chance of |
| 164 | // flakiness on bots. |
| 165 | void ProcessCallAfterAFewMs(int64_t milliseconds) { |
stefan | 847855b | 2015-09-11 09:52:15 -0700 | [diff] [blame] | 166 | ProcessThreadImpl thread("ProcessThread"); |
tommi@webrtc.org | 3985f01 | 2015-02-27 13:36:34 +0000 | [diff] [blame] | 167 | thread.Start(); |
tommi@webrtc.org | 0c3e12b | 2015-02-06 09:44:12 +0000 | [diff] [blame] | 168 | |
Niels Möller | c572ff3 | 2018-11-07 08:43:50 +0100 | [diff] [blame] | 169 | rtc::Event event; |
tommi@webrtc.org | 0c3e12b | 2015-02-06 09:44:12 +0000 | [diff] [blame] | 170 | |
| 171 | MockModule module; |
| 172 | int64_t start_time = 0; |
| 173 | int64_t called_time = 0; |
| 174 | EXPECT_CALL(module, TimeUntilNextProcess()) |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 175 | .WillOnce(DoAll(SetTimestamp(&start_time), Return(milliseconds))) |
tommi@webrtc.org | 0c3e12b | 2015-02-06 09:44:12 +0000 | [diff] [blame] | 176 | .WillRepeatedly(Return(milliseconds)); |
| 177 | EXPECT_CALL(module, Process()) |
Niels Möller | 2c16cc6 | 2018-10-29 09:47:51 +0100 | [diff] [blame] | 178 | .WillOnce(DoAll(SetTimestamp(&called_time), SetEvent(&event), Return())) |
pbos | a26ac92 | 2016-02-25 04:50:01 -0800 | [diff] [blame] | 179 | .WillRepeatedly(Return()); |
tommi@webrtc.org | 0c3e12b | 2015-02-06 09:44:12 +0000 | [diff] [blame] | 180 | |
tommi@webrtc.org | 3985f01 | 2015-02-27 13:36:34 +0000 | [diff] [blame] | 181 | EXPECT_CALL(module, ProcessThreadAttached(&thread)).Times(1); |
tommi | dea489f | 2017-03-03 03:20:24 -0800 | [diff] [blame] | 182 | thread.RegisterModule(&module, RTC_FROM_HERE); |
tommi@webrtc.org | 0c3e12b | 2015-02-06 09:44:12 +0000 | [diff] [blame] | 183 | |
| 184 | // Add a buffer of 50ms due to slowness of some trybots |
| 185 | // (e.g. win_drmemory_light) |
Niels Möller | 2c16cc6 | 2018-10-29 09:47:51 +0100 | [diff] [blame] | 186 | EXPECT_TRUE(event.Wait(milliseconds + 50)); |
tommi@webrtc.org | 3985f01 | 2015-02-27 13:36:34 +0000 | [diff] [blame] | 187 | |
| 188 | EXPECT_CALL(module, ProcessThreadAttached(nullptr)).Times(1); |
| 189 | thread.Stop(); |
tommi@webrtc.org | 0c3e12b | 2015-02-06 09:44:12 +0000 | [diff] [blame] | 190 | |
| 191 | ASSERT_GT(start_time, 0); |
| 192 | ASSERT_GT(called_time, 0); |
| 193 | // Use >= instead of > since due to rounding and timer accuracy (or lack |
| 194 | // thereof), can make the test run in "0"ms time. |
| 195 | EXPECT_GE(called_time, start_time); |
| 196 | // Check for an acceptable range. |
kwiberg@webrtc.org | 11426dc | 2015-02-11 14:30:34 +0000 | [diff] [blame] | 197 | uint32_t diff = called_time - start_time; |
tommi@webrtc.org | 0c3e12b | 2015-02-06 09:44:12 +0000 | [diff] [blame] | 198 | EXPECT_GE(diff, milliseconds - 15); |
| 199 | EXPECT_LT(diff, milliseconds + 15); |
| 200 | } |
| 201 | |
tommi@webrtc.org | 1d4830a | 2015-02-07 08:44:28 +0000 | [diff] [blame] | 202 | // DISABLED for now since the virtual build bots are too slow :( |
| 203 | // TODO(tommi): Fix. |
| 204 | TEST(ProcessThreadImpl, DISABLED_ProcessCallAfter5ms) { |
tommi@webrtc.org | 0c3e12b | 2015-02-06 09:44:12 +0000 | [diff] [blame] | 205 | ProcessCallAfterAFewMs(5); |
| 206 | } |
| 207 | |
tommi@webrtc.org | 1d4830a | 2015-02-07 08:44:28 +0000 | [diff] [blame] | 208 | // DISABLED for now since the virtual build bots are too slow :( |
| 209 | // TODO(tommi): Fix. |
| 210 | TEST(ProcessThreadImpl, DISABLED_ProcessCallAfter50ms) { |
tommi@webrtc.org | 0c3e12b | 2015-02-06 09:44:12 +0000 | [diff] [blame] | 211 | ProcessCallAfterAFewMs(50); |
| 212 | } |
| 213 | |
tommi@webrtc.org | 1d4830a | 2015-02-07 08:44:28 +0000 | [diff] [blame] | 214 | // DISABLED for now since the virtual build bots are too slow :( |
| 215 | // TODO(tommi): Fix. |
| 216 | TEST(ProcessThreadImpl, DISABLED_ProcessCallAfter200ms) { |
tommi@webrtc.org | 0c3e12b | 2015-02-06 09:44:12 +0000 | [diff] [blame] | 217 | ProcessCallAfterAFewMs(200); |
| 218 | } |
| 219 | |
| 220 | // Runs callbacks with the goal of getting up to 50 callbacks within a second |
| 221 | // (on average 1 callback every 20ms). On real hardware, we're usually pretty |
| 222 | // close to that, but the test bots that run on virtual machines, will |
| 223 | // typically be in the range 30-40 callbacks. |
tommi@webrtc.org | 1d4830a | 2015-02-07 08:44:28 +0000 | [diff] [blame] | 224 | // DISABLED for now since this can take up to 2 seconds to run on the slowest |
| 225 | // build bots. |
| 226 | // TODO(tommi): Fix. |
| 227 | TEST(ProcessThreadImpl, DISABLED_Process50Times) { |
stefan | 847855b | 2015-09-11 09:52:15 -0700 | [diff] [blame] | 228 | ProcessThreadImpl thread("ProcessThread"); |
tommi@webrtc.org | 3985f01 | 2015-02-27 13:36:34 +0000 | [diff] [blame] | 229 | thread.Start(); |
tommi@webrtc.org | 0c3e12b | 2015-02-06 09:44:12 +0000 | [diff] [blame] | 230 | |
Niels Möller | c572ff3 | 2018-11-07 08:43:50 +0100 | [diff] [blame] | 231 | rtc::Event event; |
tommi@webrtc.org | 0c3e12b | 2015-02-06 09:44:12 +0000 | [diff] [blame] | 232 | |
| 233 | MockModule module; |
| 234 | int callback_count = 0; |
| 235 | // Ask for a callback after 20ms. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 236 | EXPECT_CALL(module, TimeUntilNextProcess()).WillRepeatedly(Return(20)); |
tommi@webrtc.org | 0c3e12b | 2015-02-06 09:44:12 +0000 | [diff] [blame] | 237 | EXPECT_CALL(module, Process()) |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 238 | .WillRepeatedly(DoAll(Increment(&callback_count), Return())); |
tommi@webrtc.org | 0c3e12b | 2015-02-06 09:44:12 +0000 | [diff] [blame] | 239 | |
tommi@webrtc.org | 3985f01 | 2015-02-27 13:36:34 +0000 | [diff] [blame] | 240 | EXPECT_CALL(module, ProcessThreadAttached(&thread)).Times(1); |
tommi | dea489f | 2017-03-03 03:20:24 -0800 | [diff] [blame] | 241 | thread.RegisterModule(&module, RTC_FROM_HERE); |
tommi@webrtc.org | 0c3e12b | 2015-02-06 09:44:12 +0000 | [diff] [blame] | 242 | |
Niels Möller | 2c16cc6 | 2018-10-29 09:47:51 +0100 | [diff] [blame] | 243 | EXPECT_TRUE(event.Wait(1000)); |
tommi@webrtc.org | 3985f01 | 2015-02-27 13:36:34 +0000 | [diff] [blame] | 244 | |
| 245 | EXPECT_CALL(module, ProcessThreadAttached(nullptr)).Times(1); |
| 246 | thread.Stop(); |
tommi@webrtc.org | 0c3e12b | 2015-02-06 09:44:12 +0000 | [diff] [blame] | 247 | |
| 248 | printf("Callback count: %i\n", callback_count); |
| 249 | // Check that we got called back up to 50 times. |
| 250 | // Some of the try bots run on slow virtual machines, so the lower bound |
| 251 | // is much more relaxed to avoid flakiness. |
| 252 | EXPECT_GE(callback_count, 25); |
| 253 | EXPECT_LE(callback_count, 50); |
| 254 | } |
| 255 | |
| 256 | // Tests that we can wake up the worker thread to give us a callback right |
| 257 | // away when we know the thread is sleeping. |
| 258 | TEST(ProcessThreadImpl, WakeUp) { |
stefan | 847855b | 2015-09-11 09:52:15 -0700 | [diff] [blame] | 259 | ProcessThreadImpl thread("ProcessThread"); |
tommi@webrtc.org | 3985f01 | 2015-02-27 13:36:34 +0000 | [diff] [blame] | 260 | thread.Start(); |
tommi@webrtc.org | 0c3e12b | 2015-02-06 09:44:12 +0000 | [diff] [blame] | 261 | |
Niels Möller | c572ff3 | 2018-11-07 08:43:50 +0100 | [diff] [blame] | 262 | rtc::Event started; |
| 263 | rtc::Event called; |
tommi@webrtc.org | 0c3e12b | 2015-02-06 09:44:12 +0000 | [diff] [blame] | 264 | |
| 265 | MockModule module; |
thaloun | 2935e01 | 2015-11-17 15:02:44 -0800 | [diff] [blame] | 266 | int64_t start_time; |
| 267 | int64_t called_time; |
| 268 | |
tommi@webrtc.org | 3985f01 | 2015-02-27 13:36:34 +0000 | [diff] [blame] | 269 | // Ask for a callback after 1000ms. |
| 270 | // TimeUntilNextProcess will be called twice. |
| 271 | // The first time we use it to get the thread into a waiting state. |
| 272 | // Then we wake the thread and there should not be another call made to |
| 273 | // TimeUntilNextProcess before Process() is called. |
| 274 | // The second time TimeUntilNextProcess is then called, is after Process |
| 275 | // has been called and we don't expect any more calls. |
tommi@webrtc.org | 0c3e12b | 2015-02-06 09:44:12 +0000 | [diff] [blame] | 276 | EXPECT_CALL(module, TimeUntilNextProcess()) |
Niels Möller | 2c16cc6 | 2018-10-29 09:47:51 +0100 | [diff] [blame] | 277 | .WillOnce( |
| 278 | DoAll(SetTimestamp(&start_time), SetEvent(&started), Return(1000))) |
tommi@webrtc.org | 3985f01 | 2015-02-27 13:36:34 +0000 | [diff] [blame] | 279 | .WillOnce(Return(1000)); |
tommi@webrtc.org | 0c3e12b | 2015-02-06 09:44:12 +0000 | [diff] [blame] | 280 | EXPECT_CALL(module, Process()) |
Niels Möller | 2c16cc6 | 2018-10-29 09:47:51 +0100 | [diff] [blame] | 281 | .WillOnce(DoAll(SetTimestamp(&called_time), SetEvent(&called), Return())) |
pbos | a26ac92 | 2016-02-25 04:50:01 -0800 | [diff] [blame] | 282 | .WillRepeatedly(Return()); |
tommi@webrtc.org | 0c3e12b | 2015-02-06 09:44:12 +0000 | [diff] [blame] | 283 | |
tommi@webrtc.org | 3985f01 | 2015-02-27 13:36:34 +0000 | [diff] [blame] | 284 | EXPECT_CALL(module, ProcessThreadAttached(&thread)).Times(1); |
tommi | dea489f | 2017-03-03 03:20:24 -0800 | [diff] [blame] | 285 | thread.RegisterModule(&module, RTC_FROM_HERE); |
tommi@webrtc.org | 0c3e12b | 2015-02-06 09:44:12 +0000 | [diff] [blame] | 286 | |
Niels Möller | 2c16cc6 | 2018-10-29 09:47:51 +0100 | [diff] [blame] | 287 | EXPECT_TRUE(started.Wait(kEventWaitTimeout)); |
tommi@webrtc.org | 0c3e12b | 2015-02-06 09:44:12 +0000 | [diff] [blame] | 288 | thread.WakeUp(&module); |
Niels Möller | 2c16cc6 | 2018-10-29 09:47:51 +0100 | [diff] [blame] | 289 | EXPECT_TRUE(called.Wait(kEventWaitTimeout)); |
tommi@webrtc.org | 3985f01 | 2015-02-27 13:36:34 +0000 | [diff] [blame] | 290 | |
| 291 | EXPECT_CALL(module, ProcessThreadAttached(nullptr)).Times(1); |
| 292 | thread.Stop(); |
tommi@webrtc.org | 0c3e12b | 2015-02-06 09:44:12 +0000 | [diff] [blame] | 293 | |
tommi@webrtc.org | 0c3e12b | 2015-02-06 09:44:12 +0000 | [diff] [blame] | 294 | EXPECT_GE(called_time, start_time); |
kwiberg@webrtc.org | 11426dc | 2015-02-11 14:30:34 +0000 | [diff] [blame] | 295 | uint32_t diff = called_time - start_time; |
tommi@webrtc.org | 0c3e12b | 2015-02-06 09:44:12 +0000 | [diff] [blame] | 296 | // We should have been called back much quicker than 1sec. |
| 297 | EXPECT_LE(diff, 100u); |
| 298 | } |
| 299 | |
tommi@webrtc.org | 0305448 | 2015-03-05 13:13:42 +0000 | [diff] [blame] | 300 | // Tests that we can post a task that gets run straight away on the worker |
| 301 | // thread. |
| 302 | TEST(ProcessThreadImpl, PostTask) { |
stefan | 847855b | 2015-09-11 09:52:15 -0700 | [diff] [blame] | 303 | ProcessThreadImpl thread("ProcessThread"); |
Niels Möller | c572ff3 | 2018-11-07 08:43:50 +0100 | [diff] [blame] | 304 | rtc::Event task_ran; |
Niels Möller | 2c16cc6 | 2018-10-29 09:47:51 +0100 | [diff] [blame] | 305 | std::unique_ptr<RaiseEventTask> task(new RaiseEventTask(&task_ran)); |
tommi@webrtc.org | 0305448 | 2015-03-05 13:13:42 +0000 | [diff] [blame] | 306 | thread.Start(); |
kwiberg | 1c7fdd8 | 2016-04-26 08:18:04 -0700 | [diff] [blame] | 307 | thread.PostTask(std::move(task)); |
Niels Möller | 2c16cc6 | 2018-10-29 09:47:51 +0100 | [diff] [blame] | 308 | EXPECT_TRUE(task_ran.Wait(kEventWaitTimeout)); |
tommi@webrtc.org | 0305448 | 2015-03-05 13:13:42 +0000 | [diff] [blame] | 309 | thread.Stop(); |
| 310 | } |
| 311 | |
tommi@webrtc.org | 0c3e12b | 2015-02-06 09:44:12 +0000 | [diff] [blame] | 312 | } // namespace webrtc |