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