blob: 97b25e02e261c40936b404d334b4aa33a53528aa [file] [log] [blame]
hta@webrtc.orge1919f42012-05-22 15:57:34 +00001/*
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.org40300132012-05-23 15:49:48 +000010
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "rtc_base/platform_thread.h"
hta@webrtc.orge1919f42012-05-22 15:57:34 +000012
Markus Handellad5037b2021-05-07 15:02:36 +020013#include "absl/types/optional.h"
Markus Handell97c44582021-04-20 17:41:54 +020014#include "rtc_base/event.h"
15#include "system_wrappers/include/sleep.h"
16#include "test/gmock.h"
hta@webrtc.orge1919f42012-05-22 15:57:34 +000017
tommi845afa82016-04-22 09:08:44 -070018namespace rtc {
hta@webrtc.orge1919f42012-05-22 15:57:34 +000019
Markus Handellad5037b2021-05-07 15:02:36 +020020TEST(PlatformThreadTest, DefaultConstructedIsEmpty) {
21 PlatformThread thread;
22 EXPECT_EQ(thread.GetHandle(), absl::nullopt);
23 EXPECT_TRUE(thread.empty());
tommi0f8b4032017-02-22 11:22:05 -080024}
25
Markus Handellad5037b2021-05-07 15:02:36 +020026TEST(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 Handell74543b72021-06-28 10:29:15 +020032 rtc::Event done;
33 thread = PlatformThread::SpawnDetached([&] { done.Set(); }, "2");
Markus Handellad5037b2021-05-07 15:02:36 +020034 EXPECT_FALSE(thread.empty());
35 thread.Finalize();
36 EXPECT_TRUE(thread.empty());
Markus Handell2cfc1af2022-08-19 08:16:48 +000037 done.Wait(webrtc::TimeDelta::Seconds(30));
Markus Handell97c44582021-04-20 17:41:54 +020038}
39
Markus Handellad5037b2021-05-07 15:02:36 +020040TEST(PlatformThreadTest, MovesEmpty) {
41 PlatformThread thread1;
42 PlatformThread thread2 = std::move(thread1);
43 EXPECT_TRUE(thread1.empty());
44 EXPECT_TRUE(thread2.empty());
tommi845afa82016-04-22 09:08:44 -070045}
46
Markus Handellad5037b2021-05-07 15:02:36 +020047TEST(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 Handell74543b72021-06-28 10:29:15 +020052 rtc::Event done;
53 thread1 = PlatformThread::SpawnDetached([&] { done.Set(); }, "2");
Markus Handellad5037b2021-05-07 15:02:36 +020054 thread2 = std::move(thread1);
55 EXPECT_TRUE(thread1.empty());
56 EXPECT_FALSE(thread2.empty());
Markus Handell2cfc1af2022-08-19 08:16:48 +000057 done.Wait(webrtc::TimeDelta::Seconds(30));
Markus Handellad5037b2021-05-07 15:02:36 +020058}
59
60TEST(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());
tommi845afa82016-04-22 09:08:44 -070072}
hta@webrtc.orge1919f42012-05-22 15:57:34 +000073
pbos12411ef2015-11-23 14:47:56 -080074TEST(PlatformThreadTest, RunFunctionIsCalled) {
hta@webrtc.orge1919f42012-05-22 15:57:34 +000075 bool flag = false;
Markus Handellad5037b2021-05-07 15:02:36 +020076 PlatformThread::SpawnJoinable([&] { flag = true; }, "T");
hta@webrtc.orge1919f42012-05-22 15:57:34 +000077 EXPECT_TRUE(flag);
hta@webrtc.orge1919f42012-05-22 15:57:34 +000078}
tommi0f8b4032017-02-22 11:22:05 -080079
Markus Handell97c44582021-04-20 17:41:54 +020080TEST(PlatformThreadTest, JoinsThread) {
81 // This test flakes if there are problems with the join implementation.
Markus Handell97c44582021-04-20 17:41:54 +020082 rtc::Event event;
Markus Handellad5037b2021-05-07 15:02:36 +020083 PlatformThread::SpawnJoinable([&] { event.Set(); }, "T");
Markus Handell2cfc1af2022-08-19 08:16:48 +000084 EXPECT_TRUE(event.Wait(/*give_up_after=*/webrtc::TimeDelta::Zero()));
Markus Handell97c44582021-04-20 17:41:54 +020085}
86
87TEST(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 Handellad5037b2021-05-07 15:02:36 +020094 PlatformThread::SpawnDetached(
95 [&] {
96 thread_started.Set();
97 thread_continue.Wait(Event::kForever);
98 flag = true;
99 thread_exiting.Set();
100 },
101 "T");
Markus Handell97c44582021-04-20 17:41:54 +0200102 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 Gerey665174f2018-06-19 15:03:05 +0200109} // namespace rtc