blob: d09772fddc9b85182a9a830778dda6e0e7bed24f [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 Handell97c44582021-04-20 17:41:54 +020013#include "rtc_base/event.h"
14#include "system_wrappers/include/sleep.h"
15#include "test/gmock.h"
hta@webrtc.orge1919f42012-05-22 15:57:34 +000016
tommi845afa82016-04-22 09:08:44 -070017namespace rtc {
Guido Urdaneta793bac52021-05-06 13:12:47 +000018namespace {
hta@webrtc.orge1919f42012-05-22 15:57:34 +000019
Guido Urdaneta793bac52021-05-06 13:12:47 +000020void NullRunFunction(void* obj) {}
21
22// Function that sets a boolean.
23void SetFlagRunFunction(void* obj) {
24 bool* obj_as_bool = static_cast<bool*>(obj);
25 *obj_as_bool = true;
tommi0f8b4032017-02-22 11:22:05 -080026}
27
Guido Urdaneta793bac52021-05-06 13:12:47 +000028void StdFunctionRunFunction(void* obj) {
29 std::function<void()>* fun = static_cast<std::function<void()>*>(obj);
30 (*fun)();
Markus Handell97c44582021-04-20 17:41:54 +020031}
32
Guido Urdaneta793bac52021-05-06 13:12:47 +000033} // namespace
34
35TEST(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);
tommi845afa82016-04-22 09:08:44 -070043}
44
Guido Urdaneta793bac52021-05-06 13:12:47 +000045TEST(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();
tommi845afa82016-04-22 09:08:44 -070054}
hta@webrtc.orge1919f42012-05-22 15:57:34 +000055
pbos12411ef2015-11-23 14:47:56 -080056TEST(PlatformThreadTest, RunFunctionIsCalled) {
hta@webrtc.orge1919f42012-05-22 15:57:34 +000057 bool flag = false;
Guido Urdaneta793bac52021-05-06 13:12:47 +000058 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.orge1919f42012-05-22 15:57:34 +000065 EXPECT_TRUE(flag);
hta@webrtc.orge1919f42012-05-22 15:57:34 +000066}
tommi0f8b4032017-02-22 11:22:05 -080067
Markus Handell97c44582021-04-20 17:41:54 +020068TEST(PlatformThreadTest, JoinsThread) {
69 // This test flakes if there are problems with the join implementation.
Guido Urdaneta793bac52021-05-06 13:12:47 +000070 EXPECT_TRUE(ThreadAttributes().joinable);
Markus Handell97c44582021-04-20 17:41:54 +020071 rtc::Event event;
Guido Urdaneta793bac52021-05-06 13:12:47 +000072 std::function<void()> thread_function = [&] { event.Set(); };
73 PlatformThread thread(&StdFunctionRunFunction, &thread_function, "T");
74 thread.Start();
75 thread.Stop();
Markus Handell97c44582021-04-20 17:41:54 +020076 EXPECT_TRUE(event.Wait(/*give_up_after_ms=*/0));
77}
78
79TEST(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 Urdaneta793bac52021-05-06 13:12:47 +000086 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 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