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

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: CR gab #8 (sleep before Set()) 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
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"
12 #include "base/single_thread_task_runner.h" 8 #include "base/single_thread_task_runner.h"
13 #include "base/synchronization/spin_wait.h" 9 #include "base/synchronization/waitable_event.h"
10 #include "base/threading/platform_thread.h"
14 #include "base/threading/thread.h" 11 #include "base/threading/thread.h"
15 #include "base/time/time.h"
16 #include "testing/gtest/include/gtest/gtest.h" 12 #include "testing/gtest/include/gtest/gtest.h"
17 #include "testing/platform_test.h"
18 13
19 namespace base { 14 namespace base {
20 15
21 namespace { 16 namespace {
22 17
23 //------------------------------------------------------------------------------ 18 void ExpectSetFlagDeath(AtomicFlag* flag) {
24 // Define our test class. 19 ASSERT_TRUE(flag);
25 //------------------------------------------------------------------------------
26
27 void CancelHelper(CancellationFlag* flag) {
28 #if GTEST_HAS_DEATH_TEST 20 #if GTEST_HAS_DEATH_TEST
29 ASSERT_DEBUG_DEATH(flag->Set(), ""); 21 ASSERT_DEBUG_DEATH(flag->Set(), "");
30 #endif 22 #endif
31 } 23 }
32 24
33 TEST(CancellationFlagTest, SimpleSingleThreadedTest) { 25 void BusyWaitUntilFlagIsSet(AtomicFlag* flag) {
34 CancellationFlag flag; 26 while (!flag->IsSet())
27 PlatformThread::YieldCurrentThread();
28 }
29
30 } // namespace
31
32 TEST(AtomicFlagTest, SimpleSingleThreadedTest) {
33 AtomicFlag flag;
35 ASSERT_FALSE(flag.IsSet()); 34 ASSERT_FALSE(flag.IsSet());
36 flag.Set(); 35 flag.Set();
37 ASSERT_TRUE(flag.IsSet()); 36 ASSERT_TRUE(flag.IsSet());
38 } 37 }
39 38
40 TEST(CancellationFlagTest, DoubleSetTest) { 39 TEST(AtomicFlagTest, DoubleSetTest) {
41 CancellationFlag flag; 40 AtomicFlag flag;
42 ASSERT_FALSE(flag.IsSet()); 41 ASSERT_FALSE(flag.IsSet());
43 flag.Set(); 42 flag.Set();
44 ASSERT_TRUE(flag.IsSet()); 43 ASSERT_TRUE(flag.IsSet());
45 flag.Set(); 44 flag.Set();
46 ASSERT_TRUE(flag.IsSet()); 45 ASSERT_TRUE(flag.IsSet());
47 } 46 }
48 47
49 TEST(CancellationFlagTest, SetOnDifferentThreadDeathTest) { 48 TEST(AtomicFlagTest, ReadFromDifferentThread) {
50 // Checks that Set() can't be called from any other thread. 49 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 50
59 CancellationFlag flag; 51 Thread thread("AtomicFlagTest.ReadFromDifferentThread");
60 t.task_runner()->PostTask(FROM_HERE, base::Bind(&CancelHelper, &flag)); 52 ASSERT_TRUE(thread.Start());
53 thread.task_runner()->PostTask(FROM_HERE,
54 Bind(&BusyWaitUntilFlagIsSet, &flag));
55
56 // To verify that IsSet() fetches the flag's value from memory every time it
57 // is called (not just the first time that it is called on a thread), sleep
58 // before setting the flag.
59 PlatformThread::Sleep(TimeDelta::FromMilliseconds(25));
60
61 flag.Set();
danakj 2016/07/21 19:21:42 can you leave a comment below this that quickly ex
61 } 62 }
62 63
63 } // namespace 64 TEST(AtomicFlagTest, SetOnDifferentThreadDeathTest) {
65 // Checks that Set() can't be called from any other thread. AtomicFlag should
66 // die on a DCHECK if Set() is called from other thread.
67 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
68 Thread t("AtomicFlagTest.SetOnDifferentThreadDeathTest");
69 ASSERT_TRUE(t.Start());
70
71 AtomicFlag flag;
72 t.task_runner()->PostTask(FROM_HERE, Bind(&ExpectSetFlagDeath, &flag));
73 }
64 74
65 } // namespace base 75 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698