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 | |
tommi | 0f8b403 | 2017-02-22 19:22:05 | [diff] [blame] | 19 | void NullRunFunction(void* obj) {} |
| 20 | |
hta@webrtc.org | e1919f4 | 2012-05-22 15:57:34 | [diff] [blame] | 21 | // Function that sets a boolean. |
tommi | 0f8b403 | 2017-02-22 19:22:05 | [diff] [blame] | 22 | void SetFlagRunFunction(void* obj) { |
| 23 | bool* obj_as_bool = static_cast<bool*>(obj); |
| 24 | *obj_as_bool = true; |
| 25 | } |
| 26 | |
tommi | 845afa8 | 2016-04-22 16:08:44 | [diff] [blame] | 27 | } // namespace |
| 28 | |
| 29 | TEST(PlatformThreadTest, StartStop) { |
| 30 | PlatformThread thread(&NullRunFunction, nullptr, "PlatformThreadTest"); |
| 31 | EXPECT_TRUE(thread.name() == "PlatformThreadTest"); |
| 32 | EXPECT_TRUE(thread.GetThreadRef() == 0); |
| 33 | thread.Start(); |
| 34 | EXPECT_TRUE(thread.GetThreadRef() != 0); |
| 35 | thread.Stop(); |
| 36 | EXPECT_TRUE(thread.GetThreadRef() == 0); |
| 37 | } |
| 38 | |
| 39 | TEST(PlatformThreadTest, StartStop2) { |
| 40 | PlatformThread thread1(&NullRunFunction, nullptr, "PlatformThreadTest1"); |
| 41 | PlatformThread thread2(&NullRunFunction, nullptr, "PlatformThreadTest2"); |
| 42 | EXPECT_TRUE(thread1.GetThreadRef() == thread2.GetThreadRef()); |
| 43 | thread1.Start(); |
| 44 | thread2.Start(); |
| 45 | EXPECT_TRUE(thread1.GetThreadRef() != thread2.GetThreadRef()); |
| 46 | thread2.Stop(); |
| 47 | thread1.Stop(); |
| 48 | } |
hta@webrtc.org | e1919f4 | 2012-05-22 15:57:34 | [diff] [blame] | 49 | |
pbos | 12411ef | 2015-11-23 22:47:56 | [diff] [blame] | 50 | TEST(PlatformThreadTest, RunFunctionIsCalled) { |
hta@webrtc.org | e1919f4 | 2012-05-22 15:57:34 | [diff] [blame] | 51 | bool flag = false; |
tommi | 845afa8 | 2016-04-22 16:08:44 | [diff] [blame] | 52 | PlatformThread thread(&SetFlagRunFunction, &flag, "RunFunctionIsCalled"); |
Peter Boström | 8c38e8b | 2015-11-26 16:45:47 | [diff] [blame] | 53 | thread.Start(); |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 | [diff] [blame] | 54 | |
hta@webrtc.org | e1919f4 | 2012-05-22 15:57:34 | [diff] [blame] | 55 | // At this point, the flag may be either true or false. |
Peter Boström | 8c38e8b | 2015-11-26 16:45:47 | [diff] [blame] | 56 | thread.Stop(); |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 | [diff] [blame] | 57 | |
hta@webrtc.org | e1919f4 | 2012-05-22 15:57:34 | [diff] [blame] | 58 | // We expect the thread to have run at least once. |
| 59 | EXPECT_TRUE(flag); |
hta@webrtc.org | e1919f4 | 2012-05-22 15:57:34 | [diff] [blame] | 60 | } |
tommi | 0f8b403 | 2017-02-22 19:22:05 | [diff] [blame] | 61 | |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 62 | } // namespace rtc |