deadbeef | 8290ddf | 2017-07-11 16:56:05 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2004 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 "rtc_base/signal_thread.h" |
| 12 | |
deadbeef | 8290ddf | 2017-07-11 16:56:05 -0700 | [diff] [blame] | 13 | #include <memory> |
| 14 | |
Niels Möller | 5a96a0e | 2019-04-30 11:45:58 +0200 | [diff] [blame] | 15 | #include "absl/memory/memory.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 16 | #include "rtc_base/constructor_magic.h" |
Yves Gerey | c2accf2 | 2019-09-06 01:26:09 +0200 | [diff] [blame^] | 17 | #include "rtc_base/critical_section.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 18 | #include "rtc_base/gunit.h" |
Niels Möller | 5a96a0e | 2019-04-30 11:45:58 +0200 | [diff] [blame] | 19 | #include "rtc_base/null_socket_server.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 20 | #include "rtc_base/thread.h" |
Yves Gerey | c2accf2 | 2019-09-06 01:26:09 +0200 | [diff] [blame^] | 21 | #include "rtc_base/thread_annotations.h" |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 22 | #include "test/gtest.h" |
deadbeef | 8290ddf | 2017-07-11 16:56:05 -0700 | [diff] [blame] | 23 | |
Mirko Bonadei | e10b163 | 2018-12-11 18:43:40 +0100 | [diff] [blame] | 24 | namespace rtc { |
| 25 | namespace { |
deadbeef | 8290ddf | 2017-07-11 16:56:05 -0700 | [diff] [blame] | 26 | |
| 27 | // 10 seconds. |
| 28 | static const int kTimeout = 10000; |
| 29 | |
Mirko Bonadei | 6a489f2 | 2019-04-09 15:11:12 +0200 | [diff] [blame] | 30 | class SignalThreadTest : public ::testing::Test, public sigslot::has_slots<> { |
deadbeef | 8290ddf | 2017-07-11 16:56:05 -0700 | [diff] [blame] | 31 | public: |
| 32 | class SlowSignalThread : public SignalThread { |
| 33 | public: |
| 34 | SlowSignalThread(SignalThreadTest* harness) : harness_(harness) {} |
| 35 | |
Steve Anton | 9de3aac | 2017-10-24 10:08:26 -0700 | [diff] [blame] | 36 | ~SlowSignalThread() override { |
deadbeef | 8290ddf | 2017-07-11 16:56:05 -0700 | [diff] [blame] | 37 | EXPECT_EQ(harness_->main_thread_, Thread::Current()); |
| 38 | ++harness_->thread_deleted_; |
| 39 | } |
| 40 | |
| 41 | const SignalThreadTest* harness() { return harness_; } |
| 42 | |
| 43 | protected: |
Steve Anton | 9de3aac | 2017-10-24 10:08:26 -0700 | [diff] [blame] | 44 | void OnWorkStart() override { |
deadbeef | 8290ddf | 2017-07-11 16:56:05 -0700 | [diff] [blame] | 45 | ASSERT_TRUE(harness_ != nullptr); |
| 46 | ++harness_->thread_started_; |
| 47 | EXPECT_EQ(harness_->main_thread_, Thread::Current()); |
| 48 | EXPECT_FALSE(worker()->RunningForTest()); // not started yet |
| 49 | } |
| 50 | |
Steve Anton | 9de3aac | 2017-10-24 10:08:26 -0700 | [diff] [blame] | 51 | void OnWorkStop() override { |
deadbeef | 8290ddf | 2017-07-11 16:56:05 -0700 | [diff] [blame] | 52 | ++harness_->thread_stopped_; |
| 53 | EXPECT_EQ(harness_->main_thread_, Thread::Current()); |
| 54 | EXPECT_TRUE(worker()->RunningForTest()); // not stopped yet |
| 55 | } |
| 56 | |
Steve Anton | 9de3aac | 2017-10-24 10:08:26 -0700 | [diff] [blame] | 57 | void OnWorkDone() override { |
deadbeef | 8290ddf | 2017-07-11 16:56:05 -0700 | [diff] [blame] | 58 | ++harness_->thread_done_; |
| 59 | EXPECT_EQ(harness_->main_thread_, Thread::Current()); |
| 60 | EXPECT_TRUE(worker()->RunningForTest()); // not stopped yet |
| 61 | } |
| 62 | |
Steve Anton | 9de3aac | 2017-10-24 10:08:26 -0700 | [diff] [blame] | 63 | void DoWork() override { |
deadbeef | 8290ddf | 2017-07-11 16:56:05 -0700 | [diff] [blame] | 64 | EXPECT_NE(harness_->main_thread_, Thread::Current()); |
| 65 | EXPECT_EQ(worker(), Thread::Current()); |
| 66 | Thread::Current()->socketserver()->Wait(250, false); |
| 67 | } |
| 68 | |
| 69 | private: |
| 70 | SignalThreadTest* harness_; |
| 71 | RTC_DISALLOW_COPY_AND_ASSIGN(SlowSignalThread); |
| 72 | }; |
| 73 | |
| 74 | void OnWorkComplete(rtc::SignalThread* thread) { |
| 75 | SlowSignalThread* t = static_cast<SlowSignalThread*>(thread); |
| 76 | EXPECT_EQ(t->harness(), this); |
| 77 | EXPECT_EQ(main_thread_, Thread::Current()); |
| 78 | |
| 79 | ++thread_completed_; |
| 80 | if (!called_release_) { |
| 81 | thread->Release(); |
| 82 | } |
| 83 | } |
| 84 | |
Steve Anton | 9de3aac | 2017-10-24 10:08:26 -0700 | [diff] [blame] | 85 | void SetUp() override { |
deadbeef | 8290ddf | 2017-07-11 16:56:05 -0700 | [diff] [blame] | 86 | main_thread_ = Thread::Current(); |
| 87 | thread_ = new SlowSignalThread(this); |
| 88 | thread_->SignalWorkDone.connect(this, &SignalThreadTest::OnWorkComplete); |
| 89 | called_release_ = false; |
| 90 | thread_started_ = 0; |
| 91 | thread_done_ = 0; |
| 92 | thread_completed_ = 0; |
| 93 | thread_stopped_ = 0; |
| 94 | thread_deleted_ = 0; |
| 95 | } |
| 96 | |
deadbeef | 8290ddf | 2017-07-11 16:56:05 -0700 | [diff] [blame] | 97 | void ExpectState(int started, |
| 98 | int done, |
| 99 | int completed, |
| 100 | int stopped, |
| 101 | int deleted) { |
| 102 | EXPECT_EQ(started, thread_started_); |
| 103 | EXPECT_EQ(done, thread_done_); |
| 104 | EXPECT_EQ(completed, thread_completed_); |
| 105 | EXPECT_EQ(stopped, thread_stopped_); |
| 106 | EXPECT_EQ(deleted, thread_deleted_); |
| 107 | } |
| 108 | |
| 109 | void ExpectStateWait(int started, |
| 110 | int done, |
| 111 | int completed, |
| 112 | int stopped, |
| 113 | int deleted, |
| 114 | int timeout) { |
| 115 | EXPECT_EQ_WAIT(started, thread_started_, timeout); |
| 116 | EXPECT_EQ_WAIT(done, thread_done_, timeout); |
| 117 | EXPECT_EQ_WAIT(completed, thread_completed_, timeout); |
| 118 | EXPECT_EQ_WAIT(stopped, thread_stopped_, timeout); |
| 119 | EXPECT_EQ_WAIT(deleted, thread_deleted_, timeout); |
| 120 | } |
| 121 | |
| 122 | Thread* main_thread_; |
| 123 | SlowSignalThread* thread_; |
| 124 | bool called_release_; |
| 125 | |
| 126 | int thread_started_; |
| 127 | int thread_done_; |
| 128 | int thread_completed_; |
| 129 | int thread_stopped_; |
| 130 | int thread_deleted_; |
| 131 | }; |
| 132 | |
| 133 | class OwnerThread : public Thread, public sigslot::has_slots<> { |
| 134 | public: |
| 135 | explicit OwnerThread(SignalThreadTest* harness) |
Niels Möller | 5a96a0e | 2019-04-30 11:45:58 +0200 | [diff] [blame] | 136 | : Thread(absl::make_unique<NullSocketServer>()), |
| 137 | harness_(harness), |
| 138 | has_run_(false) {} |
deadbeef | 8290ddf | 2017-07-11 16:56:05 -0700 | [diff] [blame] | 139 | |
Steve Anton | 9de3aac | 2017-10-24 10:08:26 -0700 | [diff] [blame] | 140 | ~OwnerThread() override { Stop(); } |
deadbeef | 8290ddf | 2017-07-11 16:56:05 -0700 | [diff] [blame] | 141 | |
Steve Anton | 9de3aac | 2017-10-24 10:08:26 -0700 | [diff] [blame] | 142 | void Run() override { |
deadbeef | 8290ddf | 2017-07-11 16:56:05 -0700 | [diff] [blame] | 143 | SignalThreadTest::SlowSignalThread* signal_thread = |
| 144 | new SignalThreadTest::SlowSignalThread(harness_); |
| 145 | signal_thread->SignalWorkDone.connect(this, &OwnerThread::OnWorkDone); |
| 146 | signal_thread->Start(); |
| 147 | Thread::Current()->socketserver()->Wait(100, false); |
| 148 | signal_thread->Release(); |
| 149 | // Delete |signal_thread|. |
| 150 | signal_thread->Destroy(true); |
Yves Gerey | c2accf2 | 2019-09-06 01:26:09 +0200 | [diff] [blame^] | 151 | { |
| 152 | rtc::CritScope cs(&crit_); |
| 153 | has_run_ = true; |
| 154 | } |
deadbeef | 8290ddf | 2017-07-11 16:56:05 -0700 | [diff] [blame] | 155 | } |
| 156 | |
Yves Gerey | c2accf2 | 2019-09-06 01:26:09 +0200 | [diff] [blame^] | 157 | bool has_run() { |
| 158 | rtc::CritScope cs(&crit_); |
| 159 | return has_run_; |
| 160 | } |
| 161 | void OnWorkDone(SignalThread* /*signal_thread*/) { |
deadbeef | 8290ddf | 2017-07-11 16:56:05 -0700 | [diff] [blame] | 162 | FAIL() << " This shouldn't get called."; |
| 163 | } |
| 164 | |
| 165 | private: |
Yves Gerey | c2accf2 | 2019-09-06 01:26:09 +0200 | [diff] [blame^] | 166 | rtc::CriticalSection crit_; |
deadbeef | 8290ddf | 2017-07-11 16:56:05 -0700 | [diff] [blame] | 167 | SignalThreadTest* harness_; |
Yves Gerey | c2accf2 | 2019-09-06 01:26:09 +0200 | [diff] [blame^] | 168 | bool has_run_ RTC_GUARDED_BY(crit_); |
deadbeef | 8290ddf | 2017-07-11 16:56:05 -0700 | [diff] [blame] | 169 | RTC_DISALLOW_COPY_AND_ASSIGN(OwnerThread); |
| 170 | }; |
| 171 | |
| 172 | // Test for when the main thread goes away while the |
| 173 | // signal thread is still working. This may happen |
| 174 | // when shutting down the process. |
| 175 | TEST_F(SignalThreadTest, OwnerThreadGoesAway) { |
| 176 | // We don't use |thread_| for this test, so destroy it. |
| 177 | thread_->Destroy(true); |
| 178 | |
| 179 | { |
| 180 | std::unique_ptr<OwnerThread> owner(new OwnerThread(this)); |
| 181 | main_thread_ = owner.get(); |
| 182 | owner->Start(); |
| 183 | while (!owner->has_run()) { |
| 184 | Thread::Current()->socketserver()->Wait(10, false); |
| 185 | } |
| 186 | } |
| 187 | // At this point the main thread has gone away. |
| 188 | // Give the SignalThread a little time to do its callback, |
| 189 | // which will crash if the signal thread doesn't handle |
| 190 | // this situation well. |
| 191 | Thread::Current()->socketserver()->Wait(500, false); |
| 192 | } |
| 193 | |
| 194 | TEST_F(SignalThreadTest, ThreadFinishes) { |
| 195 | thread_->Start(); |
| 196 | ExpectState(1, 0, 0, 0, 0); |
| 197 | ExpectStateWait(1, 1, 1, 0, 1, kTimeout); |
| 198 | } |
| 199 | |
| 200 | TEST_F(SignalThreadTest, ReleasedThreadFinishes) { |
| 201 | thread_->Start(); |
| 202 | ExpectState(1, 0, 0, 0, 0); |
| 203 | thread_->Release(); |
| 204 | called_release_ = true; |
| 205 | ExpectState(1, 0, 0, 0, 0); |
| 206 | ExpectStateWait(1, 1, 1, 0, 1, kTimeout); |
| 207 | } |
| 208 | |
| 209 | TEST_F(SignalThreadTest, DestroyedThreadCleansUp) { |
| 210 | thread_->Start(); |
| 211 | ExpectState(1, 0, 0, 0, 0); |
| 212 | thread_->Destroy(true); |
| 213 | ExpectState(1, 0, 0, 1, 1); |
| 214 | Thread::Current()->ProcessMessages(0); |
| 215 | ExpectState(1, 0, 0, 1, 1); |
| 216 | } |
| 217 | |
| 218 | TEST_F(SignalThreadTest, DeferredDestroyedThreadCleansUp) { |
| 219 | thread_->Start(); |
| 220 | ExpectState(1, 0, 0, 0, 0); |
| 221 | thread_->Destroy(false); |
| 222 | ExpectState(1, 0, 0, 1, 0); |
| 223 | ExpectStateWait(1, 1, 0, 1, 1, kTimeout); |
| 224 | } |
Mirko Bonadei | e10b163 | 2018-12-11 18:43:40 +0100 | [diff] [blame] | 225 | |
| 226 | } // namespace |
| 227 | } // namespace rtc |