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