| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ | 9 */ |
| 10 | 10 |
| 11 #include "webrtc/base/platform_thread.h" | 11 #include "webrtc/base/platform_thread.h" |
| 12 | 12 |
| 13 #include "webrtc/system_wrappers/include/sleep.h" | 13 #include "webrtc/system_wrappers/include/sleep.h" |
| 14 #include "webrtc/test/gtest.h" | 14 #include "webrtc/test/gtest.h" |
| 15 | 15 |
| 16 namespace rtc { | 16 namespace rtc { |
| 17 namespace { | 17 namespace { |
| 18 // Function that does nothing, and reports success. | 18 // Function that does nothing, and reports success. |
| 19 bool NullRunFunction(void* obj) { | 19 bool NullRunFunctionDeprecated(void* obj) { |
| 20 webrtc::SleepMs(0); // Hand over timeslice, prevents busy looping. | 20 webrtc::SleepMs(0); // Hand over timeslice, prevents busy looping. |
| 21 return true; | 21 return true; |
| 22 } | 22 } |
| 23 | 23 |
| 24 void NullRunFunction(void* obj) {} |
| 25 |
| 24 // Function that sets a boolean. | 26 // Function that sets a boolean. |
| 25 bool SetFlagRunFunction(void* obj) { | 27 bool SetFlagRunFunctionDeprecated(void* obj) { |
| 26 bool* obj_as_bool = static_cast<bool*>(obj); | 28 bool* obj_as_bool = static_cast<bool*>(obj); |
| 27 *obj_as_bool = true; | 29 *obj_as_bool = true; |
| 28 webrtc::SleepMs(0); // Hand over timeslice, prevents busy looping. | 30 webrtc::SleepMs(0); // Hand over timeslice, prevents busy looping. |
| 29 return true; | 31 return true; |
| 30 } | 32 } |
| 33 |
| 34 void SetFlagRunFunction(void* obj) { |
| 35 bool* obj_as_bool = static_cast<bool*>(obj); |
| 36 *obj_as_bool = true; |
| 37 } |
| 38 |
| 31 } // namespace | 39 } // namespace |
| 32 | 40 |
| 41 TEST(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 |
| 52 TEST(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 |
| 65 TEST(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 |
| 33 TEST(PlatformThreadTest, StartStop) { | 78 TEST(PlatformThreadTest, StartStop) { |
| 34 PlatformThread thread(&NullRunFunction, nullptr, "PlatformThreadTest"); | 79 PlatformThread thread(&NullRunFunction, nullptr, "PlatformThreadTest"); |
| 35 EXPECT_TRUE(thread.name() == "PlatformThreadTest"); | 80 EXPECT_TRUE(thread.name() == "PlatformThreadTest"); |
| 36 EXPECT_TRUE(thread.GetThreadRef() == 0); | 81 EXPECT_TRUE(thread.GetThreadRef() == 0); |
| 37 thread.Start(); | 82 thread.Start(); |
| 38 EXPECT_TRUE(thread.GetThreadRef() != 0); | 83 EXPECT_TRUE(thread.GetThreadRef() != 0); |
| 39 thread.Stop(); | 84 thread.Stop(); |
| 40 EXPECT_TRUE(thread.GetThreadRef() == 0); | 85 EXPECT_TRUE(thread.GetThreadRef() == 0); |
| 41 } | 86 } |
| 42 | 87 |
| (...skipping 12 matching lines...) Expand all Loading... |
| 55 bool flag = false; | 100 bool flag = false; |
| 56 PlatformThread thread(&SetFlagRunFunction, &flag, "RunFunctionIsCalled"); | 101 PlatformThread thread(&SetFlagRunFunction, &flag, "RunFunctionIsCalled"); |
| 57 thread.Start(); | 102 thread.Start(); |
| 58 | 103 |
| 59 // At this point, the flag may be either true or false. | 104 // At this point, the flag may be either true or false. |
| 60 thread.Stop(); | 105 thread.Stop(); |
| 61 | 106 |
| 62 // We expect the thread to have run at least once. | 107 // We expect the thread to have run at least once. |
| 63 EXPECT_TRUE(flag); | 108 EXPECT_TRUE(flag); |
| 64 } | 109 } |
| 110 |
| 65 } // rtc | 111 } // rtc |
| OLD | NEW |