blob: d6d35e40e4955dfe82367ac29e3ed2de00c1efbf [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
pbos12411ef2015-11-23 14:47:56 -080011#include "webrtc/base/platform_thread.h"
hta@webrtc.orge1919f42012-05-22 15:57:34 +000012
Henrik Kjellander98f53512015-10-28 18:17:40 +010013#include "webrtc/system_wrappers/include/sleep.h"
kwibergac9f8762016-09-30 22:29:43 -070014#include "webrtc/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) {
tommi845afa82016-04-22 09:08:44 -070020 webrtc::SleepMs(0); // Hand over timeslice, prevents busy looping.
hta@webrtc.orge1919f42012-05-22 15:57:34 +000021 return true;
22}
23
tommi0f8b4032017-02-22 11:22:05 -080024void NullRunFunction(void* obj) {}
25
hta@webrtc.orge1919f42012-05-22 15:57:34 +000026// Function that sets a boolean.
tommi0f8b4032017-02-22 11:22:05 -080027bool SetFlagRunFunctionDeprecated(void* obj) {
phoglund@webrtc.orgec9c9422013-01-02 08:45:03 +000028 bool* obj_as_bool = static_cast<bool*>(obj);
hta@webrtc.orge1919f42012-05-22 15:57:34 +000029 *obj_as_bool = true;
tommi845afa82016-04-22 09:08:44 -070030 webrtc::SleepMs(0); // Hand over timeslice, prevents busy looping.
hta@webrtc.orge1919f42012-05-22 15:57:34 +000031 return true;
32}
tommi0f8b4032017-02-22 11:22:05 -080033
34void SetFlagRunFunction(void* obj) {
35 bool* obj_as_bool = static_cast<bool*>(obj);
36 *obj_as_bool = true;
37}
38
tommi845afa82016-04-22 09:08:44 -070039} // namespace
40
tommi0f8b4032017-02-22 11:22:05 -080041TEST(PlatformThreadTest, StartStopDeprecated) {
42 PlatformThread thread(&NullRunFunctionDeprecated, nullptr,
43 "PlatformThreadTest");
44 EXPECT_TRUE(thread.name() == "PlatformThreadTest");
45 EXPECT_TRUE(thread.GetThreadRef() == 0);
46 thread.Start();
47 EXPECT_TRUE(thread.GetThreadRef() != 0);
48 thread.Stop();
49 EXPECT_TRUE(thread.GetThreadRef() == 0);
50}
51
52TEST(PlatformThreadTest, StartStop2Deprecated) {
53 PlatformThread thread1(&NullRunFunctionDeprecated, nullptr,
54 "PlatformThreadTest1");
55 PlatformThread thread2(&NullRunFunctionDeprecated, nullptr,
56 "PlatformThreadTest2");
57 EXPECT_TRUE(thread1.GetThreadRef() == thread2.GetThreadRef());
58 thread1.Start();
59 thread2.Start();
60 EXPECT_TRUE(thread1.GetThreadRef() != thread2.GetThreadRef());
61 thread2.Stop();
62 thread1.Stop();
63}
64
65TEST(PlatformThreadTest, RunFunctionIsCalledDeprecated) {
66 bool flag = false;
67 PlatformThread thread(&SetFlagRunFunctionDeprecated, &flag,
68 "RunFunctionIsCalled");
69 thread.Start();
70
71 // At this point, the flag may be either true or false.
72 thread.Stop();
73
74 // We expect the thread to have run at least once.
75 EXPECT_TRUE(flag);
76}
77
tommi845afa82016-04-22 09:08:44 -070078TEST(PlatformThreadTest, StartStop) {
79 PlatformThread thread(&NullRunFunction, nullptr, "PlatformThreadTest");
80 EXPECT_TRUE(thread.name() == "PlatformThreadTest");
81 EXPECT_TRUE(thread.GetThreadRef() == 0);
82 thread.Start();
83 EXPECT_TRUE(thread.GetThreadRef() != 0);
84 thread.Stop();
85 EXPECT_TRUE(thread.GetThreadRef() == 0);
86}
87
88TEST(PlatformThreadTest, StartStop2) {
89 PlatformThread thread1(&NullRunFunction, nullptr, "PlatformThreadTest1");
90 PlatformThread thread2(&NullRunFunction, nullptr, "PlatformThreadTest2");
91 EXPECT_TRUE(thread1.GetThreadRef() == thread2.GetThreadRef());
92 thread1.Start();
93 thread2.Start();
94 EXPECT_TRUE(thread1.GetThreadRef() != thread2.GetThreadRef());
95 thread2.Stop();
96 thread1.Stop();
97}
hta@webrtc.orge1919f42012-05-22 15:57:34 +000098
pbos12411ef2015-11-23 14:47:56 -080099TEST(PlatformThreadTest, RunFunctionIsCalled) {
hta@webrtc.orge1919f42012-05-22 15:57:34 +0000100 bool flag = false;
tommi845afa82016-04-22 09:08:44 -0700101 PlatformThread thread(&SetFlagRunFunction, &flag, "RunFunctionIsCalled");
Peter Boström8c38e8b2015-11-26 17:45:47 +0100102 thread.Start();
phoglund@webrtc.orgec9c9422013-01-02 08:45:03 +0000103
hta@webrtc.orge1919f42012-05-22 15:57:34 +0000104 // At this point, the flag may be either true or false.
Peter Boström8c38e8b2015-11-26 17:45:47 +0100105 thread.Stop();
phoglund@webrtc.orgec9c9422013-01-02 08:45:03 +0000106
hta@webrtc.orge1919f42012-05-22 15:57:34 +0000107 // We expect the thread to have run at least once.
108 EXPECT_TRUE(flag);
hta@webrtc.orge1919f42012-05-22 15:57:34 +0000109}
tommi0f8b4032017-02-22 11:22:05 -0800110
tommi845afa82016-04-22 09:08:44 -0700111} // rtc