hta@webrtc.org | e1919f4 | 2012-05-22 15:57:34 +0000 | [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 +0000 | [diff] [blame] | 10 | |
pbos | 12411ef | 2015-11-23 14:47:56 -0800 | [diff] [blame] | 11 | #include "webrtc/base/platform_thread.h" |
hta@webrtc.org | e1919f4 | 2012-05-22 15:57:34 +0000 | [diff] [blame] | 12 | |
pbos@webrtc.org | acaf3a1 | 2013-05-27 15:07:45 +0000 | [diff] [blame] | 13 | #include "testing/gtest/include/gtest/gtest.h" |
kwiberg@webrtc.org | 00b8f6b | 2015-02-26 14:34:55 +0000 | [diff] [blame] | 14 | #include "webrtc/base/scoped_ptr.h" |
Henrik Kjellander | 98f5351 | 2015-10-28 18:17:40 +0100 | [diff] [blame] | 15 | #include "webrtc/system_wrappers/include/sleep.h" |
hta@webrtc.org | e1919f4 | 2012-05-22 15:57:34 +0000 | [diff] [blame] | 16 | |
| 17 | namespace webrtc { |
| 18 | |
hta@webrtc.org | e1919f4 | 2012-05-22 15:57:34 +0000 | [diff] [blame] | 19 | // Function that does nothing, and reports success. |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame] | 20 | bool NullRunFunction(void* obj) { |
pbos@webrtc.org | e9e4253 | 2014-07-18 10:12:50 +0000 | [diff] [blame] | 21 | SleepMs(0); // Hand over timeslice, prevents busy looping. |
hta@webrtc.org | e1919f4 | 2012-05-22 15:57:34 +0000 | [diff] [blame] | 22 | return true; |
| 23 | } |
| 24 | |
pbos | 12411ef | 2015-11-23 14:47:56 -0800 | [diff] [blame] | 25 | TEST(PlatformThreadTest, StartStop) { |
Peter Boström | 8c38e8b | 2015-11-26 17:45:47 +0100 | [diff] [blame] | 26 | rtc::PlatformThread thread(&NullRunFunction, nullptr, "PlatformThreadTest"); |
| 27 | thread.Start(); |
| 28 | thread.Stop(); |
hta@webrtc.org | e1919f4 | 2012-05-22 15:57:34 +0000 | [diff] [blame] | 29 | } |
| 30 | |
| 31 | // Function that sets a boolean. |
| 32 | bool SetFlagRunFunction(void* obj) { |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame] | 33 | bool* obj_as_bool = static_cast<bool*>(obj); |
hta@webrtc.org | e1919f4 | 2012-05-22 15:57:34 +0000 | [diff] [blame] | 34 | *obj_as_bool = true; |
pbos@webrtc.org | e9e4253 | 2014-07-18 10:12:50 +0000 | [diff] [blame] | 35 | SleepMs(0); // Hand over timeslice, prevents busy looping. |
hta@webrtc.org | e1919f4 | 2012-05-22 15:57:34 +0000 | [diff] [blame] | 36 | return true; |
| 37 | } |
| 38 | |
pbos | 12411ef | 2015-11-23 14:47:56 -0800 | [diff] [blame] | 39 | TEST(PlatformThreadTest, RunFunctionIsCalled) { |
hta@webrtc.org | e1919f4 | 2012-05-22 15:57:34 +0000 | [diff] [blame] | 40 | bool flag = false; |
Peter Boström | 8c38e8b | 2015-11-26 17:45:47 +0100 | [diff] [blame] | 41 | rtc::PlatformThread thread(&SetFlagRunFunction, &flag, "RunFunctionIsCalled"); |
| 42 | thread.Start(); |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame] | 43 | |
hta@webrtc.org | e1919f4 | 2012-05-22 15:57:34 +0000 | [diff] [blame] | 44 | // At this point, the flag may be either true or false. |
Peter Boström | 8c38e8b | 2015-11-26 17:45:47 +0100 | [diff] [blame] | 45 | thread.Stop(); |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame] | 46 | |
hta@webrtc.org | e1919f4 | 2012-05-22 15:57:34 +0000 | [diff] [blame] | 47 | // We expect the thread to have run at least once. |
| 48 | EXPECT_TRUE(flag); |
hta@webrtc.org | e1919f4 | 2012-05-22 15:57:34 +0000 | [diff] [blame] | 49 | } |
| 50 | |
hta@webrtc.org | e1919f4 | 2012-05-22 15:57:34 +0000 | [diff] [blame] | 51 | } // namespace webrtc |