blob: 0da822cf855093406ec219683feedcf5cd0696dd [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());
32 thread = PlatformThread::SpawnDetached([] {}, "2");
33 EXPECT_FALSE(thread.empty());
34 thread.Finalize();
35 EXPECT_TRUE(thread.empty());
Markus Handell97c44582021-04-20 17:41:54 +020036}
37
Markus Handellad5037b2021-05-07 15:02:36 +020038TEST(PlatformThreadTest, MovesEmpty) {
39 PlatformThread thread1;
40 PlatformThread thread2 = std::move(thread1);
41 EXPECT_TRUE(thread1.empty());
42 EXPECT_TRUE(thread2.empty());
tommi845afa82016-04-22 09:08:44 -070043}
44
Markus Handellad5037b2021-05-07 15:02:36 +020045TEST(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
56TEST(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());
tommi845afa82016-04-22 09:08:44 -070068}
hta@webrtc.orge1919f42012-05-22 15:57:34 +000069
pbos12411ef2015-11-23 14:47:56 -080070TEST(PlatformThreadTest, RunFunctionIsCalled) {
hta@webrtc.orge1919f42012-05-22 15:57:34 +000071 bool flag = false;
Markus Handellad5037b2021-05-07 15:02:36 +020072 PlatformThread::SpawnJoinable([&] { flag = true; }, "T");
hta@webrtc.orge1919f42012-05-22 15:57:34 +000073 EXPECT_TRUE(flag);
hta@webrtc.orge1919f42012-05-22 15:57:34 +000074}
tommi0f8b4032017-02-22 11:22:05 -080075
Markus Handell97c44582021-04-20 17:41:54 +020076TEST(PlatformThreadTest, JoinsThread) {
77 // This test flakes if there are problems with the join implementation.
Markus Handell97c44582021-04-20 17:41:54 +020078 rtc::Event event;
Markus Handellad5037b2021-05-07 15:02:36 +020079 PlatformThread::SpawnJoinable([&] { event.Set(); }, "T");
Markus Handell97c44582021-04-20 17:41:54 +020080 EXPECT_TRUE(event.Wait(/*give_up_after_ms=*/0));
81}
82
83TEST(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 Handellad5037b2021-05-07 15:02:36 +020090 PlatformThread::SpawnDetached(
91 [&] {
92 thread_started.Set();
93 thread_continue.Wait(Event::kForever);
94 flag = true;
95 thread_exiting.Set();
96 },
97 "T");
Markus Handell97c44582021-04-20 17:41:54 +020098 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 Gerey665174f2018-06-19 15:03:05 +0200105} // namespace rtc