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 | ad5037b | 2021-05-07 15:02:36 +0200 | [diff] [blame] | 13 | #include "absl/types/optional.h" |
Markus Handell | 97c4458 | 2021-04-20 17:41:54 +0200 | [diff] [blame] | 14 | #include "rtc_base/event.h" |
| 15 | #include "system_wrappers/include/sleep.h" |
| 16 | #include "test/gmock.h" |
hta@webrtc.org | e1919f4 | 2012-05-22 15:57:34 +0000 | [diff] [blame] | 17 | |
tommi | 845afa8 | 2016-04-22 09:08:44 -0700 | [diff] [blame] | 18 | namespace rtc { |
hta@webrtc.org | e1919f4 | 2012-05-22 15:57:34 +0000 | [diff] [blame] | 19 | |
Markus Handell | ad5037b | 2021-05-07 15:02:36 +0200 | [diff] [blame] | 20 | TEST(PlatformThreadTest, DefaultConstructedIsEmpty) { |
| 21 | PlatformThread thread; |
| 22 | EXPECT_EQ(thread.GetHandle(), absl::nullopt); |
| 23 | EXPECT_TRUE(thread.empty()); |
tommi | 0f8b403 | 2017-02-22 11:22:05 -0800 | [diff] [blame] | 24 | } |
| 25 | |
Markus Handell | ad5037b | 2021-05-07 15:02:36 +0200 | [diff] [blame] | 26 | TEST(PlatformThreadTest, StartFinalize) { |
| 27 | PlatformThread thread = PlatformThread::SpawnJoinable([] {}, "1"); |
| 28 | EXPECT_NE(thread.GetHandle(), absl::nullopt); |
| 29 | EXPECT_FALSE(thread.empty()); |
| 30 | thread.Finalize(); |
| 31 | EXPECT_TRUE(thread.empty()); |
Markus Handell | 74543b7 | 2021-06-28 10:29:15 +0200 | [diff] [blame] | 32 | rtc::Event done; |
| 33 | thread = PlatformThread::SpawnDetached([&] { done.Set(); }, "2"); |
Markus Handell | ad5037b | 2021-05-07 15:02:36 +0200 | [diff] [blame] | 34 | EXPECT_FALSE(thread.empty()); |
| 35 | thread.Finalize(); |
| 36 | EXPECT_TRUE(thread.empty()); |
Markus Handell | 2cfc1af | 2022-08-19 08:16:48 +0000 | [diff] [blame] | 37 | done.Wait(webrtc::TimeDelta::Seconds(30)); |
Markus Handell | 97c4458 | 2021-04-20 17:41:54 +0200 | [diff] [blame] | 38 | } |
| 39 | |
Markus Handell | ad5037b | 2021-05-07 15:02:36 +0200 | [diff] [blame] | 40 | TEST(PlatformThreadTest, MovesEmpty) { |
| 41 | PlatformThread thread1; |
| 42 | PlatformThread thread2 = std::move(thread1); |
| 43 | EXPECT_TRUE(thread1.empty()); |
| 44 | EXPECT_TRUE(thread2.empty()); |
tommi | 845afa8 | 2016-04-22 09:08:44 -0700 | [diff] [blame] | 45 | } |
| 46 | |
Markus Handell | ad5037b | 2021-05-07 15:02:36 +0200 | [diff] [blame] | 47 | TEST(PlatformThreadTest, MovesHandles) { |
| 48 | PlatformThread thread1 = PlatformThread::SpawnJoinable([] {}, "1"); |
| 49 | PlatformThread thread2 = std::move(thread1); |
| 50 | EXPECT_TRUE(thread1.empty()); |
| 51 | EXPECT_FALSE(thread2.empty()); |
Markus Handell | 74543b7 | 2021-06-28 10:29:15 +0200 | [diff] [blame] | 52 | rtc::Event done; |
| 53 | thread1 = PlatformThread::SpawnDetached([&] { done.Set(); }, "2"); |
Markus Handell | ad5037b | 2021-05-07 15:02:36 +0200 | [diff] [blame] | 54 | thread2 = std::move(thread1); |
| 55 | EXPECT_TRUE(thread1.empty()); |
| 56 | EXPECT_FALSE(thread2.empty()); |
Markus Handell | 2cfc1af | 2022-08-19 08:16:48 +0000 | [diff] [blame] | 57 | done.Wait(webrtc::TimeDelta::Seconds(30)); |
Markus Handell | ad5037b | 2021-05-07 15:02:36 +0200 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | TEST(PlatformThreadTest, |
| 61 | TwoThreadHandlesAreDifferentWhenStartedAndEqualWhenJoined) { |
| 62 | PlatformThread thread1 = PlatformThread(); |
| 63 | PlatformThread thread2 = PlatformThread(); |
| 64 | EXPECT_EQ(thread1.GetHandle(), thread2.GetHandle()); |
| 65 | thread1 = PlatformThread::SpawnJoinable([] {}, "1"); |
| 66 | thread2 = PlatformThread::SpawnJoinable([] {}, "2"); |
| 67 | EXPECT_NE(thread1.GetHandle(), thread2.GetHandle()); |
| 68 | thread1.Finalize(); |
| 69 | EXPECT_NE(thread1.GetHandle(), thread2.GetHandle()); |
| 70 | thread2.Finalize(); |
| 71 | EXPECT_EQ(thread1.GetHandle(), thread2.GetHandle()); |
tommi | 845afa8 | 2016-04-22 09:08:44 -0700 | [diff] [blame] | 72 | } |
hta@webrtc.org | e1919f4 | 2012-05-22 15:57:34 +0000 | [diff] [blame] | 73 | |
pbos | 12411ef | 2015-11-23 14:47:56 -0800 | [diff] [blame] | 74 | TEST(PlatformThreadTest, RunFunctionIsCalled) { |
hta@webrtc.org | e1919f4 | 2012-05-22 15:57:34 +0000 | [diff] [blame] | 75 | bool flag = false; |
Markus Handell | ad5037b | 2021-05-07 15:02:36 +0200 | [diff] [blame] | 76 | PlatformThread::SpawnJoinable([&] { flag = true; }, "T"); |
hta@webrtc.org | e1919f4 | 2012-05-22 15:57:34 +0000 | [diff] [blame] | 77 | EXPECT_TRUE(flag); |
hta@webrtc.org | e1919f4 | 2012-05-22 15:57:34 +0000 | [diff] [blame] | 78 | } |
tommi | 0f8b403 | 2017-02-22 11:22:05 -0800 | [diff] [blame] | 79 | |
Markus Handell | 97c4458 | 2021-04-20 17:41:54 +0200 | [diff] [blame] | 80 | TEST(PlatformThreadTest, JoinsThread) { |
| 81 | // This test flakes if there are problems with the join implementation. |
Markus Handell | 97c4458 | 2021-04-20 17:41:54 +0200 | [diff] [blame] | 82 | rtc::Event event; |
Markus Handell | ad5037b | 2021-05-07 15:02:36 +0200 | [diff] [blame] | 83 | PlatformThread::SpawnJoinable([&] { event.Set(); }, "T"); |
Markus Handell | 2cfc1af | 2022-08-19 08:16:48 +0000 | [diff] [blame] | 84 | EXPECT_TRUE(event.Wait(/*give_up_after=*/webrtc::TimeDelta::Zero())); |
Markus Handell | 97c4458 | 2021-04-20 17:41:54 +0200 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | TEST(PlatformThreadTest, StopsBeforeDetachedThreadExits) { |
| 88 | // This test flakes if there are problems with the detached thread |
| 89 | // implementation. |
| 90 | bool flag = false; |
| 91 | rtc::Event thread_started; |
| 92 | rtc::Event thread_continue; |
| 93 | rtc::Event thread_exiting; |
Markus Handell | ad5037b | 2021-05-07 15:02:36 +0200 | [diff] [blame] | 94 | PlatformThread::SpawnDetached( |
| 95 | [&] { |
| 96 | thread_started.Set(); |
| 97 | thread_continue.Wait(Event::kForever); |
| 98 | flag = true; |
| 99 | thread_exiting.Set(); |
| 100 | }, |
| 101 | "T"); |
Markus Handell | 97c4458 | 2021-04-20 17:41:54 +0200 | [diff] [blame] | 102 | thread_started.Wait(Event::kForever); |
| 103 | EXPECT_FALSE(flag); |
| 104 | thread_continue.Set(); |
| 105 | thread_exiting.Wait(Event::kForever); |
| 106 | EXPECT_TRUE(flag); |
| 107 | } |
| 108 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 109 | } // namespace rtc |