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

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

Issue 2981623002: Make the default ctor of rtc::Thread, protected (Closed)
Patch Set: Format 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 Thread th1; 198 auto th1 = Thread::CreateWithSocketServer();
199 Socket* socket = th1.socketserver()->CreateAsyncSocket(addr.family(), 199 Socket* socket =
200 SOCK_DGRAM); 200 th1->socketserver()->CreateAsyncSocket(addr.family(), SOCK_DGRAM);
201 MessageClient msg_client(&th1, socket); 201 MessageClient msg_client(th1.get(), socket);
202 202
203 // Create the socket client on its own thread. 203 // Create the socket client on its own thread.
204 Thread th2; 204 auto th2 = Thread::CreateWithSocketServer();
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, &msg_client); 207 SocketClient sock_client(asocket, addr, th1.get(), &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 Thread *thread; 239 auto thread = Thread::CreateWithSocketServer();
240 thread = new Thread();
241 EXPECT_TRUE(thread->Start()); 240 EXPECT_TRUE(thread->Start());
242 thread->Stop(); 241 thread->Stop();
243 delete thread;
244 thread = new Thread();
245 // Name with no object parameter 242 // Name with no object parameter
243 thread = Thread::CreateWithSocketServer();
246 EXPECT_TRUE(thread->SetName("No object", nullptr)); 244 EXPECT_TRUE(thread->SetName("No object", nullptr));
247 EXPECT_TRUE(thread->Start()); 245 EXPECT_TRUE(thread->Start());
248 thread->Stop(); 246 thread->Stop();
249 delete thread;
250 // Really long name 247 // Really long name
251 thread = new Thread(); 248 thread = Thread::CreateWithSocketServer();
252 EXPECT_TRUE(thread->SetName("Abcdefghijklmnopqrstuvwxyz1234567890", this)); 249 EXPECT_TRUE(thread->SetName("Abcdefghijklmnopqrstuvwxyz1234567890", this));
253 EXPECT_TRUE(thread->Start()); 250 EXPECT_TRUE(thread->Start());
254 thread->Stop(); 251 thread->Stop();
255 delete thread;
256 } 252 }
257 253
258 TEST(ThreadTest, Wrap) { 254 TEST(ThreadTest, Wrap) {
259 Thread* current_thread = Thread::Current(); 255 Thread* current_thread = Thread::Current();
260 current_thread->UnwrapCurrent(); 256 current_thread->UnwrapCurrent();
261 CustomThread* cthread = new CustomThread(); 257 CustomThread* cthread = new CustomThread();
262 EXPECT_TRUE(cthread->WrapCurrent()); 258 EXPECT_TRUE(cthread->WrapCurrent());
263 EXPECT_TRUE(cthread->RunningForTest()); 259 EXPECT_TRUE(cthread->RunningForTest());
264 EXPECT_FALSE(cthread->IsOwned()); 260 EXPECT_FALSE(cthread->IsOwned());
265 cthread->UnwrapCurrent(); 261 cthread->UnwrapCurrent();
266 EXPECT_FALSE(cthread->RunningForTest()); 262 EXPECT_FALSE(cthread->RunningForTest());
267 delete cthread; 263 delete cthread;
268 current_thread->WrapCurrent(); 264 current_thread->WrapCurrent();
269 } 265 }
270 266
271 TEST(ThreadTest, Invoke) { 267 TEST(ThreadTest, Invoke) {
272 // Create and start the thread. 268 // Create and start the thread.
273 Thread thread; 269 auto thread = Thread::CreateWithSocketServer();
274 thread.Start(); 270 thread->Start();
275 // Try calling functors. 271 // Try calling functors.
276 EXPECT_EQ(42, thread.Invoke<int>(RTC_FROM_HERE, FunctorA())); 272 EXPECT_EQ(42, thread->Invoke<int>(RTC_FROM_HERE, FunctorA()));
277 AtomicBool called; 273 AtomicBool called;
278 FunctorB f2(&called); 274 FunctorB f2(&called);
279 thread.Invoke<void>(RTC_FROM_HERE, f2); 275 thread->Invoke<void>(RTC_FROM_HERE, f2);
280 EXPECT_TRUE(called.get()); 276 EXPECT_TRUE(called.get());
281 // Try calling bare functions. 277 // Try calling bare functions.
282 struct LocalFuncs { 278 struct LocalFuncs {
283 static int Func1() { return 999; } 279 static int Func1() { return 999; }
284 static void Func2() {} 280 static void Func2() {}
285 }; 281 };
286 EXPECT_EQ(999, thread.Invoke<int>(RTC_FROM_HERE, &LocalFuncs::Func1)); 282 EXPECT_EQ(999, thread->Invoke<int>(RTC_FROM_HERE, &LocalFuncs::Func1));
287 thread.Invoke<void>(RTC_FROM_HERE, &LocalFuncs::Func2); 283 thread->Invoke<void>(RTC_FROM_HERE, &LocalFuncs::Func2);
288 } 284 }
289 285
290 // Verifies that two threads calling Invoke on each other at the same time does 286 // Verifies that two threads calling Invoke on each other at the same time does
291 // not deadlock. 287 // not deadlock.
292 TEST(ThreadTest, TwoThreadsInvokeNoDeadlock) { 288 TEST(ThreadTest, TwoThreadsInvokeNoDeadlock) {
293 AutoThread thread; 289 AutoThread thread;
294 Thread* current_thread = Thread::Current(); 290 Thread* current_thread = Thread::Current();
295 ASSERT_TRUE(current_thread != nullptr); 291 ASSERT_TRUE(current_thread != nullptr);
296 292
297 Thread other_thread; 293 auto other_thread = Thread::CreateWithSocketServer();
298 other_thread.Start(); 294 other_thread->Start();
299 295
300 struct LocalFuncs { 296 struct LocalFuncs {
301 static void Set(bool* out) { *out = true; } 297 static void Set(bool* out) { *out = true; }
302 static void InvokeSet(Thread* thread, bool* out) { 298 static void InvokeSet(Thread* thread, bool* out) {
303 thread->Invoke<void>(RTC_FROM_HERE, Bind(&Set, out)); 299 thread->Invoke<void>(RTC_FROM_HERE, Bind(&Set, out));
304 } 300 }
305 }; 301 };
306 302
307 bool called = false; 303 bool called = false;
308 other_thread.Invoke<void>( 304 other_thread->Invoke<void>(
309 RTC_FROM_HERE, Bind(&LocalFuncs::InvokeSet, current_thread, &called)); 305 RTC_FROM_HERE, Bind(&LocalFuncs::InvokeSet, current_thread, &called));
310 306
311 EXPECT_TRUE(called); 307 EXPECT_TRUE(called);
312 } 308 }
313 309
314 // Verifies that if thread A invokes a call on thread B and thread C is trying 310 // Verifies that if thread A invokes a call on thread B and thread C is trying
315 // to invoke A at the same time, thread A does not handle C's invoke while 311 // to invoke A at the same time, thread A does not handle C's invoke while
316 // invoking B. 312 // invoking B.
317 TEST(ThreadTest, ThreeThreadsInvoke) { 313 TEST(ThreadTest, ThreeThreadsInvoke) {
318 AutoThread thread; 314 AutoThread thread;
319 Thread* thread_a = Thread::Current(); 315 Thread* thread_a = Thread::Current();
320 Thread thread_b, thread_c; 316 auto thread_b = Thread::CreateWithSocketServer();
321 thread_b.Start(); 317 auto thread_c = Thread::CreateWithSocketServer();
322 thread_c.Start(); 318 thread_b->Start();
319 thread_c->Start();
323 320
324 class LockedBool { 321 class LockedBool {
325 public: 322 public:
326 explicit LockedBool(bool value) : value_(value) {} 323 explicit LockedBool(bool value) : value_(value) {}
327 324
328 void Set(bool value) { 325 void Set(bool value) {
329 CritScope lock(&crit_); 326 CritScope lock(&crit_);
330 value_ = value; 327 value_ = value;
331 } 328 }
332 329
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
370 EXPECT_TRUE_WAIT(async_invoked.Get(), 2000); 367 EXPECT_TRUE_WAIT(async_invoked.Get(), 2000);
371 } 368 }
372 }; 369 };
373 370
374 AsyncInvoker invoker; 371 AsyncInvoker invoker;
375 LockedBool thread_a_called(false); 372 LockedBool thread_a_called(false);
376 373
377 // Start the sequence A --(invoke)--> B --(async invoke)--> C --(invoke)--> A. 374 // Start the sequence A --(invoke)--> B --(async invoke)--> C --(invoke)--> A.
378 // Thread B returns when C receives the call and C should be blocked until A 375 // Thread B returns when C receives the call and C should be blocked until A
379 // starts to process messages. 376 // starts to process messages.
380 thread_b.Invoke<void>(RTC_FROM_HERE, 377 thread_b->Invoke<void>(RTC_FROM_HERE,
381 Bind(&LocalFuncs::AsyncInvokeSetAndWait, &invoker, 378 Bind(&LocalFuncs::AsyncInvokeSetAndWait, &invoker,
382 &thread_c, thread_a, &thread_a_called)); 379 thread_c.get(), thread_a, &thread_a_called));
383 EXPECT_FALSE(thread_a_called.Get()); 380 EXPECT_FALSE(thread_a_called.Get());
384 381
385 EXPECT_TRUE_WAIT(thread_a_called.Get(), 2000); 382 EXPECT_TRUE_WAIT(thread_a_called.Get(), 2000);
386 } 383 }
387 384
388 // Set the name on a thread when the underlying QueueDestroyed signal is 385 // Set the name on a thread when the underlying QueueDestroyed signal is
389 // triggered. This causes an error if the object is already partially 386 // triggered. This causes an error if the object is already partially
390 // destroyed. 387 // destroyed.
391 class SetNameOnSignalQueueDestroyedTester : public sigslot::has_slots<> { 388 class SetNameOnSignalQueueDestroyedTester : public sigslot::has_slots<> {
392 public: 389 public:
393 SetNameOnSignalQueueDestroyedTester(Thread* thread) : thread_(thread) { 390 SetNameOnSignalQueueDestroyedTester(Thread* thread) : thread_(thread) {
394 thread->SignalQueueDestroyed.connect( 391 thread->SignalQueueDestroyed.connect(
395 this, &SetNameOnSignalQueueDestroyedTester::OnQueueDestroyed); 392 this, &SetNameOnSignalQueueDestroyedTester::OnQueueDestroyed);
396 } 393 }
397 394
398 void OnQueueDestroyed() { 395 void OnQueueDestroyed() {
399 // Makes sure that if we access the Thread while it's being destroyed, that 396 // Makes sure that if we access the Thread while it's being destroyed, that
400 // it doesn't cause a problem because the vtable has been modified. 397 // it doesn't cause a problem because the vtable has been modified.
401 thread_->SetName("foo", nullptr); 398 thread_->SetName("foo", nullptr);
402 } 399 }
403 400
404 private: 401 private:
405 Thread* thread_; 402 Thread* thread_;
406 }; 403 };
407 404
408 TEST(ThreadTest, SetNameOnSignalQueueDestroyed) { 405 TEST(ThreadTest, SetNameOnSignalQueueDestroyed) {
409 Thread* thread1 = new Thread(); 406 auto thread1 = Thread::CreateWithSocketServer();
410 SetNameOnSignalQueueDestroyedTester tester1(thread1); 407 SetNameOnSignalQueueDestroyedTester tester1(thread1.get());
411 delete thread1; 408 thread1.reset();
412 409
413 Thread* thread2 = new AutoThread(); 410 Thread* thread2 = new AutoThread();
414 SetNameOnSignalQueueDestroyedTester tester2(thread2); 411 SetNameOnSignalQueueDestroyedTester tester2(thread2);
415 delete thread2; 412 delete thread2;
416 } 413 }
417 414
418 class AsyncInvokeTest : public testing::Test { 415 class AsyncInvokeTest : public testing::Test {
419 public: 416 public:
420 void IntCallback(int value) { 417 void IntCallback(int value) {
421 EXPECT_EQ(expected_thread_, Thread::Current()); 418 EXPECT_EQ(expected_thread_, Thread::Current());
422 int_value_ = value; 419 int_value_ = value;
423 } 420 }
424 void SetExpectedThreadForIntCallback(Thread* thread) { 421 void SetExpectedThreadForIntCallback(Thread* thread) {
425 expected_thread_ = thread; 422 expected_thread_ = thread;
426 } 423 }
427 424
428 protected: 425 protected:
429 enum { kWaitTimeout = 1000 }; 426 enum { kWaitTimeout = 1000 };
430 AsyncInvokeTest() 427 AsyncInvokeTest()
431 : int_value_(0), 428 : int_value_(0),
432 expected_thread_(nullptr) {} 429 expected_thread_(nullptr) {}
433 430
434 int int_value_; 431 int int_value_;
435 Thread* expected_thread_; 432 Thread* expected_thread_;
436 }; 433 };
437 434
438 TEST_F(AsyncInvokeTest, FireAndForget) { 435 TEST_F(AsyncInvokeTest, FireAndForget) {
439 AsyncInvoker invoker; 436 AsyncInvoker invoker;
440 // Create and start the thread. 437 // Create and start the thread.
441 Thread thread; 438 auto thread = Thread::CreateWithSocketServer();
442 thread.Start(); 439 thread->Start();
443 // Try calling functor. 440 // Try calling functor.
444 AtomicBool called; 441 AtomicBool called;
445 invoker.AsyncInvoke<void>(RTC_FROM_HERE, &thread, FunctorB(&called)); 442 invoker.AsyncInvoke<void>(RTC_FROM_HERE, thread.get(), FunctorB(&called));
446 EXPECT_TRUE_WAIT(called.get(), kWaitTimeout); 443 EXPECT_TRUE_WAIT(called.get(), kWaitTimeout);
444 thread->Stop();
447 } 445 }
448 446
449 TEST_F(AsyncInvokeTest, KillInvokerDuringExecute) { 447 TEST_F(AsyncInvokeTest, KillInvokerDuringExecute) {
450 // Use these events to get in a state where the functor is in the middle of 448 // Use these events to get in a state where the functor is in the middle of
451 // executing, and then to wait for it to finish, ensuring the "EXPECT_FALSE" 449 // executing, and then to wait for it to finish, ensuring the "EXPECT_FALSE"
452 // is run. 450 // is run.
453 Event functor_started(false, false); 451 Event functor_started(false, false);
454 Event functor_continue(false, false); 452 Event functor_continue(false, false);
455 Event functor_finished(false, false); 453 Event functor_finished(false, false);
456 454
457 Thread thread; 455 auto thread = Thread::CreateWithSocketServer();
458 thread.Start(); 456 thread->Start();
459 volatile bool invoker_destroyed = false; 457 volatile bool invoker_destroyed = false;
460 { 458 {
461 AsyncInvoker invoker; 459 AsyncInvoker invoker;
462 invoker.AsyncInvoke<void>(RTC_FROM_HERE, &thread, 460 invoker.AsyncInvoke<void>(RTC_FROM_HERE, thread.get(),
463 [&functor_started, &functor_continue, 461 [&functor_started, &functor_continue,
464 &functor_finished, &invoker_destroyed] { 462 &functor_finished, &invoker_destroyed] {
465 functor_started.Set(); 463 functor_started.Set();
466 functor_continue.Wait(Event::kForever); 464 functor_continue.Wait(Event::kForever);
467 rtc::Thread::Current()->SleepMs(kWaitTimeout); 465 rtc::Thread::Current()->SleepMs(kWaitTimeout);
468 EXPECT_FALSE(invoker_destroyed); 466 EXPECT_FALSE(invoker_destroyed);
469 functor_finished.Set(); 467 functor_finished.Set();
470 }); 468 });
471 functor_started.Wait(Event::kForever); 469 functor_started.Wait(Event::kForever);
472 470
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after
543 struct CreateInvoker { 541 struct CreateInvoker {
544 CreateInvoker(std::unique_ptr<GuardedAsyncInvoker>* invoker) 542 CreateInvoker(std::unique_ptr<GuardedAsyncInvoker>* invoker)
545 : invoker_(invoker) {} 543 : invoker_(invoker) {}
546 void operator()() { invoker_->reset(new GuardedAsyncInvoker()); } 544 void operator()() { invoker_->reset(new GuardedAsyncInvoker()); }
547 std::unique_ptr<GuardedAsyncInvoker>* invoker_; 545 std::unique_ptr<GuardedAsyncInvoker>* invoker_;
548 }; 546 };
549 547
550 // Test that we can call AsyncInvoke<void>() after the thread died. 548 // Test that we can call AsyncInvoke<void>() after the thread died.
551 TEST_F(GuardedAsyncInvokeTest, KillThreadFireAndForget) { 549 TEST_F(GuardedAsyncInvokeTest, KillThreadFireAndForget) {
552 // Create and start the thread. 550 // Create and start the thread.
553 std::unique_ptr<Thread> thread(new Thread()); 551 std::unique_ptr<Thread> thread(Thread::Create());
554 thread->Start(); 552 thread->Start();
555 std::unique_ptr<GuardedAsyncInvoker> invoker; 553 std::unique_ptr<GuardedAsyncInvoker> invoker;
556 // Create the invoker on |thread|. 554 // Create the invoker on |thread|.
557 thread->Invoke<void>(RTC_FROM_HERE, CreateInvoker(&invoker)); 555 thread->Invoke<void>(RTC_FROM_HERE, CreateInvoker(&invoker));
558 // Kill |thread|. 556 // Kill |thread|.
559 thread = nullptr; 557 thread = nullptr;
560 // Try calling functor. 558 // Try calling functor.
561 AtomicBool called; 559 AtomicBool called;
562 EXPECT_FALSE(invoker->AsyncInvoke<void>(RTC_FROM_HERE, FunctorB(&called))); 560 EXPECT_FALSE(invoker->AsyncInvoke<void>(RTC_FROM_HERE, FunctorB(&called)));
563 // With thread gone, nothing should happen. 561 // With thread gone, nothing should happen.
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
604 // Execute pending calls with id == 5. 602 // Execute pending calls with id == 5.
605 EXPECT_TRUE(invoker.Flush(5)); 603 EXPECT_TRUE(invoker.Flush(5));
606 EXPECT_TRUE(flag1.get()); 604 EXPECT_TRUE(flag1.get());
607 EXPECT_FALSE(flag2.get()); 605 EXPECT_FALSE(flag2.get());
608 flag1 = false; 606 flag1 = false;
609 // Execute all pending calls. The id == 5 call should not execute again. 607 // Execute all pending calls. The id == 5 call should not execute again.
610 EXPECT_TRUE(invoker.Flush()); 608 EXPECT_TRUE(invoker.Flush());
611 EXPECT_FALSE(flag1.get()); 609 EXPECT_FALSE(flag1.get());
612 EXPECT_TRUE(flag2.get()); 610 EXPECT_TRUE(flag2.get());
613 } 611 }
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