blob: 3f0408aa4b3d74683dee0b2bb7124f6f8cc6e944 [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 "system_wrappers/include/sleep.h"
14#include "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
tommi0f8b4032017-02-22 11:22:05 -080019void NullRunFunction(void* obj) {}
20
hta@webrtc.orge1919f42012-05-22 15:57:34 +000021// Function that sets a boolean.
tommi0f8b4032017-02-22 11:22:05 -080022void SetFlagRunFunction(void* obj) {
23 bool* obj_as_bool = static_cast<bool*>(obj);
24 *obj_as_bool = true;
25}
26
tommi845afa82016-04-22 09:08:44 -070027} // namespace
28
29TEST(PlatformThreadTest, StartStop) {
30 PlatformThread thread(&NullRunFunction, nullptr, "PlatformThreadTest");
31 EXPECT_TRUE(thread.name() == "PlatformThreadTest");
32 EXPECT_TRUE(thread.GetThreadRef() == 0);
33 thread.Start();
34 EXPECT_TRUE(thread.GetThreadRef() != 0);
35 thread.Stop();
36 EXPECT_TRUE(thread.GetThreadRef() == 0);
37}
38
39TEST(PlatformThreadTest, StartStop2) {
40 PlatformThread thread1(&NullRunFunction, nullptr, "PlatformThreadTest1");
41 PlatformThread thread2(&NullRunFunction, nullptr, "PlatformThreadTest2");
42 EXPECT_TRUE(thread1.GetThreadRef() == thread2.GetThreadRef());
43 thread1.Start();
44 thread2.Start();
45 EXPECT_TRUE(thread1.GetThreadRef() != thread2.GetThreadRef());
46 thread2.Stop();
47 thread1.Stop();
48}
hta@webrtc.orge1919f42012-05-22 15:57:34 +000049
pbos12411ef2015-11-23 14:47:56 -080050TEST(PlatformThreadTest, RunFunctionIsCalled) {
hta@webrtc.orge1919f42012-05-22 15:57:34 +000051 bool flag = false;
tommi845afa82016-04-22 09:08:44 -070052 PlatformThread thread(&SetFlagRunFunction, &flag, "RunFunctionIsCalled");
Peter Boström8c38e8b2015-11-26 17:45:47 +010053 thread.Start();
phoglund@webrtc.orgec9c9422013-01-02 08:45:03 +000054
hta@webrtc.orge1919f42012-05-22 15:57:34 +000055 // At this point, the flag may be either true or false.
Peter Boström8c38e8b2015-11-26 17:45:47 +010056 thread.Stop();
phoglund@webrtc.orgec9c9422013-01-02 08:45:03 +000057
hta@webrtc.orge1919f42012-05-22 15:57:34 +000058 // We expect the thread to have run at least once.
59 EXPECT_TRUE(flag);
hta@webrtc.orge1919f42012-05-22 15:57:34 +000060}
tommi0f8b4032017-02-22 11:22:05 -080061
Yves Gerey665174f2018-06-19 15:03:05 +020062} // namespace rtc