Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(10)

Side by Side Diff: webrtc/rtc_base/thread_unittest.cc

Issue 2979963002: Revert of Make the default ctor of rtc::Thread, protected (Closed)
Patch Set: Created 3 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « webrtc/rtc_base/thread.h ('k') | webrtc/rtc_base/timeutils_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. 2 * Copyright 2004 The WebRTC Project Authors. All rights reserved.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license 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 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 6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may 7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree. 8 * be found in the AUTHORS file in the root of the source tree.
9 */ 9 */
10 10
(...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after
188 Thread::Current()->ProcessMessages(50); 188 Thread::Current()->ProcessMessages(50);
189 return 24; 189 return 24;
190 } 190 }
191 }; 191 };
192 192
193 // See: https://code.google.com/p/webrtc/issues/detail?id=2409 193 // See: https://code.google.com/p/webrtc/issues/detail?id=2409
194 TEST(ThreadTest, DISABLED_Main) { 194 TEST(ThreadTest, DISABLED_Main) {
195 const SocketAddress addr("127.0.0.1", 0); 195 const SocketAddress addr("127.0.0.1", 0);
196 196
197 // Create the messaging client on its own thread. 197 // Create the messaging client on its own thread.
198 auto th1 = Thread::CreateWithSocketServer(); 198 Thread th1;
199 Socket* socket = 199 Socket* socket = th1.socketserver()->CreateAsyncSocket(addr.family(),
200 th1->socketserver()->CreateAsyncSocket(addr.family(), SOCK_DGRAM); 200 SOCK_DGRAM);
201 MessageClient msg_client(th1.get(), socket); 201 MessageClient msg_client(&th1, socket);
202 202
203 // Create the socket client on its own thread. 203 // Create the socket client on its own thread.
204 auto th2 = Thread::CreateWithSocketServer(); 204 Thread th2;
205 AsyncSocket* asocket = 205 AsyncSocket* asocket =
206 th2->socketserver()->CreateAsyncSocket(addr.family(), SOCK_DGRAM); 206 th2.socketserver()->CreateAsyncSocket(addr.family(), SOCK_DGRAM);
207 SocketClient sock_client(asocket, addr, th1.get(), &msg_client); 207 SocketClient sock_client(asocket, addr, &th1, &msg_client);
208 208
209 socket->Connect(sock_client.address()); 209 socket->Connect(sock_client.address());
210 210
211 th1->Start(); 211 th1.Start();
212 th2->Start(); 212 th2.Start();
213 213
214 // Get the messages started. 214 // Get the messages started.
215 th1->PostDelayed(RTC_FROM_HERE, 100, &msg_client, 0, new TestMessage(1)); 215 th1.PostDelayed(RTC_FROM_HERE, 100, &msg_client, 0, new TestMessage(1));
216 216
217 // Give the clients a little while to run. 217 // Give the clients a little while to run.
218 // Messages will be processed at 100, 300, 500, 700, 900. 218 // Messages will be processed at 100, 300, 500, 700, 900.
219 Thread* th_main = Thread::Current(); 219 Thread* th_main = Thread::Current();
220 th_main->ProcessMessages(1000); 220 th_main->ProcessMessages(1000);
221 221
222 // Stop the sending client. Give the receiver a bit longer to run, in case 222 // Stop the sending client. Give the receiver a bit longer to run, in case
223 // it is running on a machine that is under load (e.g. the build machine). 223 // it is running on a machine that is under load (e.g. the build machine).
224 th1->Stop(); 224 th1.Stop();
225 th_main->ProcessMessages(200); 225 th_main->ProcessMessages(200);
226 th2->Stop(); 226 th2.Stop();
227 227
228 // Make sure the results were correct 228 // Make sure the results were correct
229 EXPECT_EQ(5, msg_client.count); 229 EXPECT_EQ(5, msg_client.count);
230 EXPECT_EQ(34, msg_client.last); 230 EXPECT_EQ(34, msg_client.last);
231 EXPECT_EQ(5, sock_client.count); 231 EXPECT_EQ(5, sock_client.count);
232 EXPECT_EQ(55, sock_client.last); 232 EXPECT_EQ(55, sock_client.last);
233 } 233 }
234 234
235 // Test that setting thread names doesn't cause a malfunction. 235 // Test that setting thread names doesn't cause a malfunction.
236 // There's no easy way to verify the name was set properly at this time. 236 // There's no easy way to verify the name was set properly at this time.
237 TEST(ThreadTest, Names) { 237 TEST(ThreadTest, Names) {
238 // Default name 238 // Default name
239 auto thread = Thread::CreateWithSocketServer(); 239 Thread *thread;
240 thread = new Thread();
240 EXPECT_TRUE(thread->Start()); 241 EXPECT_TRUE(thread->Start());
241 thread->Stop(); 242 thread->Stop();
243 delete thread;
244 thread = new Thread();
242 // Name with no object parameter 245 // Name with no object parameter
243 thread = Thread::CreateWithSocketServer();
244 EXPECT_TRUE(thread->SetName("No object", nullptr)); 246 EXPECT_TRUE(thread->SetName("No object", nullptr));
245 EXPECT_TRUE(thread->Start()); 247 EXPECT_TRUE(thread->Start());
246 thread->Stop(); 248 thread->Stop();
249 delete thread;
247 // Really long name 250 // Really long name
248 thread = Thread::CreateWithSocketServer(); 251 thread = new Thread();
249 EXPECT_TRUE(thread->SetName("Abcdefghijklmnopqrstuvwxyz1234567890", this)); 252 EXPECT_TRUE(thread->SetName("Abcdefghijklmnopqrstuvwxyz1234567890", this));
250 EXPECT_TRUE(thread->Start()); 253 EXPECT_TRUE(thread->Start());
251 thread->Stop(); 254 thread->Stop();
255 delete thread;
252 } 256 }
253 257
254 TEST(ThreadTest, Wrap) { 258 TEST(ThreadTest, Wrap) {
255 Thread* current_thread = Thread::Current(); 259 Thread* current_thread = Thread::Current();
256 current_thread->UnwrapCurrent(); 260 current_thread->UnwrapCurrent();
257 CustomThread* cthread = new CustomThread(); 261 CustomThread* cthread = new CustomThread();
258 EXPECT_TRUE(cthread->WrapCurrent()); 262 EXPECT_TRUE(cthread->WrapCurrent());
259 EXPECT_TRUE(cthread->RunningForTest()); 263 EXPECT_TRUE(cthread->RunningForTest());
260 EXPECT_FALSE(cthread->IsOwned()); 264 EXPECT_FALSE(cthread->IsOwned());
261 cthread->UnwrapCurrent(); 265 cthread->UnwrapCurrent();
262 EXPECT_FALSE(cthread->RunningForTest()); 266 EXPECT_FALSE(cthread->RunningForTest());
263 delete cthread; 267 delete cthread;
264 current_thread->WrapCurrent(); 268 current_thread->WrapCurrent();
265 } 269 }
266 270
267 TEST(ThreadTest, Invoke) { 271 TEST(ThreadTest, Invoke) {
268 // Create and start the thread. 272 // Create and start the thread.
269 auto thread = Thread::CreateWithSocketServer(); 273 Thread thread;
270 thread->Start(); 274 thread.Start();
271 // Try calling functors. 275 // Try calling functors.
272 EXPECT_EQ(42, thread->Invoke<int>(RTC_FROM_HERE, FunctorA())); 276 EXPECT_EQ(42, thread.Invoke<int>(RTC_FROM_HERE, FunctorA()));
273 AtomicBool called; 277 AtomicBool called;
274 FunctorB f2(&called); 278 FunctorB f2(&called);
275 thread->Invoke<void>(RTC_FROM_HERE, f2); 279 thread.Invoke<void>(RTC_FROM_HERE, f2);
276 EXPECT_TRUE(called.get()); 280 EXPECT_TRUE(called.get());
277 // Try calling bare functions. 281 // Try calling bare functions.
278 struct LocalFuncs { 282 struct LocalFuncs {
279 static int Func1() { return 999; } 283 static int Func1() { return 999; }
280 static void Func2() {} 284 static void Func2() {}
281 }; 285 };
282 EXPECT_EQ(999, thread->Invoke<int>(RTC_FROM_HERE, &LocalFuncs::Func1)); 286 EXPECT_EQ(999, thread.Invoke<int>(RTC_FROM_HERE, &LocalFuncs::Func1));
283 thread->Invoke<void>(RTC_FROM_HERE, &LocalFuncs::Func2); 287 thread.Invoke<void>(RTC_FROM_HERE, &LocalFuncs::Func2);
284 } 288 }
285 289
286 // Verifies that two threads calling Invoke on each other at the same time does 290 // Verifies that two threads calling Invoke on each other at the same time does
287 // not deadlock. 291 // not deadlock.
288 TEST(ThreadTest, TwoThreadsInvokeNoDeadlock) { 292 TEST(ThreadTest, TwoThreadsInvokeNoDeadlock) {
289 AutoThread thread; 293 AutoThread thread;
290 Thread* current_thread = Thread::Current(); 294 Thread* current_thread = Thread::Current();
291 ASSERT_TRUE(current_thread != nullptr); 295 ASSERT_TRUE(current_thread != nullptr);
292 296
293 auto other_thread = Thread::CreateWithSocketServer(); 297 Thread other_thread;
294 other_thread->Start(); 298 other_thread.Start();
295 299
296 struct LocalFuncs { 300 struct LocalFuncs {
297 static void Set(bool* out) { *out = true; } 301 static void Set(bool* out) { *out = true; }
298 static void InvokeSet(Thread* thread, bool* out) { 302 static void InvokeSet(Thread* thread, bool* out) {
299 thread->Invoke<void>(RTC_FROM_HERE, Bind(&Set, out)); 303 thread->Invoke<void>(RTC_FROM_HERE, Bind(&Set, out));
300 } 304 }
301 }; 305 };
302 306
303 bool called = false; 307 bool called = false;
304 other_thread->Invoke<void>( 308 other_thread.Invoke<void>(
305 RTC_FROM_HERE, Bind(&LocalFuncs::InvokeSet, current_thread, &called)); 309 RTC_FROM_HERE, Bind(&LocalFuncs::InvokeSet, current_thread, &called));
306 310
307 EXPECT_TRUE(called); 311 EXPECT_TRUE(called);
308 } 312 }
309 313
310 // Verifies that if thread A invokes a call on thread B and thread C is trying 314 // Verifies that if thread A invokes a call on thread B and thread C is trying
311 // to invoke A at the same time, thread A does not handle C's invoke while 315 // to invoke A at the same time, thread A does not handle C's invoke while
312 // invoking B. 316 // invoking B.
313 TEST(ThreadTest, ThreeThreadsInvoke) { 317 TEST(ThreadTest, ThreeThreadsInvoke) {
314 AutoThread thread; 318 AutoThread thread;
315 Thread* thread_a = Thread::Current(); 319 Thread* thread_a = Thread::Current();
316 auto thread_b = Thread::CreateWithSocketServer(); 320 Thread thread_b, thread_c;
317 auto thread_c = Thread::CreateWithSocketServer(); 321 thread_b.Start();
318 thread_b->Start(); 322 thread_c.Start();
319 thread_c->Start();
320 323
321 class LockedBool { 324 class LockedBool {
322 public: 325 public:
323 explicit LockedBool(bool value) : value_(value) {} 326 explicit LockedBool(bool value) : value_(value) {}
324 327
325 void Set(bool value) { 328 void Set(bool value) {
326 CritScope lock(&crit_); 329 CritScope lock(&crit_);
327 value_ = value; 330 value_ = value;
328 } 331 }
329 332
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
367 EXPECT_TRUE_WAIT(async_invoked.Get(), 2000); 370 EXPECT_TRUE_WAIT(async_invoked.Get(), 2000);
368 } 371 }
369 }; 372 };
370 373
371 AsyncInvoker invoker; 374 AsyncInvoker invoker;
372 LockedBool thread_a_called(false); 375 LockedBool thread_a_called(false);
373 376
374 // Start the sequence A --(invoke)--> B --(async invoke)--> C --(invoke)--> A. 377 // Start the sequence A --(invoke)--> B --(async invoke)--> C --(invoke)--> A.
375 // Thread B returns when C receives the call and C should be blocked until A 378 // Thread B returns when C receives the call and C should be blocked until A
376 // starts to process messages. 379 // starts to process messages.
377 thread_b->Invoke<void>(RTC_FROM_HERE, 380 thread_b.Invoke<void>(RTC_FROM_HERE,
378 Bind(&LocalFuncs::AsyncInvokeSetAndWait, &invoker, 381 Bind(&LocalFuncs::AsyncInvokeSetAndWait, &invoker,
379 thread_c.get(), thread_a, &thread_a_called)); 382 &thread_c, thread_a, &thread_a_called));
380 EXPECT_FALSE(thread_a_called.Get()); 383 EXPECT_FALSE(thread_a_called.Get());
381 384
382 EXPECT_TRUE_WAIT(thread_a_called.Get(), 2000); 385 EXPECT_TRUE_WAIT(thread_a_called.Get(), 2000);
383 } 386 }
384 387
385 // Set the name on a thread when the underlying QueueDestroyed signal is 388 // Set the name on a thread when the underlying QueueDestroyed signal is
386 // triggered. This causes an error if the object is already partially 389 // triggered. This causes an error if the object is already partially
387 // destroyed. 390 // destroyed.
388 class SetNameOnSignalQueueDestroyedTester : public sigslot::has_slots<> { 391 class SetNameOnSignalQueueDestroyedTester : public sigslot::has_slots<> {
389 public: 392 public:
390 SetNameOnSignalQueueDestroyedTester(Thread* thread) : thread_(thread) { 393 SetNameOnSignalQueueDestroyedTester(Thread* thread) : thread_(thread) {
391 thread->SignalQueueDestroyed.connect( 394 thread->SignalQueueDestroyed.connect(
392 this, &SetNameOnSignalQueueDestroyedTester::OnQueueDestroyed); 395 this, &SetNameOnSignalQueueDestroyedTester::OnQueueDestroyed);
393 } 396 }
394 397
395 void OnQueueDestroyed() { 398 void OnQueueDestroyed() {
396 // Makes sure that if we access the Thread while it's being destroyed, that 399 // Makes sure that if we access the Thread while it's being destroyed, that
397 // it doesn't cause a problem because the vtable has been modified. 400 // it doesn't cause a problem because the vtable has been modified.
398 thread_->SetName("foo", nullptr); 401 thread_->SetName("foo", nullptr);
399 } 402 }
400 403
401 private: 404 private:
402 Thread* thread_; 405 Thread* thread_;
403 }; 406 };
404 407
405 TEST(ThreadTest, SetNameOnSignalQueueDestroyed) { 408 TEST(ThreadTest, SetNameOnSignalQueueDestroyed) {
406 auto thread1 = Thread::CreateWithSocketServer(); 409 Thread* thread1 = new Thread();
407 SetNameOnSignalQueueDestroyedTester tester1(thread1.get()); 410 SetNameOnSignalQueueDestroyedTester tester1(thread1);
408 thread1.reset(); 411 delete thread1;
409 412
410 Thread* thread2 = new AutoThread(); 413 Thread* thread2 = new AutoThread();
411 SetNameOnSignalQueueDestroyedTester tester2(thread2); 414 SetNameOnSignalQueueDestroyedTester tester2(thread2);
412 delete thread2; 415 delete thread2;
413 } 416 }
414 417
415 class AsyncInvokeTest : public testing::Test { 418 class AsyncInvokeTest : public testing::Test {
416 public: 419 public:
417 void IntCallback(int value) { 420 void IntCallback(int value) {
418 EXPECT_EQ(expected_thread_, Thread::Current()); 421 EXPECT_EQ(expected_thread_, Thread::Current());
419 int_value_ = value; 422 int_value_ = value;
420 } 423 }
421 void SetExpectedThreadForIntCallback(Thread* thread) { 424 void SetExpectedThreadForIntCallback(Thread* thread) {
422 expected_thread_ = thread; 425 expected_thread_ = thread;
423 } 426 }
424 427
425 protected: 428 protected:
426 enum { kWaitTimeout = 1000 }; 429 enum { kWaitTimeout = 1000 };
427 AsyncInvokeTest() 430 AsyncInvokeTest()
428 : int_value_(0), 431 : int_value_(0),
429 expected_thread_(nullptr) {} 432 expected_thread_(nullptr) {}
430 433
431 int int_value_; 434 int int_value_;
432 Thread* expected_thread_; 435 Thread* expected_thread_;
433 }; 436 };
434 437
435 TEST_F(AsyncInvokeTest, FireAndForget) { 438 TEST_F(AsyncInvokeTest, FireAndForget) {
436 AsyncInvoker invoker; 439 AsyncInvoker invoker;
437 // Create and start the thread. 440 // Create and start the thread.
438 auto thread = Thread::CreateWithSocketServer(); 441 Thread thread;
439 thread->Start(); 442 thread.Start();
440 // Try calling functor. 443 // Try calling functor.
441 AtomicBool called; 444 AtomicBool called;
442 invoker.AsyncInvoke<void>(RTC_FROM_HERE, thread.get(), FunctorB(&called)); 445 invoker.AsyncInvoke<void>(RTC_FROM_HERE, &thread, FunctorB(&called));
443 EXPECT_TRUE_WAIT(called.get(), kWaitTimeout); 446 EXPECT_TRUE_WAIT(called.get(), kWaitTimeout);
444 thread->Stop();
445 } 447 }
446 448
447 TEST_F(AsyncInvokeTest, KillInvokerDuringExecute) { 449 TEST_F(AsyncInvokeTest, KillInvokerDuringExecute) {
448 // Use these events to get in a state where the functor is in the middle of 450 // Use these events to get in a state where the functor is in the middle of
449 // executing, and then to wait for it to finish, ensuring the "EXPECT_FALSE" 451 // executing, and then to wait for it to finish, ensuring the "EXPECT_FALSE"
450 // is run. 452 // is run.
451 Event functor_started(false, false); 453 Event functor_started(false, false);
452 Event functor_continue(false, false); 454 Event functor_continue(false, false);
453 Event functor_finished(false, false); 455 Event functor_finished(false, false);
454 456
455 auto thread = Thread::CreateWithSocketServer(); 457 Thread thread;
456 thread->Start(); 458 thread.Start();
457 volatile bool invoker_destroyed = false; 459 volatile bool invoker_destroyed = false;
458 { 460 {
459 AsyncInvoker invoker; 461 AsyncInvoker invoker;
460 invoker.AsyncInvoke<void>(RTC_FROM_HERE, thread.get(), 462 invoker.AsyncInvoke<void>(RTC_FROM_HERE, &thread,
461 [&functor_started, &functor_continue, 463 [&functor_started, &functor_continue,
462 &functor_finished, &invoker_destroyed] { 464 &functor_finished, &invoker_destroyed] {
463 functor_started.Set(); 465 functor_started.Set();
464 functor_continue.Wait(Event::kForever); 466 functor_continue.Wait(Event::kForever);
465 rtc::Thread::Current()->SleepMs(kWaitTimeout); 467 rtc::Thread::Current()->SleepMs(kWaitTimeout);
466 EXPECT_FALSE(invoker_destroyed); 468 EXPECT_FALSE(invoker_destroyed);
467 functor_finished.Set(); 469 functor_finished.Set();
468 }); 470 });
469 functor_started.Wait(Event::kForever); 471 functor_started.Wait(Event::kForever);
470 472
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after
541 struct CreateInvoker { 543 struct CreateInvoker {
542 CreateInvoker(std::unique_ptr<GuardedAsyncInvoker>* invoker) 544 CreateInvoker(std::unique_ptr<GuardedAsyncInvoker>* invoker)
543 : invoker_(invoker) {} 545 : invoker_(invoker) {}
544 void operator()() { invoker_->reset(new GuardedAsyncInvoker()); } 546 void operator()() { invoker_->reset(new GuardedAsyncInvoker()); }
545 std::unique_ptr<GuardedAsyncInvoker>* invoker_; 547 std::unique_ptr<GuardedAsyncInvoker>* invoker_;
546 }; 548 };
547 549
548 // Test that we can call AsyncInvoke<void>() after the thread died. 550 // Test that we can call AsyncInvoke<void>() after the thread died.
549 TEST_F(GuardedAsyncInvokeTest, KillThreadFireAndForget) { 551 TEST_F(GuardedAsyncInvokeTest, KillThreadFireAndForget) {
550 // Create and start the thread. 552 // Create and start the thread.
551 std::unique_ptr<Thread> thread(Thread::Create()); 553 std::unique_ptr<Thread> thread(new Thread());
552 thread->Start(); 554 thread->Start();
553 std::unique_ptr<GuardedAsyncInvoker> invoker; 555 std::unique_ptr<GuardedAsyncInvoker> invoker;
554 // Create the invoker on |thread|. 556 // Create the invoker on |thread|.
555 thread->Invoke<void>(RTC_FROM_HERE, CreateInvoker(&invoker)); 557 thread->Invoke<void>(RTC_FROM_HERE, CreateInvoker(&invoker));
556 // Kill |thread|. 558 // Kill |thread|.
557 thread = nullptr; 559 thread = nullptr;
558 // Try calling functor. 560 // Try calling functor.
559 AtomicBool called; 561 AtomicBool called;
560 EXPECT_FALSE(invoker->AsyncInvoke<void>(RTC_FROM_HERE, FunctorB(&called))); 562 EXPECT_FALSE(invoker->AsyncInvoke<void>(RTC_FROM_HERE, FunctorB(&called)));
561 // With thread gone, nothing should happen. 563 // With thread gone, nothing should happen.
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
602 // Execute pending calls with id == 5. 604 // Execute pending calls with id == 5.
603 EXPECT_TRUE(invoker.Flush(5)); 605 EXPECT_TRUE(invoker.Flush(5));
604 EXPECT_TRUE(flag1.get()); 606 EXPECT_TRUE(flag1.get());
605 EXPECT_FALSE(flag2.get()); 607 EXPECT_FALSE(flag2.get());
606 flag1 = false; 608 flag1 = false;
607 // Execute all pending calls. The id == 5 call should not execute again. 609 // Execute all pending calls. The id == 5 call should not execute again.
608 EXPECT_TRUE(invoker.Flush()); 610 EXPECT_TRUE(invoker.Flush());
609 EXPECT_FALSE(flag1.get()); 611 EXPECT_FALSE(flag1.get());
610 EXPECT_TRUE(flag2.get()); 612 EXPECT_TRUE(flag2.get());
611 } 613 }
OLDNEW
« no previous file with comments | « webrtc/rtc_base/thread.h ('k') | webrtc/rtc_base/timeutils_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698