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