Chromium Code Reviews| Index: base/synchronization/atomic_flag_unittest.cc |
| diff --git a/base/synchronization/cancellation_flag_unittest.cc b/base/synchronization/atomic_flag_unittest.cc |
| similarity index 36% |
| rename from base/synchronization/cancellation_flag_unittest.cc |
| rename to base/synchronization/atomic_flag_unittest.cc |
| index 13c74bcbd45ffa7b12703e93ab4c5065ccbff88b..f08d3a1bfe69cfbe67db90ee235feebdf2a9f8fc 100644 |
| --- a/base/synchronization/cancellation_flag_unittest.cc |
| +++ b/base/synchronization/atomic_flag_unittest.cc |
| @@ -2,43 +2,42 @@ |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| -// Tests of CancellationFlag class. |
| - |
| -#include "base/synchronization/cancellation_flag.h" |
| +#include "base/synchronization/atomic_flag.h" |
| #include "base/bind.h" |
| -#include "base/location.h" |
| -#include "base/logging.h" |
| #include "base/single_thread_task_runner.h" |
| -#include "base/synchronization/spin_wait.h" |
| +#include "base/synchronization/waitable_event.h" |
| +#include "base/threading/platform_thread.h" |
| #include "base/threading/thread.h" |
| -#include "base/time/time.h" |
| #include "testing/gtest/include/gtest/gtest.h" |
| -#include "testing/platform_test.h" |
| namespace base { |
| namespace { |
| -//------------------------------------------------------------------------------ |
| -// Define our test class. |
| -//------------------------------------------------------------------------------ |
| - |
| -void CancelHelper(CancellationFlag* flag) { |
| +void ExpectSetFlagDeath(AtomicFlag* flag) { |
| + ASSERT_TRUE(flag); |
| #if GTEST_HAS_DEATH_TEST |
| ASSERT_DEBUG_DEATH(flag->Set(), ""); |
| #endif |
| } |
| -TEST(CancellationFlagTest, SimpleSingleThreadedTest) { |
| - CancellationFlag flag; |
| +void BusyWaitUntilFlagIsSet(AtomicFlag* flag) { |
| + while (!flag->IsSet()) |
| + PlatformThread::YieldCurrentThread(); |
| +} |
| + |
| +} // namespace |
| + |
| +TEST(AtomicFlagTest, SimpleSingleThreadedTest) { |
| + AtomicFlag flag; |
| ASSERT_FALSE(flag.IsSet()); |
| flag.Set(); |
| ASSERT_TRUE(flag.IsSet()); |
| } |
| -TEST(CancellationFlagTest, DoubleSetTest) { |
| - CancellationFlag flag; |
| +TEST(AtomicFlagTest, DoubleSetTest) { |
| + AtomicFlag flag; |
| ASSERT_FALSE(flag.IsSet()); |
| flag.Set(); |
| ASSERT_TRUE(flag.IsSet()); |
| @@ -46,20 +45,31 @@ TEST(CancellationFlagTest, DoubleSetTest) { |
| ASSERT_TRUE(flag.IsSet()); |
| } |
| -TEST(CancellationFlagTest, SetOnDifferentThreadDeathTest) { |
| - // Checks that Set() can't be called from any other thread. |
| - // CancellationFlag should die on a DCHECK if Set() is called from |
| - // other thread. |
| +TEST(AtomicFlagTest, ReadFromDifferentThread) { |
| + AtomicFlag flag; |
| + |
| + Thread thread("AtomicFlagTest.ReadFromDifferentThread"); |
| + ASSERT_TRUE(thread.Start()); |
| + thread.task_runner()->PostTask(FROM_HERE, |
| + Bind(&BusyWaitUntilFlagIsSet, &flag)); |
| + |
| + // To verify that IsSet() fetches the flag's value from memory every time it |
| + // is called (not just the first time that it is called on a thread), sleep |
| + // before setting the flag. |
| + PlatformThread::Sleep(TimeDelta::FromMilliseconds(25)); |
| + |
| + flag.Set(); |
|
danakj
2016/07/21 19:21:42
can you leave a comment below this that quickly ex
|
| +} |
| + |
| +TEST(AtomicFlagTest, SetOnDifferentThreadDeathTest) { |
| + // Checks that Set() can't be called from any other thread. AtomicFlag should |
| + // die on a DCHECK if Set() is called from other thread. |
| ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| - Thread t("CancellationFlagTest.SetOnDifferentThreadDeathTest"); |
| + Thread t("AtomicFlagTest.SetOnDifferentThreadDeathTest"); |
| ASSERT_TRUE(t.Start()); |
| - ASSERT_TRUE(t.message_loop()); |
| - ASSERT_TRUE(t.IsRunning()); |
| - CancellationFlag flag; |
| - t.task_runner()->PostTask(FROM_HERE, base::Bind(&CancelHelper, &flag)); |
| + AtomicFlag flag; |
| + t.task_runner()->PostTask(FROM_HERE, Bind(&ExpectSetFlagDeath, &flag)); |
| } |
| -} // namespace |
| - |
| } // namespace base |