hta@webrtc.org | e1919f4 | 2012-05-22 15:57:34 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2012 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 | */ |
hta@webrtc.org | 4030013 | 2012-05-23 15:49:48 +0000 | [diff] [blame] | 10 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "rtc_base/platform_thread.h" |
hta@webrtc.org | e1919f4 | 2012-05-22 15:57:34 +0000 | [diff] [blame] | 12 | |
Markus Handell | 97c4458 | 2021-04-20 17:41:54 +0200 | [diff] [blame] | 13 | #include "rtc_base/event.h" |
| 14 | #include "system_wrappers/include/sleep.h" |
| 15 | #include "test/gmock.h" |
hta@webrtc.org | e1919f4 | 2012-05-22 15:57:34 +0000 | [diff] [blame] | 16 | |
tommi | 845afa8 | 2016-04-22 09:08:44 -0700 | [diff] [blame] | 17 | namespace rtc { |
Guido Urdaneta | 793bac5 | 2021-05-06 13:12:47 +0000 | [diff] [blame^] | 18 | namespace { |
hta@webrtc.org | e1919f4 | 2012-05-22 15:57:34 +0000 | [diff] [blame] | 19 | |
Guido Urdaneta | 793bac5 | 2021-05-06 13:12:47 +0000 | [diff] [blame^] | 20 | void NullRunFunction(void* obj) {} |
| 21 | |
| 22 | // Function that sets a boolean. |
| 23 | void SetFlagRunFunction(void* obj) { |
| 24 | bool* obj_as_bool = static_cast<bool*>(obj); |
| 25 | *obj_as_bool = true; |
tommi | 0f8b403 | 2017-02-22 11:22:05 -0800 | [diff] [blame] | 26 | } |
| 27 | |
Guido Urdaneta | 793bac5 | 2021-05-06 13:12:47 +0000 | [diff] [blame^] | 28 | void StdFunctionRunFunction(void* obj) { |
| 29 | std::function<void()>* fun = static_cast<std::function<void()>*>(obj); |
| 30 | (*fun)(); |
Markus Handell | 97c4458 | 2021-04-20 17:41:54 +0200 | [diff] [blame] | 31 | } |
| 32 | |
Guido Urdaneta | 793bac5 | 2021-05-06 13:12:47 +0000 | [diff] [blame^] | 33 | } // namespace |
| 34 | |
| 35 | TEST(PlatformThreadTest, StartStop) { |
| 36 | PlatformThread thread(&NullRunFunction, nullptr, "PlatformThreadTest"); |
| 37 | EXPECT_TRUE(thread.name() == "PlatformThreadTest"); |
| 38 | EXPECT_TRUE(thread.GetThreadRef() == 0); |
| 39 | thread.Start(); |
| 40 | EXPECT_TRUE(thread.GetThreadRef() != 0); |
| 41 | thread.Stop(); |
| 42 | EXPECT_TRUE(thread.GetThreadRef() == 0); |
tommi | 845afa8 | 2016-04-22 09:08:44 -0700 | [diff] [blame] | 43 | } |
| 44 | |
Guido Urdaneta | 793bac5 | 2021-05-06 13:12:47 +0000 | [diff] [blame^] | 45 | TEST(PlatformThreadTest, StartStop2) { |
| 46 | PlatformThread thread1(&NullRunFunction, nullptr, "PlatformThreadTest1"); |
| 47 | PlatformThread thread2(&NullRunFunction, nullptr, "PlatformThreadTest2"); |
| 48 | EXPECT_TRUE(thread1.GetThreadRef() == thread2.GetThreadRef()); |
| 49 | thread1.Start(); |
| 50 | thread2.Start(); |
| 51 | EXPECT_TRUE(thread1.GetThreadRef() != thread2.GetThreadRef()); |
| 52 | thread2.Stop(); |
| 53 | thread1.Stop(); |
tommi | 845afa8 | 2016-04-22 09:08:44 -0700 | [diff] [blame] | 54 | } |
hta@webrtc.org | e1919f4 | 2012-05-22 15:57:34 +0000 | [diff] [blame] | 55 | |
pbos | 12411ef | 2015-11-23 14:47:56 -0800 | [diff] [blame] | 56 | TEST(PlatformThreadTest, RunFunctionIsCalled) { |
hta@webrtc.org | e1919f4 | 2012-05-22 15:57:34 +0000 | [diff] [blame] | 57 | bool flag = false; |
Guido Urdaneta | 793bac5 | 2021-05-06 13:12:47 +0000 | [diff] [blame^] | 58 | PlatformThread thread(&SetFlagRunFunction, &flag, "RunFunctionIsCalled"); |
| 59 | thread.Start(); |
| 60 | |
| 61 | // At this point, the flag may be either true or false. |
| 62 | thread.Stop(); |
| 63 | |
| 64 | // We expect the thread to have run at least once. |
hta@webrtc.org | e1919f4 | 2012-05-22 15:57:34 +0000 | [diff] [blame] | 65 | EXPECT_TRUE(flag); |
hta@webrtc.org | e1919f4 | 2012-05-22 15:57:34 +0000 | [diff] [blame] | 66 | } |
tommi | 0f8b403 | 2017-02-22 11:22:05 -0800 | [diff] [blame] | 67 | |
Markus Handell | 97c4458 | 2021-04-20 17:41:54 +0200 | [diff] [blame] | 68 | TEST(PlatformThreadTest, JoinsThread) { |
| 69 | // This test flakes if there are problems with the join implementation. |
Guido Urdaneta | 793bac5 | 2021-05-06 13:12:47 +0000 | [diff] [blame^] | 70 | EXPECT_TRUE(ThreadAttributes().joinable); |
Markus Handell | 97c4458 | 2021-04-20 17:41:54 +0200 | [diff] [blame] | 71 | rtc::Event event; |
Guido Urdaneta | 793bac5 | 2021-05-06 13:12:47 +0000 | [diff] [blame^] | 72 | std::function<void()> thread_function = [&] { event.Set(); }; |
| 73 | PlatformThread thread(&StdFunctionRunFunction, &thread_function, "T"); |
| 74 | thread.Start(); |
| 75 | thread.Stop(); |
Markus Handell | 97c4458 | 2021-04-20 17:41:54 +0200 | [diff] [blame] | 76 | EXPECT_TRUE(event.Wait(/*give_up_after_ms=*/0)); |
| 77 | } |
| 78 | |
| 79 | TEST(PlatformThreadTest, StopsBeforeDetachedThreadExits) { |
| 80 | // This test flakes if there are problems with the detached thread |
| 81 | // implementation. |
| 82 | bool flag = false; |
| 83 | rtc::Event thread_started; |
| 84 | rtc::Event thread_continue; |
| 85 | rtc::Event thread_exiting; |
Guido Urdaneta | 793bac5 | 2021-05-06 13:12:47 +0000 | [diff] [blame^] | 86 | std::function<void()> thread_function = [&] { |
| 87 | thread_started.Set(); |
| 88 | thread_continue.Wait(Event::kForever); |
| 89 | flag = true; |
| 90 | thread_exiting.Set(); |
| 91 | }; |
| 92 | { |
| 93 | PlatformThread thread(&StdFunctionRunFunction, &thread_function, "T", |
| 94 | ThreadAttributes().SetDetached()); |
| 95 | thread.Start(); |
| 96 | thread.Stop(); |
| 97 | } |
Markus Handell | 97c4458 | 2021-04-20 17:41:54 +0200 | [diff] [blame] | 98 | thread_started.Wait(Event::kForever); |
| 99 | EXPECT_FALSE(flag); |
| 100 | thread_continue.Set(); |
| 101 | thread_exiting.Wait(Event::kForever); |
| 102 | EXPECT_TRUE(flag); |
| 103 | } |
| 104 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 105 | } // namespace rtc |