hta@webrtc.org | e1919f4 | 2012-05-22 15:57:34 | [diff] [blame] | 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 | */ |
hta@webrtc.org | 4030013 | 2012-05-23 15:49:48 | [diff] [blame] | 10 | |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 11 | #include "rtc_base/platform_thread.h" |
hta@webrtc.org | e1919f4 | 2012-05-22 15:57:34 | [diff] [blame] | 12 | |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 13 | #include "system_wrappers/include/sleep.h" |
| 14 | #include "test/gtest.h" |
hta@webrtc.org | e1919f4 | 2012-05-22 15:57:34 | [diff] [blame] | 15 | |
tommi | 845afa8 | 2016-04-22 16:08:44 | [diff] [blame] | 16 | namespace rtc { |
| 17 | namespace { |
hta@webrtc.org | e1919f4 | 2012-05-22 15:57:34 | [diff] [blame] | 18 | // Function that does nothing, and reports success. |
tommi | 0f8b403 | 2017-02-22 19:22:05 | [diff] [blame] | 19 | bool NullRunFunctionDeprecated(void* obj) { |
tommi | 500f1b7 | 2017-03-02 15:07:09 | [diff] [blame] | 20 | webrtc::SleepMs(2); // Hand over timeslice, prevents busy looping. |
| 21 | return true; |
| 22 | } |
| 23 | |
| 24 | bool TooBusyRunFunction(void* obj) { |
| 25 | // Indentionally busy looping. |
hta@webrtc.org | e1919f4 | 2012-05-22 15:57:34 | [diff] [blame] | 26 | return true; |
| 27 | } |
| 28 | |
tommi | 0f8b403 | 2017-02-22 19:22:05 | [diff] [blame] | 29 | void NullRunFunction(void* obj) {} |
| 30 | |
hta@webrtc.org | e1919f4 | 2012-05-22 15:57:34 | [diff] [blame] | 31 | // Function that sets a boolean. |
tommi | 0f8b403 | 2017-02-22 19:22:05 | [diff] [blame] | 32 | bool SetFlagRunFunctionDeprecated(void* obj) { |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 | [diff] [blame] | 33 | bool* obj_as_bool = static_cast<bool*>(obj); |
hta@webrtc.org | e1919f4 | 2012-05-22 15:57:34 | [diff] [blame] | 34 | *obj_as_bool = true; |
tommi | 845afa8 | 2016-04-22 16:08:44 | [diff] [blame] | 35 | webrtc::SleepMs(0); // Hand over timeslice, prevents busy looping. |
hta@webrtc.org | e1919f4 | 2012-05-22 15:57:34 | [diff] [blame] | 36 | return true; |
| 37 | } |
tommi | 0f8b403 | 2017-02-22 19:22:05 | [diff] [blame] | 38 | |
| 39 | void SetFlagRunFunction(void* obj) { |
| 40 | bool* obj_as_bool = static_cast<bool*>(obj); |
| 41 | *obj_as_bool = true; |
| 42 | } |
| 43 | |
tommi | 845afa8 | 2016-04-22 16:08:44 | [diff] [blame] | 44 | } // namespace |
| 45 | |
tommi | 0f8b403 | 2017-02-22 19:22:05 | [diff] [blame] | 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 | |
tommi | 845afa8 | 2016-04-22 16:08:44 | [diff] [blame] | 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 | } |
hta@webrtc.org | e1919f4 | 2012-05-22 15:57:34 | [diff] [blame] | 103 | |
pbos | 12411ef | 2015-11-23 22:47:56 | [diff] [blame] | 104 | TEST(PlatformThreadTest, RunFunctionIsCalled) { |
hta@webrtc.org | e1919f4 | 2012-05-22 15:57:34 | [diff] [blame] | 105 | bool flag = false; |
tommi | 845afa8 | 2016-04-22 16:08:44 | [diff] [blame] | 106 | PlatformThread thread(&SetFlagRunFunction, &flag, "RunFunctionIsCalled"); |
Peter Boström | 8c38e8b | 2015-11-26 16:45:47 | [diff] [blame] | 107 | thread.Start(); |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 | [diff] [blame] | 108 | |
hta@webrtc.org | e1919f4 | 2012-05-22 15:57:34 | [diff] [blame] | 109 | // At this point, the flag may be either true or false. |
Peter Boström | 8c38e8b | 2015-11-26 16:45:47 | [diff] [blame] | 110 | thread.Stop(); |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 | [diff] [blame] | 111 | |
hta@webrtc.org | e1919f4 | 2012-05-22 15:57:34 | [diff] [blame] | 112 | // We expect the thread to have run at least once. |
| 113 | EXPECT_TRUE(flag); |
hta@webrtc.org | e1919f4 | 2012-05-22 15:57:34 | [diff] [blame] | 114 | } |
tommi | 0f8b403 | 2017-02-22 19:22:05 | [diff] [blame] | 115 | |
tommi | 500f1b7 | 2017-03-02 15:07:09 | [diff] [blame] | 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 | |
tommi | 845afa8 | 2016-04-22 16:08:44 | [diff] [blame] | 128 | } // rtc |