1 // Copyright (c) 2010 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/cancellation_flag.h"
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/thread.h"
14 #include "testing/gtest/include/gtest/gtest.h"
15 #include "testing/platform_test.h"
17 using base::CancellationFlag
;
18 using base::TimeDelta
;
23 //------------------------------------------------------------------------------
24 // Define our test class.
25 //------------------------------------------------------------------------------
27 class CancelTask
: public Task
{
29 explicit CancelTask(CancellationFlag
* flag
) : flag_(flag
) {}
31 ASSERT_DEBUG_DEATH(flag_
->Set(), "");
34 CancellationFlag
* flag_
;
37 TEST(CancellationFlagTest
, SimpleSingleThreadedTest
) {
38 CancellationFlag flag
;
39 ASSERT_FALSE(flag
.IsSet());
41 ASSERT_TRUE(flag
.IsSet());
44 TEST(CancellationFlagTest
, DoubleSetTest
) {
45 CancellationFlag flag
;
46 ASSERT_FALSE(flag
.IsSet());
48 ASSERT_TRUE(flag
.IsSet());
50 ASSERT_TRUE(flag
.IsSet());
53 TEST(CancellationFlagTest
, SetOnDifferentThreadDeathTest
) {
54 // Checks that Set() can't be called from any other thread.
55 // CancellationFlag should die on a DCHECK if Set() is called from
57 ::testing::FLAGS_gtest_death_test_style
= "threadsafe";
58 Thread
t("CancellationFlagTest.SetOnDifferentThreadDeathTest");
59 ASSERT_TRUE(t
.Start());
60 ASSERT_TRUE(t
.message_loop());
61 ASSERT_TRUE(t
.IsRunning());
63 CancellationFlag flag
;
64 t
.message_loop()->PostTask(FROM_HERE
, new CancelTask(&flag
));