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.
5 // Tests of CancellationFlag class.
7 #include "base/synchronization/cancellation_flag.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"
22 //------------------------------------------------------------------------------
23 // Define our test class.
24 //------------------------------------------------------------------------------
26 void CancelHelper(CancellationFlag
* flag
) {
27 #if GTEST_HAS_DEATH_TEST
28 ASSERT_DEBUG_DEATH(flag
->Set(), "");
32 TEST(CancellationFlagTest
, SimpleSingleThreadedTest
) {
33 CancellationFlag flag
;
34 ASSERT_FALSE(flag
.IsSet());
36 ASSERT_TRUE(flag
.IsSet());
39 TEST(CancellationFlagTest
, DoubleSetTest
) {
40 CancellationFlag flag
;
41 ASSERT_FALSE(flag
.IsSet());
43 ASSERT_TRUE(flag
.IsSet());
45 ASSERT_TRUE(flag
.IsSet());
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
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());
58 CancellationFlag flag
;
59 t
.message_loop()->PostTask(FROM_HERE
, base::Bind(&CancelHelper
, &flag
));