blob: d06a738a337a4af09fb102ca34264f967e832c2e [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
tommi845afa82016-04-22 09:08:44 -070017namespace rtc {
18namespace {
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) {
tommi845afa82016-04-22 09:08:44 -070021 webrtc::SleepMs(0); // Hand over timeslice, prevents busy looping.
hta@webrtc.orge1919f42012-05-22 15:57:34 +000022 return true;
23}
24
hta@webrtc.orge1919f42012-05-22 15:57:34 +000025// Function that sets a boolean.
26bool SetFlagRunFunction(void* obj) {
phoglund@webrtc.orgec9c9422013-01-02 08:45:03 +000027 bool* obj_as_bool = static_cast<bool*>(obj);
hta@webrtc.orge1919f42012-05-22 15:57:34 +000028 *obj_as_bool = true;
tommi845afa82016-04-22 09:08:44 -070029 webrtc::SleepMs(0); // Hand over timeslice, prevents busy looping.
hta@webrtc.orge1919f42012-05-22 15:57:34 +000030 return true;
31}
tommi845afa82016-04-22 09:08:44 -070032} // namespace
33
34TEST(PlatformThreadTest, StartStop) {
35 PlatformThread thread(&NullRunFunction, nullptr, "PlatformThreadTest");
36 EXPECT_TRUE(thread.name() == "PlatformThreadTest");
37 EXPECT_TRUE(thread.GetThreadRef() == 0);
38 thread.Start();
39 EXPECT_TRUE(thread.GetThreadRef() != 0);
40 thread.Stop();
41 EXPECT_TRUE(thread.GetThreadRef() == 0);
42}
43
44TEST(PlatformThreadTest, StartStop2) {
45 PlatformThread thread1(&NullRunFunction, nullptr, "PlatformThreadTest1");
46 PlatformThread thread2(&NullRunFunction, nullptr, "PlatformThreadTest2");
47 EXPECT_TRUE(thread1.GetThreadRef() == thread2.GetThreadRef());
48 thread1.Start();
49 thread2.Start();
50 EXPECT_TRUE(thread1.GetThreadRef() != thread2.GetThreadRef());
51 thread2.Stop();
52 thread1.Stop();
53}
hta@webrtc.orge1919f42012-05-22 15:57:34 +000054
pbos12411ef2015-11-23 14:47:56 -080055TEST(PlatformThreadTest, RunFunctionIsCalled) {
hta@webrtc.orge1919f42012-05-22 15:57:34 +000056 bool flag = false;
tommi845afa82016-04-22 09:08:44 -070057 PlatformThread thread(&SetFlagRunFunction, &flag, "RunFunctionIsCalled");
Peter Boström8c38e8b2015-11-26 17:45:47 +010058 thread.Start();
phoglund@webrtc.orgec9c9422013-01-02 08:45:03 +000059
hta@webrtc.orge1919f42012-05-22 15:57:34 +000060 // At this point, the flag may be either true or false.
Peter Boström8c38e8b2015-11-26 17:45:47 +010061 thread.Stop();
phoglund@webrtc.orgec9c9422013-01-02 08:45:03 +000062
hta@webrtc.orge1919f42012-05-22 15:57:34 +000063 // We expect the thread to have run at least once.
64 EXPECT_TRUE(flag);
hta@webrtc.orge1919f42012-05-22 15:57:34 +000065}
tommi845afa82016-04-22 09:08:44 -070066} // rtc