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()); |
| 32 | thread = PlatformThread::SpawnDetached([] {}, "2"); |
| 33 | EXPECT_FALSE(thread.empty()); |
| 34 | thread.Finalize(); |
| 35 | EXPECT_TRUE(thread.empty()); |
Markus Handell | 97c4458 | 2021-04-20 17:41:54 +0200 | [diff] [blame] | 36 | } |
| 37 | |
Markus Handell | ad5037b | 2021-05-07 15:02:36 +0200 | [diff] [blame] | 38 | TEST(PlatformThreadTest, MovesEmpty) { |
| 39 | PlatformThread thread1; |
| 40 | PlatformThread thread2 = std::move(thread1); |
| 41 | EXPECT_TRUE(thread1.empty()); |
| 42 | EXPECT_TRUE(thread2.empty()); |
tommi | 845afa8 | 2016-04-22 09:08:44 -0700 | [diff] [blame] | 43 | } |
| 44 | |
Markus Handell | ad5037b | 2021-05-07 15:02:36 +0200 | [diff] [blame] | 45 | TEST(PlatformThreadTest, MovesHandles) { |
| 46 | PlatformThread thread1 = PlatformThread::SpawnJoinable([] {}, "1"); |
| 47 | PlatformThread thread2 = std::move(thread1); |
| 48 | EXPECT_TRUE(thread1.empty()); |
| 49 | EXPECT_FALSE(thread2.empty()); |
| 50 | thread1 = PlatformThread::SpawnDetached([] {}, "2"); |
| 51 | thread2 = std::move(thread1); |
| 52 | EXPECT_TRUE(thread1.empty()); |
| 53 | EXPECT_FALSE(thread2.empty()); |
| 54 | } |
| 55 | |
| 56 | TEST(PlatformThreadTest, |
| 57 | TwoThreadHandlesAreDifferentWhenStartedAndEqualWhenJoined) { |
| 58 | PlatformThread thread1 = PlatformThread(); |
| 59 | PlatformThread thread2 = PlatformThread(); |
| 60 | EXPECT_EQ(thread1.GetHandle(), thread2.GetHandle()); |
| 61 | thread1 = PlatformThread::SpawnJoinable([] {}, "1"); |
| 62 | thread2 = PlatformThread::SpawnJoinable([] {}, "2"); |
| 63 | EXPECT_NE(thread1.GetHandle(), thread2.GetHandle()); |
| 64 | thread1.Finalize(); |
| 65 | EXPECT_NE(thread1.GetHandle(), thread2.GetHandle()); |
| 66 | thread2.Finalize(); |
| 67 | EXPECT_EQ(thread1.GetHandle(), thread2.GetHandle()); |
tommi | 845afa8 | 2016-04-22 09:08:44 -0700 | [diff] [blame] | 68 | } |
hta@webrtc.org | e1919f4 | 2012-05-22 15:57:34 +0000 | [diff] [blame] | 69 | |
pbos | 12411ef | 2015-11-23 14:47:56 -0800 | [diff] [blame] | 70 | TEST(PlatformThreadTest, RunFunctionIsCalled) { |
hta@webrtc.org | e1919f4 | 2012-05-22 15:57:34 +0000 | [diff] [blame] | 71 | bool flag = false; |
Markus Handell | ad5037b | 2021-05-07 15:02:36 +0200 | [diff] [blame] | 72 | PlatformThread::SpawnJoinable([&] { flag = true; }, "T"); |
hta@webrtc.org | e1919f4 | 2012-05-22 15:57:34 +0000 | [diff] [blame] | 73 | EXPECT_TRUE(flag); |
hta@webrtc.org | e1919f4 | 2012-05-22 15:57:34 +0000 | [diff] [blame] | 74 | } |
tommi | 0f8b403 | 2017-02-22 11:22:05 -0800 | [diff] [blame] | 75 | |
Markus Handell | 97c4458 | 2021-04-20 17:41:54 +0200 | [diff] [blame] | 76 | TEST(PlatformThreadTest, JoinsThread) { |
| 77 | // This test flakes if there are problems with the join implementation. |
Markus Handell | 97c4458 | 2021-04-20 17:41:54 +0200 | [diff] [blame] | 78 | rtc::Event event; |
Markus Handell | ad5037b | 2021-05-07 15:02:36 +0200 | [diff] [blame] | 79 | PlatformThread::SpawnJoinable([&] { event.Set(); }, "T"); |
Markus Handell | 97c4458 | 2021-04-20 17:41:54 +0200 | [diff] [blame] | 80 | EXPECT_TRUE(event.Wait(/*give_up_after_ms=*/0)); |
| 81 | } |
| 82 | |
| 83 | TEST(PlatformThreadTest, StopsBeforeDetachedThreadExits) { |
| 84 | // This test flakes if there are problems with the detached thread |
| 85 | // implementation. |
| 86 | bool flag = false; |
| 87 | rtc::Event thread_started; |
| 88 | rtc::Event thread_continue; |
| 89 | rtc::Event thread_exiting; |
Markus Handell | ad5037b | 2021-05-07 15:02:36 +0200 | [diff] [blame] | 90 | PlatformThread::SpawnDetached( |
| 91 | [&] { |
| 92 | thread_started.Set(); |
| 93 | thread_continue.Wait(Event::kForever); |
| 94 | flag = true; |
| 95 | thread_exiting.Set(); |
| 96 | }, |
| 97 | "T"); |
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 |