Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(32)

Side by Side Diff: base/synchronization/atomic_flag_unittest.cc

Issue 2163753004: Rename CancellationFlag to AtomicFlag. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: fix dcheck death test Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « base/synchronization/atomic_flag.cc ('k') | base/synchronization/cancellation_flag.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 // Tests of CancellationFlag class. 5 #include "base/synchronization/atomic_flag.h"
6
7 #include "base/synchronization/cancellation_flag.h"
8 6
9 #include "base/bind.h" 7 #include "base/bind.h"
10 #include "base/location.h"
11 #include "base/logging.h" 8 #include "base/logging.h"
12 #include "base/single_thread_task_runner.h" 9 #include "base/single_thread_task_runner.h"
13 #include "base/synchronization/spin_wait.h" 10 #include "base/synchronization/waitable_event.h"
11 #include "base/threading/platform_thread.h"
14 #include "base/threading/thread.h" 12 #include "base/threading/thread.h"
15 #include "base/time/time.h" 13 #include "build/build_config.h"
16 #include "testing/gtest/include/gtest/gtest.h" 14 #include "testing/gtest/include/gtest/gtest.h"
17 #include "testing/platform_test.h" 15
16 // Death tests misbehave on Android.
17 // TODO(fdoray): Remove this when https://codereview.chromium.org/2162053006
18 // lands.
19 #if DCHECK_IS_ON() && defined(GTEST_HAS_DEATH_TEST) && !defined(OS_ANDROID)
20 #define EXPECT_DCHECK_DEATH(statement, regex) EXPECT_DEATH(statement, regex)
21 #else
22 #define EXPECT_DCHECK_DEATH(statement, regex)
23 #endif
18 24
19 namespace base { 25 namespace base {
20 26
21 namespace { 27 namespace {
22 28
23 //------------------------------------------------------------------------------ 29 void ExpectSetFlagDeath(AtomicFlag* flag) {
24 // Define our test class. 30 ASSERT_TRUE(flag);
25 //------------------------------------------------------------------------------ 31 EXPECT_DCHECK_DEATH(flag->Set(), "");
26
27 void CancelHelper(CancellationFlag* flag) {
28 #if GTEST_HAS_DEATH_TEST
29 ASSERT_DEBUG_DEATH(flag->Set(), "");
30 #endif
31 } 32 }
32 33
33 TEST(CancellationFlagTest, SimpleSingleThreadedTest) { 34 void BusyWaitUntilFlagIsSet(AtomicFlag* flag) {
34 CancellationFlag flag; 35 while (!flag->IsSet())
36 PlatformThread::YieldCurrentThread();
37 }
38
39 } // namespace
40
41 TEST(AtomicFlagTest, SimpleSingleThreadedTest) {
42 AtomicFlag flag;
35 ASSERT_FALSE(flag.IsSet()); 43 ASSERT_FALSE(flag.IsSet());
36 flag.Set(); 44 flag.Set();
37 ASSERT_TRUE(flag.IsSet()); 45 ASSERT_TRUE(flag.IsSet());
38 } 46 }
39 47
40 TEST(CancellationFlagTest, DoubleSetTest) { 48 TEST(AtomicFlagTest, DoubleSetTest) {
41 CancellationFlag flag; 49 AtomicFlag flag;
42 ASSERT_FALSE(flag.IsSet()); 50 ASSERT_FALSE(flag.IsSet());
43 flag.Set(); 51 flag.Set();
44 ASSERT_TRUE(flag.IsSet()); 52 ASSERT_TRUE(flag.IsSet());
45 flag.Set(); 53 flag.Set();
46 ASSERT_TRUE(flag.IsSet()); 54 ASSERT_TRUE(flag.IsSet());
47 } 55 }
48 56
49 TEST(CancellationFlagTest, SetOnDifferentThreadDeathTest) { 57 TEST(AtomicFlagTest, ReadFromDifferentThread) {
50 // Checks that Set() can't be called from any other thread. 58 AtomicFlag flag;
51 // CancellationFlag should die on a DCHECK if Set() is called from
52 // other thread.
53 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
54 Thread t("CancellationFlagTest.SetOnDifferentThreadDeathTest");
55 ASSERT_TRUE(t.Start());
56 ASSERT_TRUE(t.message_loop());
57 ASSERT_TRUE(t.IsRunning());
58 59
59 CancellationFlag flag; 60 Thread thread("AtomicFlagTest.ReadFromDifferentThread");
60 t.task_runner()->PostTask(FROM_HERE, base::Bind(&CancelHelper, &flag)); 61 ASSERT_TRUE(thread.Start());
62 thread.task_runner()->PostTask(FROM_HERE,
63 Bind(&BusyWaitUntilFlagIsSet, &flag));
64
65 // To verify that IsSet() fetches the flag's value from memory every time it
66 // is called (not just the first time that it is called on a thread), sleep
67 // before setting the flag.
68 PlatformThread::Sleep(TimeDelta::FromMilliseconds(25));
69
70 flag.Set();
71
72 // The |thread|'s destructor will block until the posted task completes, so
73 // the test will time out if it fails to see the flag be set.
61 } 74 }
62 75
63 } // namespace 76 TEST(AtomicFlagTest, SetOnDifferentThreadDeathTest) {
77 // Checks that Set() can't be called from any other thread. AtomicFlag should
78 // die on a DCHECK if Set() is called from other thread.
79 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
80 Thread t("AtomicFlagTest.SetOnDifferentThreadDeathTest");
81 ASSERT_TRUE(t.Start());
82
83 AtomicFlag flag;
84 t.task_runner()->PostTask(FROM_HERE, Bind(&ExpectSetFlagDeath, &flag));
85 }
64 86
65 } // namespace base 87 } // namespace base
OLDNEW
« no previous file with comments | « base/synchronization/atomic_flag.cc ('k') | base/synchronization/cancellation_flag.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698