henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [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 | |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 11 | #include <memory> |
| 12 | |
kwiberg | 4485ffb | 2016-04-26 08:14:39 -0700 | [diff] [blame] | 13 | #include "webrtc/base/constructormagic.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 14 | #include "webrtc/base/gunit.h" |
| 15 | #include "webrtc/base/signalthread.h" |
| 16 | #include "webrtc/base/thread.h" |
| 17 | |
| 18 | using namespace rtc; |
| 19 | |
deadbeef | d50f4f3 | 2016-06-03 10:30:58 -0700 | [diff] [blame] | 20 | // 10 seconds. |
| 21 | static const int kTimeout = 10000; |
| 22 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 23 | class SignalThreadTest : public testing::Test, public sigslot::has_slots<> { |
| 24 | public: |
| 25 | class SlowSignalThread : public SignalThread { |
| 26 | public: |
| 27 | SlowSignalThread(SignalThreadTest* harness) : harness_(harness) { |
| 28 | } |
| 29 | |
| 30 | virtual ~SlowSignalThread() { |
| 31 | EXPECT_EQ(harness_->main_thread_, Thread::Current()); |
| 32 | ++harness_->thread_deleted_; |
| 33 | } |
| 34 | |
| 35 | const SignalThreadTest* harness() { return harness_; } |
| 36 | |
| 37 | protected: |
| 38 | virtual void OnWorkStart() { |
| 39 | ASSERT_TRUE(harness_ != NULL); |
| 40 | ++harness_->thread_started_; |
| 41 | EXPECT_EQ(harness_->main_thread_, Thread::Current()); |
fischman@webrtc.org | e5063b1 | 2014-05-23 17:28:50 +0000 | [diff] [blame] | 42 | EXPECT_FALSE(worker()->RunningForTest()); // not started yet |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | virtual void OnWorkStop() { |
| 46 | ++harness_->thread_stopped_; |
| 47 | EXPECT_EQ(harness_->main_thread_, Thread::Current()); |
fischman@webrtc.org | e5063b1 | 2014-05-23 17:28:50 +0000 | [diff] [blame] | 48 | EXPECT_TRUE(worker()->RunningForTest()); // not stopped yet |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | virtual void OnWorkDone() { |
| 52 | ++harness_->thread_done_; |
| 53 | EXPECT_EQ(harness_->main_thread_, Thread::Current()); |
fischman@webrtc.org | e5063b1 | 2014-05-23 17:28:50 +0000 | [diff] [blame] | 54 | EXPECT_TRUE(worker()->RunningForTest()); // not stopped yet |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | virtual void DoWork() { |
| 58 | EXPECT_NE(harness_->main_thread_, Thread::Current()); |
| 59 | EXPECT_EQ(worker(), Thread::Current()); |
| 60 | Thread::Current()->socketserver()->Wait(250, false); |
| 61 | } |
| 62 | |
| 63 | private: |
| 64 | SignalThreadTest* harness_; |
henrikg | 3c089d7 | 2015-09-16 05:37:44 -0700 | [diff] [blame] | 65 | RTC_DISALLOW_COPY_AND_ASSIGN(SlowSignalThread); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 66 | }; |
| 67 | |
| 68 | void OnWorkComplete(rtc::SignalThread* thread) { |
| 69 | SlowSignalThread* t = static_cast<SlowSignalThread*>(thread); |
| 70 | EXPECT_EQ(t->harness(), this); |
| 71 | EXPECT_EQ(main_thread_, Thread::Current()); |
| 72 | |
| 73 | ++thread_completed_; |
| 74 | if (!called_release_) { |
| 75 | thread->Release(); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | virtual void SetUp() { |
| 80 | main_thread_ = Thread::Current(); |
| 81 | thread_ = new SlowSignalThread(this); |
| 82 | thread_->SignalWorkDone.connect(this, &SignalThreadTest::OnWorkComplete); |
| 83 | called_release_ = false; |
| 84 | thread_started_ = 0; |
| 85 | thread_done_ = 0; |
| 86 | thread_completed_ = 0; |
| 87 | thread_stopped_ = 0; |
| 88 | thread_deleted_ = 0; |
| 89 | } |
| 90 | |
| 91 | virtual void TearDown() { |
| 92 | } |
| 93 | |
| 94 | Thread* main_thread_; |
| 95 | SlowSignalThread* thread_; |
| 96 | bool called_release_; |
| 97 | |
| 98 | int thread_started_; |
| 99 | int thread_done_; |
| 100 | int thread_completed_; |
| 101 | int thread_stopped_; |
| 102 | int thread_deleted_; |
| 103 | }; |
| 104 | |
| 105 | class OwnerThread : public Thread, public sigslot::has_slots<> { |
| 106 | public: |
| 107 | explicit OwnerThread(SignalThreadTest* harness) |
| 108 | : harness_(harness), |
| 109 | has_run_(false) { |
| 110 | } |
| 111 | |
| 112 | virtual ~OwnerThread() { |
| 113 | Stop(); |
| 114 | } |
| 115 | |
| 116 | virtual void Run() { |
| 117 | SignalThreadTest::SlowSignalThread* signal_thread = |
| 118 | new SignalThreadTest::SlowSignalThread(harness_); |
| 119 | signal_thread->SignalWorkDone.connect(this, &OwnerThread::OnWorkDone); |
| 120 | signal_thread->Start(); |
| 121 | Thread::Current()->socketserver()->Wait(100, false); |
| 122 | signal_thread->Release(); |
| 123 | // Delete |signal_thread|. |
| 124 | signal_thread->Destroy(true); |
| 125 | has_run_ = true; |
| 126 | } |
| 127 | |
| 128 | bool has_run() { return has_run_; } |
| 129 | void OnWorkDone(SignalThread* signal_thread) { |
| 130 | FAIL() << " This shouldn't get called."; |
| 131 | } |
| 132 | |
| 133 | private: |
| 134 | SignalThreadTest* harness_; |
| 135 | bool has_run_; |
henrikg | 3c089d7 | 2015-09-16 05:37:44 -0700 | [diff] [blame] | 136 | RTC_DISALLOW_COPY_AND_ASSIGN(OwnerThread); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 137 | }; |
| 138 | |
| 139 | // Test for when the main thread goes away while the |
| 140 | // signal thread is still working. This may happen |
| 141 | // when shutting down the process. |
henrike@webrtc.org | c732a3e | 2014-10-09 22:08:15 +0000 | [diff] [blame] | 142 | TEST_F(SignalThreadTest, OwnerThreadGoesAway) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 143 | { |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 144 | std::unique_ptr<OwnerThread> owner(new OwnerThread(this)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 145 | main_thread_ = owner.get(); |
| 146 | owner->Start(); |
| 147 | while (!owner->has_run()) { |
| 148 | Thread::Current()->socketserver()->Wait(10, false); |
| 149 | } |
| 150 | } |
| 151 | // At this point the main thread has gone away. |
| 152 | // Give the SignalThread a little time to do its callback, |
| 153 | // which will crash if the signal thread doesn't handle |
| 154 | // this situation well. |
| 155 | Thread::Current()->socketserver()->Wait(500, false); |
| 156 | } |
| 157 | |
| 158 | #define EXPECT_STATE(started, done, completed, stopped, deleted) \ |
| 159 | EXPECT_EQ(started, thread_started_); \ |
| 160 | EXPECT_EQ(done, thread_done_); \ |
| 161 | EXPECT_EQ(completed, thread_completed_); \ |
| 162 | EXPECT_EQ(stopped, thread_stopped_); \ |
| 163 | EXPECT_EQ(deleted, thread_deleted_); |
| 164 | |
deadbeef | d50f4f3 | 2016-06-03 10:30:58 -0700 | [diff] [blame] | 165 | #define EXPECT_STATE_WAIT(started, done, completed, stopped, deleted, timeout) \ |
| 166 | EXPECT_EQ_WAIT(started, thread_started_, timeout); \ |
| 167 | EXPECT_EQ_WAIT(done, thread_done_, timeout); \ |
| 168 | EXPECT_EQ_WAIT(completed, thread_completed_, timeout); \ |
| 169 | EXPECT_EQ_WAIT(stopped, thread_stopped_, timeout); \ |
| 170 | EXPECT_EQ_WAIT(deleted, thread_deleted_, timeout); |
| 171 | |
| 172 | TEST_F(SignalThreadTest, ThreadFinishes) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 173 | thread_->Start(); |
| 174 | EXPECT_STATE(1, 0, 0, 0, 0); |
deadbeef | d50f4f3 | 2016-06-03 10:30:58 -0700 | [diff] [blame] | 175 | EXPECT_STATE_WAIT(1, 1, 1, 0, 1, kTimeout); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 176 | } |
| 177 | |
deadbeef | d50f4f3 | 2016-06-03 10:30:58 -0700 | [diff] [blame] | 178 | TEST_F(SignalThreadTest, ReleasedThreadFinishes) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 179 | thread_->Start(); |
| 180 | EXPECT_STATE(1, 0, 0, 0, 0); |
| 181 | thread_->Release(); |
| 182 | called_release_ = true; |
| 183 | EXPECT_STATE(1, 0, 0, 0, 0); |
deadbeef | d50f4f3 | 2016-06-03 10:30:58 -0700 | [diff] [blame] | 184 | EXPECT_STATE_WAIT(1, 1, 1, 0, 1, kTimeout); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 185 | } |
| 186 | |
henrike@webrtc.org | c732a3e | 2014-10-09 22:08:15 +0000 | [diff] [blame] | 187 | TEST_F(SignalThreadTest, DestroyedThreadCleansUp) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 188 | thread_->Start(); |
| 189 | EXPECT_STATE(1, 0, 0, 0, 0); |
| 190 | thread_->Destroy(true); |
| 191 | EXPECT_STATE(1, 0, 0, 1, 1); |
| 192 | Thread::Current()->ProcessMessages(0); |
| 193 | EXPECT_STATE(1, 0, 0, 1, 1); |
| 194 | } |
| 195 | |
henrike@webrtc.org | c732a3e | 2014-10-09 22:08:15 +0000 | [diff] [blame] | 196 | TEST_F(SignalThreadTest, DeferredDestroyedThreadCleansUp) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 197 | thread_->Start(); |
| 198 | EXPECT_STATE(1, 0, 0, 0, 0); |
| 199 | thread_->Destroy(false); |
| 200 | EXPECT_STATE(1, 0, 0, 1, 0); |
deadbeef | d50f4f3 | 2016-06-03 10:30:58 -0700 | [diff] [blame] | 201 | EXPECT_STATE_WAIT(1, 1, 0, 1, 1, kTimeout); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 202 | } |