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 | |
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_ != nullptr); | |
40 ++harness_->thread_started_; | |
41 EXPECT_EQ(harness_->main_thread_, Thread::Current()); | |
42 EXPECT_FALSE(worker()->RunningForTest()); // not started yet | |
43 } | |
44 | |
45 virtual void OnWorkStop() { | |
46 ++harness_->thread_stopped_; | |
47 EXPECT_EQ(harness_->main_thread_, Thread::Current()); | |
48 EXPECT_TRUE(worker()->RunningForTest()); // not stopped yet | |
49 } | |
50 | |
51 virtual void OnWorkDone() { | |
52 ++harness_->thread_done_; | |
53 EXPECT_EQ(harness_->main_thread_, Thread::Current()); | |
54 EXPECT_TRUE(worker()->RunningForTest()); // not stopped yet | |
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_; | |
65 RTC_DISALLOW_COPY_AND_ASSIGN(SlowSignalThread); | |
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_; | |
136 RTC_DISALLOW_COPY_AND_ASSIGN(OwnerThread); | |
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. | |
142 TEST_F(SignalThreadTest, OwnerThreadGoesAway) { | |
143 // We don't use |thread_| for this test, so destroy it. | |
144 thread_->Destroy(true); | |
145 | |
146 { | |
147 std::unique_ptr<OwnerThread> owner(new OwnerThread(this)); | |
148 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 | |
168 #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 | |
175 TEST_F(SignalThreadTest, ThreadFinishes) { | |
176 thread_->Start(); | |
177 EXPECT_STATE(1, 0, 0, 0, 0); | |
178 EXPECT_STATE_WAIT(1, 1, 1, 0, 1, kTimeout); | |
179 } | |
180 | |
181 TEST_F(SignalThreadTest, ReleasedThreadFinishes) { | |
182 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); | |
187 EXPECT_STATE_WAIT(1, 1, 1, 0, 1, kTimeout); | |
188 } | |
189 | |
190 TEST_F(SignalThreadTest, DestroyedThreadCleansUp) { | |
191 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 | |
199 TEST_F(SignalThreadTest, DeferredDestroyedThreadCleansUp) { | |
200 thread_->Start(); | |
201 EXPECT_STATE(1, 0, 0, 0, 0); | |
202 thread_->Destroy(false); | |
203 EXPECT_STATE(1, 0, 0, 1, 0); | |
204 EXPECT_STATE_WAIT(1, 1, 0, 1, 1, kTimeout); | |
205 } | |
OLD | NEW |