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 38% |
| rename from base/synchronization/cancellation_flag_unittest.cc |
| rename to base/synchronization/atomic_flag_unittest.cc |
| index 13c74bcbd45ffa7b12703e93ab4c5065ccbff88b..3ba20aaa68a2204178151b523ef5ba8a69fb535a 100644 |
| --- a/base/synchronization/cancellation_flag_unittest.cc |
| +++ b/base/synchronization/atomic_flag_unittest.cc |
| @@ -2,43 +2,47 @@ |
| // 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) { |
| + // Sleep to increase the likelihood of Set() being called before the first |
| + // call to IsSet(). |
| + PlatformThread::Sleep(TimeDelta::FromMilliseconds(10)); |
|
gab
2016/07/21 14:16:04
I meant to add the Sleep() on the main thread befo
fdoray
2016/07/21 15:50:02
Done.
|
| + |
| + 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 +50,26 @@ 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)); |
| + |
| + flag.Set(); |
| +} |
| + |
| +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 |