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