blob: 6bbe136937e592fb6001dc571b77fbe8526aa646 [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
hta@webrtc.orge1919f42012-05-22 15:57:34 +000011#include "system_wrappers/interface/thread_wrapper.h"
12
13#include "gtest/gtest.h"
henrike@webrtc.org5ba44112012-10-05 14:36:54 +000014#include "system_wrappers/interface/scoped_ptr.h"
hta@webrtc.orge1919f42012-05-22 15:57:34 +000015#include "system_wrappers/interface/trace.h"
16
17namespace webrtc {
18
hta@webrtc.org40300132012-05-23 15:49:48 +000019const int kLogTrace = 0;
hta@webrtc.orge1919f42012-05-22 15:57:34 +000020
21class TestTraceCallback : public TraceCallback {
22 public:
23 virtual void Print(const TraceLevel level,
24 const char* traceString,
25 const int length) {
26 if (traceString) {
27 char* cmd_print = new char[length+1];
28 memcpy(cmd_print, traceString, length);
29 cmd_print[length] = '\0';
30 printf("%s\n", cmd_print);
31 fflush(stdout);
32 delete[] cmd_print;
33 }
34 }
35};
36
37class ThreadTest : public ::testing::Test {
38 public:
39 ThreadTest() {
40 StartTrace();
41 }
42 ~ThreadTest() {
43 StopTrace();
44 }
45
46 private:
47 void StartTrace() {
48 if (kLogTrace) {
49 Trace::CreateTrace();
50 Trace::SetLevelFilter(webrtc::kTraceAll);
51 Trace::SetTraceCallback(&trace_);
52 }
53 }
54
55 void StopTrace() {
56 if (kLogTrace) {
57 Trace::ReturnTrace();
58 }
59 }
60
61 TestTraceCallback trace_;
62};
63
henrike@webrtc.org5ba44112012-10-05 14:36:54 +000064TEST_F(ThreadTest, NullFunctionPointer) {
65 webrtc::scoped_ptr<ThreadWrapper> thread(
66 webrtc::ThreadWrapper::CreateThread());
67 unsigned int id = 42;
68 EXPECT_FALSE(thread->Start(id));
69}
70
hta@webrtc.orge1919f42012-05-22 15:57:34 +000071// Function that does nothing, and reports success.
72bool NullRunFunction(void* /* obj */) {
73 return true;
74}
75
76TEST_F(ThreadTest, StartStop) {
77 ThreadWrapper* thread = ThreadWrapper::CreateThread(&NullRunFunction);
78 unsigned int id = 42;
79 ASSERT_TRUE(thread->Start(id));
80 EXPECT_TRUE(thread->Stop());
hta@webrtc.org6ed617b2012-05-22 16:55:16 +000081 delete thread;
hta@webrtc.orge1919f42012-05-22 15:57:34 +000082}
83
84// Function that sets a boolean.
85bool SetFlagRunFunction(void* obj) {
86 bool* obj_as_bool = static_cast<bool*> (obj);
87 *obj_as_bool = true;
88 return true;
89}
90
91TEST_F(ThreadTest, RunFunctionIsCalled) {
92 bool flag = false;
93 ThreadWrapper* thread = ThreadWrapper::CreateThread(&SetFlagRunFunction,
94 &flag);
95 unsigned int id = 42;
96 ASSERT_TRUE(thread->Start(id));
97 // At this point, the flag may be either true or false.
98 EXPECT_TRUE(thread->Stop());
99 // We expect the thread to have run at least once.
100 EXPECT_TRUE(flag);
hta@webrtc.org6ed617b2012-05-22 16:55:16 +0000101 delete thread;
hta@webrtc.orge1919f42012-05-22 15:57:34 +0000102}
103
104} // namespace webrtc