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