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