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 | |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame] | 11 | #include "webrtc/system_wrappers/interface/thread_wrapper.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" |
pbos@webrtc.org | e9e4253 | 2014-07-18 10:12:50 +0000 | [diff] [blame] | 15 | #include "webrtc/system_wrappers/interface/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 | |
andrew@webrtc.org | 23ec30b | 2012-11-15 05:33:25 +0000 | [diff] [blame] | 25 | TEST(ThreadTest, StartStop) { |
tommi@webrtc.org | 361981f | 2015-03-19 14:44:18 +0000 | [diff] [blame] | 26 | rtc::scoped_ptr<ThreadWrapper> thread = ThreadWrapper::CreateThread( |
tommi@webrtc.org | 38492c5 | 2015-03-22 14:41:46 +0000 | [diff] [blame] | 27 | &NullRunFunction, nullptr, "ThreadTest"); |
pbos@webrtc.org | 8663973 | 2015-03-13 00:06:21 +0000 | [diff] [blame] | 28 | ASSERT_TRUE(thread->Start()); |
hta@webrtc.org | e1919f4 | 2012-05-22 15:57:34 +0000 | [diff] [blame] | 29 | EXPECT_TRUE(thread->Stop()); |
| 30 | } |
| 31 | |
| 32 | // Function that sets a boolean. |
| 33 | bool SetFlagRunFunction(void* obj) { |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame] | 34 | bool* obj_as_bool = static_cast<bool*>(obj); |
hta@webrtc.org | e1919f4 | 2012-05-22 15:57:34 +0000 | [diff] [blame] | 35 | *obj_as_bool = true; |
pbos@webrtc.org | e9e4253 | 2014-07-18 10:12:50 +0000 | [diff] [blame] | 36 | SleepMs(0); // Hand over timeslice, prevents busy looping. |
hta@webrtc.org | e1919f4 | 2012-05-22 15:57:34 +0000 | [diff] [blame] | 37 | return true; |
| 38 | } |
| 39 | |
andrew@webrtc.org | 23ec30b | 2012-11-15 05:33:25 +0000 | [diff] [blame] | 40 | TEST(ThreadTest, RunFunctionIsCalled) { |
hta@webrtc.org | e1919f4 | 2012-05-22 15:57:34 +0000 | [diff] [blame] | 41 | bool flag = false; |
tommi@webrtc.org | 361981f | 2015-03-19 14:44:18 +0000 | [diff] [blame] | 42 | rtc::scoped_ptr<ThreadWrapper> thread = ThreadWrapper::CreateThread( |
tommi@webrtc.org | 38492c5 | 2015-03-22 14:41:46 +0000 | [diff] [blame] | 43 | &SetFlagRunFunction, &flag, "RunFunctionIsCalled"); |
pbos@webrtc.org | 8663973 | 2015-03-13 00:06:21 +0000 | [diff] [blame] | 44 | ASSERT_TRUE(thread->Start()); |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame] | 45 | |
hta@webrtc.org | e1919f4 | 2012-05-22 15:57:34 +0000 | [diff] [blame] | 46 | // At this point, the flag may be either true or false. |
| 47 | EXPECT_TRUE(thread->Stop()); |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame] | 48 | |
hta@webrtc.org | e1919f4 | 2012-05-22 15:57:34 +0000 | [diff] [blame] | 49 | // We expect the thread to have run at least once. |
| 50 | EXPECT_TRUE(flag); |
hta@webrtc.org | e1919f4 | 2012-05-22 15:57:34 +0000 | [diff] [blame] | 51 | } |
| 52 | |
hta@webrtc.org | e1919f4 | 2012-05-22 15:57:34 +0000 | [diff] [blame] | 53 | } // namespace webrtc |