Home | History | Annotate | Download | only in base
      1 /*
      2  * libjingle
      3  * Copyright 2004--2011, Google Inc.
      4  *
      5  * Redistribution and use in source and binary forms, with or without
      6  * modification, are permitted provided that the following conditions are met:
      7  *
      8  *  1. Redistributions of source code must retain the above copyright notice,
      9  *     this list of conditions and the following disclaimer.
     10  *  2. Redistributions in binary form must reproduce the above copyright notice,
     11  *     this list of conditions and the following disclaimer in the documentation
     12  *     and/or other materials provided with the distribution.
     13  *  3. The name of the author may not be used to endorse or promote products
     14  *     derived from this software without specific prior written permission.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
     17  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
     18  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
     19  * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     20  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     21  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
     22  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     23  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
     24  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
     25  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     26  */
     27 
     28 #include "talk/base/gunit.h"
     29 #include "talk/base/signalthread.h"
     30 #include "talk/base/thread.h"
     31 
     32 using namespace talk_base;
     33 
     34 class SignalThreadTest : public testing::Test, public sigslot::has_slots<> {
     35  public:
     36   class SlowSignalThread : public SignalThread {
     37    public:
     38     SlowSignalThread(SignalThreadTest* harness) : harness_(harness) {
     39     }
     40 
     41     virtual ~SlowSignalThread() {
     42       EXPECT_EQ(harness_->main_thread_, Thread::Current());
     43       ++harness_->thread_deleted_;
     44     }
     45 
     46     const SignalThreadTest* harness() { return harness_; }
     47 
     48    protected:
     49     virtual void OnWorkStart() {
     50       ASSERT_TRUE(harness_ != NULL);
     51       ++harness_->thread_started_;
     52       EXPECT_EQ(harness_->main_thread_, Thread::Current());
     53       EXPECT_FALSE(worker()->started());  // not started yet
     54     }
     55 
     56     virtual void OnWorkStop() {
     57       ++harness_->thread_stopped_;
     58       EXPECT_EQ(harness_->main_thread_, Thread::Current());
     59       EXPECT_TRUE(worker()->started());  // not stopped yet
     60     }
     61 
     62     virtual void OnWorkDone() {
     63       ++harness_->thread_done_;
     64       EXPECT_EQ(harness_->main_thread_, Thread::Current());
     65       EXPECT_TRUE(worker()->started());  // not stopped yet
     66     }
     67 
     68     virtual void DoWork() {
     69       EXPECT_NE(harness_->main_thread_, Thread::Current());
     70       EXPECT_EQ(worker(), Thread::Current());
     71       Thread::Current()->socketserver()->Wait(250, false);
     72     }
     73 
     74    private:
     75     SignalThreadTest* harness_;
     76     DISALLOW_EVIL_CONSTRUCTORS(SlowSignalThread);
     77   };
     78 
     79   void OnWorkComplete(talk_base::SignalThread* thread) {
     80     SlowSignalThread* t = static_cast<SlowSignalThread*>(thread);
     81     EXPECT_EQ(t->harness(), this);
     82     EXPECT_EQ(main_thread_, Thread::Current());
     83 
     84     ++thread_completed_;
     85     if (!called_release_) {
     86       thread->Release();
     87     }
     88   }
     89 
     90   virtual void SetUp() {
     91     main_thread_ = Thread::Current();
     92     thread_ = new SlowSignalThread(this);
     93     thread_->SignalWorkDone.connect(this, &SignalThreadTest::OnWorkComplete);
     94     called_release_ = false;
     95     thread_started_ = 0;
     96     thread_done_ = 0;
     97     thread_completed_ = 0;
     98     thread_stopped_ = 0;
     99     thread_deleted_ = 0;
    100   }
    101 
    102   virtual void TearDown() {
    103   }
    104 
    105   Thread* main_thread_;
    106   SlowSignalThread* thread_;
    107   bool called_release_;
    108 
    109   int thread_started_;
    110   int thread_done_;
    111   int thread_completed_;
    112   int thread_stopped_;
    113   int thread_deleted_;
    114 };
    115 
    116 class OwnerThread : public Thread, public sigslot::has_slots<> {
    117  public:
    118   explicit OwnerThread(SignalThreadTest* harness)
    119       : harness_(harness),
    120         has_run_(false) {
    121   }
    122 
    123   virtual void Run() {
    124     SignalThreadTest::SlowSignalThread* signal_thread =
    125         new SignalThreadTest::SlowSignalThread(harness_);
    126     signal_thread->SignalWorkDone.connect(this, &OwnerThread::OnWorkDone);
    127     signal_thread->Start();
    128     Thread::Current()->socketserver()->Wait(100, false);
    129     signal_thread->Release();
    130     // Delete |signal_thread|.
    131     signal_thread->Destroy(true);
    132     has_run_ = true;
    133   }
    134 
    135   bool has_run() { return has_run_; }
    136   void OnWorkDone(SignalThread* signal_thread) {
    137     FAIL() << " This shouldn't get called.";
    138   }
    139 
    140  private:
    141   SignalThreadTest* harness_;
    142   bool has_run_;
    143   DISALLOW_EVIL_CONSTRUCTORS(OwnerThread);
    144 };
    145 
    146 // Test for when the main thread goes away while the
    147 // signal thread is still working.  This may happen
    148 // when shutting down the process.
    149 TEST_F(SignalThreadTest, OwnerThreadGoesAway) {
    150   {
    151     scoped_ptr<OwnerThread> owner(new OwnerThread(this));
    152     main_thread_ = owner.get();
    153     owner->Start();
    154     while (!owner->has_run()) {
    155       Thread::Current()->socketserver()->Wait(10, false);
    156     }
    157   }
    158   // At this point the main thread has gone away.
    159   // Give the SignalThread a little time to do its callback,
    160   // which will crash if the signal thread doesn't handle
    161   // this situation well.
    162   Thread::Current()->socketserver()->Wait(500, false);
    163 }
    164 
    165 #define EXPECT_STATE(started, done, completed, stopped, deleted) \
    166   EXPECT_EQ(started, thread_started_); \
    167   EXPECT_EQ(done, thread_done_); \
    168   EXPECT_EQ(completed, thread_completed_); \
    169   EXPECT_EQ(stopped, thread_stopped_); \
    170   EXPECT_EQ(deleted, thread_deleted_);
    171 
    172 TEST_F(SignalThreadTest, ThreadFinishes) {
    173   thread_->Start();
    174   EXPECT_STATE(1, 0, 0, 0, 0);
    175   Thread::SleepMs(500);
    176   EXPECT_STATE(1, 0, 0, 0, 0);
    177   Thread::Current()->ProcessMessages(0);
    178   EXPECT_STATE(1, 1, 1, 0, 1);
    179 }
    180 
    181 TEST_F(SignalThreadTest, ReleasedThreadFinishes) {
    182   thread_->Start();
    183   EXPECT_STATE(1, 0, 0, 0, 0);
    184   thread_->Release();
    185   called_release_ = true;
    186   EXPECT_STATE(1, 0, 0, 0, 0);
    187   Thread::SleepMs(500);
    188   EXPECT_STATE(1, 0, 0, 0, 0);
    189   Thread::Current()->ProcessMessages(0);
    190   EXPECT_STATE(1, 1, 1, 0, 1);
    191 }
    192 
    193 TEST_F(SignalThreadTest, DestroyedThreadCleansUp) {
    194   thread_->Start();
    195   EXPECT_STATE(1, 0, 0, 0, 0);
    196   thread_->Destroy(true);
    197   EXPECT_STATE(1, 0, 0, 1, 1);
    198   Thread::Current()->ProcessMessages(0);
    199   EXPECT_STATE(1, 0, 0, 1, 1);
    200 }
    201 
    202 TEST_F(SignalThreadTest, DeferredDestroyedThreadCleansUp) {
    203   thread_->Start();
    204   EXPECT_STATE(1, 0, 0, 0, 0);
    205   thread_->Destroy(false);
    206   EXPECT_STATE(1, 0, 0, 1, 0);
    207   Thread::SleepMs(500);
    208   EXPECT_STATE(1, 0, 0, 1, 0);
    209   Thread::Current()->ProcessMessages(0);
    210   EXPECT_STATE(1, 1, 0, 1, 1);
    211 }
    212