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