blob: a52e4cd9f5ec3f0d875056d754cb7f8ef3c2c3ef [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "rtc_base/platform_thread.h"
hta@webrtc.orge1919f42012-05-22 15:57:34 +000012
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020013#include "test/gtest.h"
hta@webrtc.orge1919f42012-05-22 15:57:34 +000014
tommi845afa82016-04-22 09:08:44 -070015namespace rtc {
16namespace {
hta@webrtc.orge1919f42012-05-22 15:57:34 +000017
tommi0f8b4032017-02-22 11:22:05 -080018void NullRunFunction(void* obj) {}
19
hta@webrtc.orge1919f42012-05-22 15:57:34 +000020// Function that sets a boolean.
tommi0f8b4032017-02-22 11:22:05 -080021void SetFlagRunFunction(void* obj) {
22 bool* obj_as_bool = static_cast<bool*>(obj);
23 *obj_as_bool = true;
24}
25
tommi845afa82016-04-22 09:08:44 -070026} // namespace
27
28TEST(PlatformThreadTest, StartStop) {
29 PlatformThread thread(&NullRunFunction, nullptr, "PlatformThreadTest");
30 EXPECT_TRUE(thread.name() == "PlatformThreadTest");
31 EXPECT_TRUE(thread.GetThreadRef() == 0);
32 thread.Start();
33 EXPECT_TRUE(thread.GetThreadRef() != 0);
34 thread.Stop();
35 EXPECT_TRUE(thread.GetThreadRef() == 0);
36}
37
38TEST(PlatformThreadTest, StartStop2) {
39 PlatformThread thread1(&NullRunFunction, nullptr, "PlatformThreadTest1");
40 PlatformThread thread2(&NullRunFunction, nullptr, "PlatformThreadTest2");
41 EXPECT_TRUE(thread1.GetThreadRef() == thread2.GetThreadRef());
42 thread1.Start();
43 thread2.Start();
44 EXPECT_TRUE(thread1.GetThreadRef() != thread2.GetThreadRef());
45 thread2.Stop();
46 thread1.Stop();
47}
hta@webrtc.orge1919f42012-05-22 15:57:34 +000048
pbos12411ef2015-11-23 14:47:56 -080049TEST(PlatformThreadTest, RunFunctionIsCalled) {
hta@webrtc.orge1919f42012-05-22 15:57:34 +000050 bool flag = false;
tommi845afa82016-04-22 09:08:44 -070051 PlatformThread thread(&SetFlagRunFunction, &flag, "RunFunctionIsCalled");
Peter Boström8c38e8b2015-11-26 17:45:47 +010052 thread.Start();
phoglund@webrtc.orgec9c9422013-01-02 08:45:03 +000053
hta@webrtc.orge1919f42012-05-22 15:57:34 +000054 // At this point, the flag may be either true or false.
Peter Boström8c38e8b2015-11-26 17:45:47 +010055 thread.Stop();
phoglund@webrtc.orgec9c9422013-01-02 08:45:03 +000056
hta@webrtc.orge1919f42012-05-22 15:57:34 +000057 // We expect the thread to have run at least once.
58 EXPECT_TRUE(flag);
hta@webrtc.orge1919f42012-05-22 15:57:34 +000059}
tommi0f8b4032017-02-22 11:22:05 -080060
Yves Gerey665174f2018-06-19 15:03:05 +020061} // namespace rtc