blob: d8c8995507cb7a33c1bddfc59b0cc2c8922b84a5 [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020013#include "system_wrappers/include/sleep.h"
14#include "test/gtest.h"
hta@webrtc.orge1919f42012-05-22 15:57:34 +000015
tommi845afa82016-04-22 09:08:44 -070016namespace rtc {
17namespace {
hta@webrtc.orge1919f42012-05-22 15:57:34 +000018// Function that does nothing, and reports success.
tommi0f8b4032017-02-22 11:22:05 -080019bool NullRunFunctionDeprecated(void* obj) {
tommi500f1b72017-03-02 07:07:09 -080020 webrtc::SleepMs(2); // Hand over timeslice, prevents busy looping.
21 return true;
22}
23
24bool TooBusyRunFunction(void* obj) {
25 // Indentionally busy looping.
hta@webrtc.orge1919f42012-05-22 15:57:34 +000026 return true;
27}
28
tommi0f8b4032017-02-22 11:22:05 -080029void NullRunFunction(void* obj) {}
30
hta@webrtc.orge1919f42012-05-22 15:57:34 +000031// Function that sets a boolean.
tommi0f8b4032017-02-22 11:22:05 -080032bool SetFlagRunFunctionDeprecated(void* obj) {
phoglund@webrtc.orgec9c9422013-01-02 08:45:03 +000033 bool* obj_as_bool = static_cast<bool*>(obj);
hta@webrtc.orge1919f42012-05-22 15:57:34 +000034 *obj_as_bool = true;
tommi845afa82016-04-22 09:08:44 -070035 webrtc::SleepMs(0); // Hand over timeslice, prevents busy looping.
hta@webrtc.orge1919f42012-05-22 15:57:34 +000036 return true;
37}
tommi0f8b4032017-02-22 11:22:05 -080038
39void SetFlagRunFunction(void* obj) {
40 bool* obj_as_bool = static_cast<bool*>(obj);
41 *obj_as_bool = true;
42}
43
tommi845afa82016-04-22 09:08:44 -070044} // namespace
45
tommi0f8b4032017-02-22 11:22:05 -080046TEST(PlatformThreadTest, StartStopDeprecated) {
47 PlatformThread thread(&NullRunFunctionDeprecated, nullptr,
48 "PlatformThreadTest");
49 EXPECT_TRUE(thread.name() == "PlatformThreadTest");
50 EXPECT_TRUE(thread.GetThreadRef() == 0);
51 thread.Start();
52 EXPECT_TRUE(thread.GetThreadRef() != 0);
53 thread.Stop();
54 EXPECT_TRUE(thread.GetThreadRef() == 0);
55}
56
57TEST(PlatformThreadTest, StartStop2Deprecated) {
58 PlatformThread thread1(&NullRunFunctionDeprecated, nullptr,
59 "PlatformThreadTest1");
60 PlatformThread thread2(&NullRunFunctionDeprecated, nullptr,
61 "PlatformThreadTest2");
62 EXPECT_TRUE(thread1.GetThreadRef() == thread2.GetThreadRef());
63 thread1.Start();
64 thread2.Start();
65 EXPECT_TRUE(thread1.GetThreadRef() != thread2.GetThreadRef());
66 thread2.Stop();
67 thread1.Stop();
68}
69
70TEST(PlatformThreadTest, RunFunctionIsCalledDeprecated) {
71 bool flag = false;
72 PlatformThread thread(&SetFlagRunFunctionDeprecated, &flag,
73 "RunFunctionIsCalled");
74 thread.Start();
75
76 // At this point, the flag may be either true or false.
77 thread.Stop();
78
79 // We expect the thread to have run at least once.
80 EXPECT_TRUE(flag);
81}
82
tommi845afa82016-04-22 09:08:44 -070083TEST(PlatformThreadTest, StartStop) {
84 PlatformThread thread(&NullRunFunction, nullptr, "PlatformThreadTest");
85 EXPECT_TRUE(thread.name() == "PlatformThreadTest");
86 EXPECT_TRUE(thread.GetThreadRef() == 0);
87 thread.Start();
88 EXPECT_TRUE(thread.GetThreadRef() != 0);
89 thread.Stop();
90 EXPECT_TRUE(thread.GetThreadRef() == 0);
91}
92
93TEST(PlatformThreadTest, StartStop2) {
94 PlatformThread thread1(&NullRunFunction, nullptr, "PlatformThreadTest1");
95 PlatformThread thread2(&NullRunFunction, nullptr, "PlatformThreadTest2");
96 EXPECT_TRUE(thread1.GetThreadRef() == thread2.GetThreadRef());
97 thread1.Start();
98 thread2.Start();
99 EXPECT_TRUE(thread1.GetThreadRef() != thread2.GetThreadRef());
100 thread2.Stop();
101 thread1.Stop();
102}
hta@webrtc.orge1919f42012-05-22 15:57:34 +0000103
pbos12411ef2015-11-23 14:47:56 -0800104TEST(PlatformThreadTest, RunFunctionIsCalled) {
hta@webrtc.orge1919f42012-05-22 15:57:34 +0000105 bool flag = false;
tommi845afa82016-04-22 09:08:44 -0700106 PlatformThread thread(&SetFlagRunFunction, &flag, "RunFunctionIsCalled");
Peter Boström8c38e8b2015-11-26 17:45:47 +0100107 thread.Start();
phoglund@webrtc.orgec9c9422013-01-02 08:45:03 +0000108
hta@webrtc.orge1919f42012-05-22 15:57:34 +0000109 // At this point, the flag may be either true or false.
Peter Boström8c38e8b2015-11-26 17:45:47 +0100110 thread.Stop();
phoglund@webrtc.orgec9c9422013-01-02 08:45:03 +0000111
hta@webrtc.orge1919f42012-05-22 15:57:34 +0000112 // We expect the thread to have run at least once.
113 EXPECT_TRUE(flag);
hta@webrtc.orge1919f42012-05-22 15:57:34 +0000114}
tommi0f8b4032017-02-22 11:22:05 -0800115
tommi500f1b72017-03-02 07:07:09 -0800116// This test is disabled since it will cause a crash.
117// There might be a way to implement this as a death test, but it looks like
118// a death test requires an expression to be checked but does not allow a
119// flag to be raised that says "some thread will crash after this point".
120// TODO(tommi): Look into ways to enable the test by default.
121TEST(PlatformThreadTest, DISABLED_TooBusyDeprecated) {
122 PlatformThread thread(&TooBusyRunFunction, nullptr, "BusyThread");
123 thread.Start();
124 webrtc::SleepMs(1000);
125 thread.Stop();
126}
127
tommi845afa82016-04-22 09:08:44 -0700128} // rtc