• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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  */
10 
11 #include "rtc_base/platform_thread.h"
12 
13 #include "system_wrappers/include/sleep.h"
14 #include "test/gtest.h"
15 
16 namespace rtc {
17 namespace {
18 
NullRunFunction(void * obj)19 void NullRunFunction(void* obj) {}
20 
21 // Function that sets a boolean.
SetFlagRunFunction(void * obj)22 void SetFlagRunFunction(void* obj) {
23   bool* obj_as_bool = static_cast<bool*>(obj);
24   *obj_as_bool = true;
25 }
26 
27 }  // namespace
28 
TEST(PlatformThreadTest,StartStop)29 TEST(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 
TEST(PlatformThreadTest,StartStop2)39 TEST(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 }
49 
TEST(PlatformThreadTest,RunFunctionIsCalled)50 TEST(PlatformThreadTest, RunFunctionIsCalled) {
51   bool flag = false;
52   PlatformThread thread(&SetFlagRunFunction, &flag, "RunFunctionIsCalled");
53   thread.Start();
54 
55   // At this point, the flag may be either true or false.
56   thread.Stop();
57 
58   // We expect the thread to have run at least once.
59   EXPECT_TRUE(flag);
60 }
61 
62 }  // namespace rtc
63