blob: 0029acf5d966b7ee6bab05f4ba4e474dab179387 [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001/*
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
jbauch555604a2016-04-26 03:13:22 -070011#include <memory>
12
kwiberg4485ffb2016-04-26 08:14:39 -070013#include "webrtc/base/constructormagic.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000014#include "webrtc/base/gunit.h"
15#include "webrtc/base/signalthread.h"
16#include "webrtc/base/thread.h"
17
18using namespace rtc;
19
deadbeefd50f4f32016-06-03 10:30:58 -070020// 10 seconds.
21static const int kTimeout = 10000;
22
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000023class 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.orge5063b12014-05-23 17:28:50 +000042 EXPECT_FALSE(worker()->RunningForTest()); // not started yet
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000043 }
44
45 virtual void OnWorkStop() {
46 ++harness_->thread_stopped_;
47 EXPECT_EQ(harness_->main_thread_, Thread::Current());
fischman@webrtc.orge5063b12014-05-23 17:28:50 +000048 EXPECT_TRUE(worker()->RunningForTest()); // not stopped yet
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000049 }
50
51 virtual void OnWorkDone() {
52 ++harness_->thread_done_;
53 EXPECT_EQ(harness_->main_thread_, Thread::Current());
fischman@webrtc.orge5063b12014-05-23 17:28:50 +000054 EXPECT_TRUE(worker()->RunningForTest()); // not stopped yet
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000055 }
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_;
henrikg3c089d72015-09-16 05:37:44 -070065 RTC_DISALLOW_COPY_AND_ASSIGN(SlowSignalThread);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000066 };
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
105class 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_;
henrikg3c089d72015-09-16 05:37:44 -0700136 RTC_DISALLOW_COPY_AND_ASSIGN(OwnerThread);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000137};
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.orgc732a3e2014-10-09 22:08:15 +0000142TEST_F(SignalThreadTest, OwnerThreadGoesAway) {
deadbeeff5f03e82016-06-06 11:16:06 -0700143 // We don't use |thread_| for this test, so destroy it.
144 thread_->Destroy(true);
145
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000146 {
jbauch555604a2016-04-26 03:13:22 -0700147 std::unique_ptr<OwnerThread> owner(new OwnerThread(this));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000148 main_thread_ = owner.get();
149 owner->Start();
150 while (!owner->has_run()) {
151 Thread::Current()->socketserver()->Wait(10, false);
152 }
153 }
154 // At this point the main thread has gone away.
155 // Give the SignalThread a little time to do its callback,
156 // which will crash if the signal thread doesn't handle
157 // this situation well.
158 Thread::Current()->socketserver()->Wait(500, false);
159}
160
161#define EXPECT_STATE(started, done, completed, stopped, deleted) \
162 EXPECT_EQ(started, thread_started_); \
163 EXPECT_EQ(done, thread_done_); \
164 EXPECT_EQ(completed, thread_completed_); \
165 EXPECT_EQ(stopped, thread_stopped_); \
166 EXPECT_EQ(deleted, thread_deleted_);
167
deadbeefd50f4f32016-06-03 10:30:58 -0700168#define EXPECT_STATE_WAIT(started, done, completed, stopped, deleted, timeout) \
169 EXPECT_EQ_WAIT(started, thread_started_, timeout); \
170 EXPECT_EQ_WAIT(done, thread_done_, timeout); \
171 EXPECT_EQ_WAIT(completed, thread_completed_, timeout); \
172 EXPECT_EQ_WAIT(stopped, thread_stopped_, timeout); \
173 EXPECT_EQ_WAIT(deleted, thread_deleted_, timeout);
174
175TEST_F(SignalThreadTest, ThreadFinishes) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000176 thread_->Start();
177 EXPECT_STATE(1, 0, 0, 0, 0);
deadbeefd50f4f32016-06-03 10:30:58 -0700178 EXPECT_STATE_WAIT(1, 1, 1, 0, 1, kTimeout);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000179}
180
deadbeefd50f4f32016-06-03 10:30:58 -0700181TEST_F(SignalThreadTest, ReleasedThreadFinishes) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000182 thread_->Start();
183 EXPECT_STATE(1, 0, 0, 0, 0);
184 thread_->Release();
185 called_release_ = true;
186 EXPECT_STATE(1, 0, 0, 0, 0);
deadbeefd50f4f32016-06-03 10:30:58 -0700187 EXPECT_STATE_WAIT(1, 1, 1, 0, 1, kTimeout);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000188}
189
henrike@webrtc.orgc732a3e2014-10-09 22:08:15 +0000190TEST_F(SignalThreadTest, DestroyedThreadCleansUp) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000191 thread_->Start();
192 EXPECT_STATE(1, 0, 0, 0, 0);
193 thread_->Destroy(true);
194 EXPECT_STATE(1, 0, 0, 1, 1);
195 Thread::Current()->ProcessMessages(0);
196 EXPECT_STATE(1, 0, 0, 1, 1);
197}
198
henrike@webrtc.orgc732a3e2014-10-09 22:08:15 +0000199TEST_F(SignalThreadTest, DeferredDestroyedThreadCleansUp) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000200 thread_->Start();
201 EXPECT_STATE(1, 0, 0, 0, 0);
202 thread_->Destroy(false);
203 EXPECT_STATE(1, 0, 0, 1, 0);
deadbeefd50f4f32016-06-03 10:30:58 -0700204 EXPECT_STATE_WAIT(1, 1, 0, 1, 1, kTimeout);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000205}