blob: d8c8995507cb7a33c1bddfc59b0cc2c8922b84a5 [file] [log] [blame]
hta@webrtc.orge1919f42012-05-22 15:57:341/*
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:4810
Mirko Bonadei92ea95e2017-09-15 04:47:3111#include "rtc_base/platform_thread.h"
hta@webrtc.orge1919f42012-05-22 15:57:3412
Mirko Bonadei92ea95e2017-09-15 04:47:3113#include "system_wrappers/include/sleep.h"
14#include "test/gtest.h"
hta@webrtc.orge1919f42012-05-22 15:57:3415
tommi845afa82016-04-22 16:08:4416namespace rtc {
17namespace {
hta@webrtc.orge1919f42012-05-22 15:57:3418// Function that does nothing, and reports success.
tommi0f8b4032017-02-22 19:22:0519bool NullRunFunctionDeprecated(void* obj) {
tommi500f1b72017-03-02 15:07:0920 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:3426 return true;
27}
28
tommi0f8b4032017-02-22 19:22:0529void NullRunFunction(void* obj) {}
30
hta@webrtc.orge1919f42012-05-22 15:57:3431// Function that sets a boolean.
tommi0f8b4032017-02-22 19:22:0532bool SetFlagRunFunctionDeprecated(void* obj) {
phoglund@webrtc.orgec9c9422013-01-02 08:45:0333 bool* obj_as_bool = static_cast<bool*>(obj);
hta@webrtc.orge1919f42012-05-22 15:57:3434 *obj_as_bool = true;
tommi845afa82016-04-22 16:08:4435 webrtc::SleepMs(0); // Hand over timeslice, prevents busy looping.
hta@webrtc.orge1919f42012-05-22 15:57:3436 return true;
37}
tommi0f8b4032017-02-22 19:22:0538
39void SetFlagRunFunction(void* obj) {
40 bool* obj_as_bool = static_cast<bool*>(obj);
41 *obj_as_bool = true;
42}
43
tommi845afa82016-04-22 16:08:4444} // namespace
45
tommi0f8b4032017-02-22 19:22:0546TEST(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 16:08:4483TEST(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:34103
pbos12411ef2015-11-23 22:47:56104TEST(PlatformThreadTest, RunFunctionIsCalled) {
hta@webrtc.orge1919f42012-05-22 15:57:34105 bool flag = false;
tommi845afa82016-04-22 16:08:44106 PlatformThread thread(&SetFlagRunFunction, &flag, "RunFunctionIsCalled");
Peter Boström8c38e8b2015-11-26 16:45:47107 thread.Start();
phoglund@webrtc.orgec9c9422013-01-02 08:45:03108
hta@webrtc.orge1919f42012-05-22 15:57:34109 // At this point, the flag may be either true or false.
Peter Boström8c38e8b2015-11-26 16:45:47110 thread.Stop();
phoglund@webrtc.orgec9c9422013-01-02 08:45:03111
hta@webrtc.orge1919f42012-05-22 15:57:34112 // We expect the thread to have run at least once.
113 EXPECT_TRUE(flag);
hta@webrtc.orge1919f42012-05-22 15:57:34114}
tommi0f8b4032017-02-22 19:22:05115
tommi500f1b72017-03-02 15:07:09116// 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 16:08:44128} // rtc