blob: 322eb35ffcb747e7f12e84244926d720a26be36f [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
Henrik Kjellander98f53512015-10-28 18:17:40 +010013#include "webrtc/system_wrappers/include/sleep.h"
kwibergac9f8762016-09-30 22:29:43 -070014#include "webrtc/test/gtest.h"
hta@webrtc.orge1919f42012-05-22 15:57:34 +000015
tommi845afa82016-04-22 09:08:44 -070016namespace rtc {
17namespace {
hta@webrtc.orge1919f42012-05-22 15:57:34 +000018// Function that does nothing, and reports success.
phoglund@webrtc.orgec9c9422013-01-02 08:45:03 +000019bool NullRunFunction(void* obj) {
tommi845afa82016-04-22 09:08:44 -070020 webrtc::SleepMs(0); // Hand over timeslice, prevents busy looping.
hta@webrtc.orge1919f42012-05-22 15:57:34 +000021 return true;
22}
23
hta@webrtc.orge1919f42012-05-22 15:57:34 +000024// Function that sets a boolean.
25bool SetFlagRunFunction(void* obj) {
phoglund@webrtc.orgec9c9422013-01-02 08:45:03 +000026 bool* obj_as_bool = static_cast<bool*>(obj);
hta@webrtc.orge1919f42012-05-22 15:57:34 +000027 *obj_as_bool = true;
tommi845afa82016-04-22 09:08:44 -070028 webrtc::SleepMs(0); // Hand over timeslice, prevents busy looping.
hta@webrtc.orge1919f42012-05-22 15:57:34 +000029 return true;
30}
tommi845afa82016-04-22 09:08:44 -070031} // namespace
32
33TEST(PlatformThreadTest, StartStop) {
34 PlatformThread thread(&NullRunFunction, nullptr, "PlatformThreadTest");
35 EXPECT_TRUE(thread.name() == "PlatformThreadTest");
36 EXPECT_TRUE(thread.GetThreadRef() == 0);
37 thread.Start();
38 EXPECT_TRUE(thread.GetThreadRef() != 0);
39 thread.Stop();
40 EXPECT_TRUE(thread.GetThreadRef() == 0);
41}
42
43TEST(PlatformThreadTest, StartStop2) {
44 PlatformThread thread1(&NullRunFunction, nullptr, "PlatformThreadTest1");
45 PlatformThread thread2(&NullRunFunction, nullptr, "PlatformThreadTest2");
46 EXPECT_TRUE(thread1.GetThreadRef() == thread2.GetThreadRef());
47 thread1.Start();
48 thread2.Start();
49 EXPECT_TRUE(thread1.GetThreadRef() != thread2.GetThreadRef());
50 thread2.Stop();
51 thread1.Stop();
52}
hta@webrtc.orge1919f42012-05-22 15:57:34 +000053
pbos12411ef2015-11-23 14:47:56 -080054TEST(PlatformThreadTest, RunFunctionIsCalled) {
hta@webrtc.orge1919f42012-05-22 15:57:34 +000055 bool flag = false;
tommi845afa82016-04-22 09:08:44 -070056 PlatformThread thread(&SetFlagRunFunction, &flag, "RunFunctionIsCalled");
Peter Boström8c38e8b2015-11-26 17:45:47 +010057 thread.Start();
phoglund@webrtc.orgec9c9422013-01-02 08:45:03 +000058
hta@webrtc.orge1919f42012-05-22 15:57:34 +000059 // At this point, the flag may be either true or false.
Peter Boström8c38e8b2015-11-26 17:45:47 +010060 thread.Stop();
phoglund@webrtc.orgec9c9422013-01-02 08:45:03 +000061
hta@webrtc.orge1919f42012-05-22 15:57:34 +000062 // We expect the thread to have run at least once.
63 EXPECT_TRUE(flag);
hta@webrtc.orge1919f42012-05-22 15:57:34 +000064}
tommi845afa82016-04-22 09:08:44 -070065} // rtc