blob: ffb60b53243b6f30b9540bfe7171c5aaa32c0a19 [file] [log] [blame]
hta@webrtc.orge1919f42012-05-22 15:57:34 +00001/*
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.org40300132012-05-23 15:49:48 +000010
pbos12411ef2015-11-23 14:47:56 -080011#include "webrtc/base/platform_thread.h"
hta@webrtc.orge1919f42012-05-22 15:57:34 +000012
pbos@webrtc.orgacaf3a12013-05-27 15:07:45 +000013#include "testing/gtest/include/gtest/gtest.h"
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +000014#include "webrtc/base/scoped_ptr.h"
Henrik Kjellander98f53512015-10-28 18:17:40 +010015#include "webrtc/system_wrappers/include/sleep.h"
hta@webrtc.orge1919f42012-05-22 15:57:34 +000016
17namespace webrtc {
18
hta@webrtc.orge1919f42012-05-22 15:57:34 +000019// Function that does nothing, and reports success.
phoglund@webrtc.orgec9c9422013-01-02 08:45:03 +000020bool NullRunFunction(void* obj) {
pbos@webrtc.orge9e42532014-07-18 10:12:50 +000021 SleepMs(0); // Hand over timeslice, prevents busy looping.
hta@webrtc.orge1919f42012-05-22 15:57:34 +000022 return true;
23}
24
pbos12411ef2015-11-23 14:47:56 -080025TEST(PlatformThreadTest, StartStop) {
26 rtc::scoped_ptr<PlatformThread> thread = PlatformThread::CreateThread(
27 &NullRunFunction, nullptr, "PlatformThreadTest");
pbos@webrtc.org86639732015-03-13 00:06:21 +000028 ASSERT_TRUE(thread->Start());
hta@webrtc.orge1919f42012-05-22 15:57:34 +000029 EXPECT_TRUE(thread->Stop());
30}
31
32// Function that sets a boolean.
33bool SetFlagRunFunction(void* obj) {
phoglund@webrtc.orgec9c9422013-01-02 08:45:03 +000034 bool* obj_as_bool = static_cast<bool*>(obj);
hta@webrtc.orge1919f42012-05-22 15:57:34 +000035 *obj_as_bool = true;
pbos@webrtc.orge9e42532014-07-18 10:12:50 +000036 SleepMs(0); // Hand over timeslice, prevents busy looping.
hta@webrtc.orge1919f42012-05-22 15:57:34 +000037 return true;
38}
39
pbos12411ef2015-11-23 14:47:56 -080040TEST(PlatformThreadTest, RunFunctionIsCalled) {
hta@webrtc.orge1919f42012-05-22 15:57:34 +000041 bool flag = false;
pbos12411ef2015-11-23 14:47:56 -080042 rtc::scoped_ptr<PlatformThread> thread = PlatformThread::CreateThread(
tommi@webrtc.org38492c52015-03-22 14:41:46 +000043 &SetFlagRunFunction, &flag, "RunFunctionIsCalled");
pbos@webrtc.org86639732015-03-13 00:06:21 +000044 ASSERT_TRUE(thread->Start());
phoglund@webrtc.orgec9c9422013-01-02 08:45:03 +000045
hta@webrtc.orge1919f42012-05-22 15:57:34 +000046 // At this point, the flag may be either true or false.
47 EXPECT_TRUE(thread->Stop());
phoglund@webrtc.orgec9c9422013-01-02 08:45:03 +000048
hta@webrtc.orge1919f42012-05-22 15:57:34 +000049 // We expect the thread to have run at least once.
50 EXPECT_TRUE(flag);
hta@webrtc.orge1919f42012-05-22 15:57:34 +000051}
52
hta@webrtc.orge1919f42012-05-22 15:57:34 +000053} // namespace webrtc