OLD | NEW |
| (Empty) |
1 /* | |
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 */ | |
10 | |
11 #include "webrtc/base/platform_thread.h" | |
12 | |
13 #include "webrtc/system_wrappers/include/sleep.h" | |
14 #include "webrtc/test/gtest.h" | |
15 | |
16 namespace rtc { | |
17 namespace { | |
18 // Function that does nothing, and reports success. | |
19 bool NullRunFunctionDeprecated(void* obj) { | |
20 webrtc::SleepMs(2); // Hand over timeslice, prevents busy looping. | |
21 return true; | |
22 } | |
23 | |
24 bool TooBusyRunFunction(void* obj) { | |
25 // Indentionally busy looping. | |
26 return true; | |
27 } | |
28 | |
29 void NullRunFunction(void* obj) {} | |
30 | |
31 // Function that sets a boolean. | |
32 bool SetFlagRunFunctionDeprecated(void* obj) { | |
33 bool* obj_as_bool = static_cast<bool*>(obj); | |
34 *obj_as_bool = true; | |
35 webrtc::SleepMs(0); // Hand over timeslice, prevents busy looping. | |
36 return true; | |
37 } | |
38 | |
39 void SetFlagRunFunction(void* obj) { | |
40 bool* obj_as_bool = static_cast<bool*>(obj); | |
41 *obj_as_bool = true; | |
42 } | |
43 | |
44 } // namespace | |
45 | |
46 TEST(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 | |
57 TEST(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 | |
70 TEST(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 | |
83 TEST(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 | |
93 TEST(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 } | |
103 | |
104 TEST(PlatformThreadTest, RunFunctionIsCalled) { | |
105 bool flag = false; | |
106 PlatformThread thread(&SetFlagRunFunction, &flag, "RunFunctionIsCalled"); | |
107 thread.Start(); | |
108 | |
109 // At this point, the flag may be either true or false. | |
110 thread.Stop(); | |
111 | |
112 // We expect the thread to have run at least once. | |
113 EXPECT_TRUE(flag); | |
114 } | |
115 | |
116 // 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. | |
121 TEST(PlatformThreadTest, DISABLED_TooBusyDeprecated) { | |
122 PlatformThread thread(&TooBusyRunFunction, nullptr, "BusyThread"); | |
123 thread.Start(); | |
124 webrtc::SleepMs(1000); | |
125 thread.Stop(); | |
126 } | |
127 | |
128 } // rtc | |
OLD | NEW |