Roll breakpad a513e85:7caf028 (svn 1384:1385)
[chromium-blink-merge.git] / base / synchronization / cancellation_flag_unittest.cc
blob02b08b6a9de74e38c584d10ff3a30685d7e2308d
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"
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"
18 namespace base {
20 namespace {
22 //------------------------------------------------------------------------------
23 // Define our test class.
24 //------------------------------------------------------------------------------
26 void CancelHelper(CancellationFlag* flag) {
27 #if GTEST_HAS_DEATH_TEST
28 ASSERT_DEBUG_DEATH(flag->Set(), "");
29 #endif
32 TEST(CancellationFlagTest, SimpleSingleThreadedTest) {
33 CancellationFlag flag;
34 ASSERT_FALSE(flag.IsSet());
35 flag.Set();
36 ASSERT_TRUE(flag.IsSet());
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());
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());
58 CancellationFlag flag;
59 t.message_loop()->PostTask(FROM_HERE, base::Bind(&CancelHelper, &flag));
62 } // namespace
64 } // namespace base