Home | History | Annotate | Download | only in base
      1 // Copyright (c) 2009 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 // Tests of CancellationFlag class.
      6 
      7 #include "base/cancellation_flag.h"
      8 
      9 #include "base/logging.h"
     10 #include "base/spin_wait.h"
     11 #include "base/time.h"
     12 #include "base/thread.h"
     13 #include "testing/gtest/include/gtest/gtest.h"
     14 #include "testing/platform_test.h"
     15 
     16 using base::CancellationFlag;
     17 using base::TimeDelta;
     18 using base::Thread;
     19 
     20 namespace {
     21 
     22 //------------------------------------------------------------------------------
     23 // Define our test class.
     24 //------------------------------------------------------------------------------
     25 
     26 class CancelTask : public Task {
     27  public:
     28   explicit CancelTask(CancellationFlag* flag) : flag_(flag) {}
     29   virtual void Run() {
     30     ASSERT_DEBUG_DEATH(flag_->Set(), "");
     31   }
     32  private:
     33   CancellationFlag* flag_;
     34 };
     35 
     36 TEST(CancellationFlagTest, SimpleSingleThreadedTest) {
     37   CancellationFlag flag;
     38   ASSERT_FALSE(flag.IsSet());
     39   flag.Set();
     40   ASSERT_TRUE(flag.IsSet());
     41 }
     42 
     43 TEST(CancellationFlagTest, DoubleSetTest) {
     44   CancellationFlag flag;
     45   ASSERT_FALSE(flag.IsSet());
     46   flag.Set();
     47   ASSERT_TRUE(flag.IsSet());
     48   flag.Set();
     49   ASSERT_TRUE(flag.IsSet());
     50 }
     51 
     52 TEST(CancellationFlagTest, SetOnDifferentThreadDeathTest) {
     53   // Checks that Set() can't be called from any other thread.
     54   // CancellationFlag should die on a DCHECK if Set() is called from
     55   // other thread.
     56   ::testing::FLAGS_gtest_death_test_style = "threadsafe";
     57   Thread t("CancellationFlagTest.SetOnDifferentThreadDeathTest");
     58   ASSERT_TRUE(t.Start());
     59   ASSERT_TRUE(t.message_loop());
     60   ASSERT_TRUE(t.IsRunning());
     61 
     62   CancellationFlag flag;
     63   t.message_loop()->PostTask(FROM_HERE, new CancelTask(&flag));
     64 }
     65 
     66 }  // namespace
     67