blob: 3f0408aa4b3d74683dee0b2bb7124f6f8cc6e944 [file] [log] [blame]
hta@webrtc.orge1919f42012-05-22 15:57:341/*
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:4810
Mirko Bonadei92ea95e2017-09-15 04:47:3111#include "rtc_base/platform_thread.h"
hta@webrtc.orge1919f42012-05-22 15:57:3412
Mirko Bonadei92ea95e2017-09-15 04:47:3113#include "system_wrappers/include/sleep.h"
14#include "test/gtest.h"
hta@webrtc.orge1919f42012-05-22 15:57:3415
tommi845afa82016-04-22 16:08:4416namespace rtc {
17namespace {
hta@webrtc.orge1919f42012-05-22 15:57:3418
tommi0f8b4032017-02-22 19:22:0519void NullRunFunction(void* obj) {}
20
hta@webrtc.orge1919f42012-05-22 15:57:3421// Function that sets a boolean.
tommi0f8b4032017-02-22 19:22:0522void SetFlagRunFunction(void* obj) {
23 bool* obj_as_bool = static_cast<bool*>(obj);
24 *obj_as_bool = true;
25}
26
tommi845afa82016-04-22 16:08:4427} // 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:3449
pbos12411ef2015-11-23 22:47:5650TEST(PlatformThreadTest, RunFunctionIsCalled) {
hta@webrtc.orge1919f42012-05-22 15:57:3451 bool flag = false;
tommi845afa82016-04-22 16:08:4452 PlatformThread thread(&SetFlagRunFunction, &flag, "RunFunctionIsCalled");
Peter Boström8c38e8b2015-11-26 16:45:4753 thread.Start();
phoglund@webrtc.orgec9c9422013-01-02 08:45:0354
hta@webrtc.orge1919f42012-05-22 15:57:3455 // At this point, the flag may be either true or false.
Peter Boström8c38e8b2015-11-26 16:45:4756 thread.Stop();
phoglund@webrtc.orgec9c9422013-01-02 08:45:0357
hta@webrtc.orge1919f42012-05-22 15:57:3458 // We expect the thread to have run at least once.
59 EXPECT_TRUE(flag);
hta@webrtc.orge1919f42012-05-22 15:57:3460}
tommi0f8b4032017-02-22 19:22:0561
Yves Gerey665174f2018-06-19 13:03:0562} // namespace rtc