| 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 28%
|
| rename from base/synchronization/cancellation_flag_unittest.cc
|
| rename to base/synchronization/atomic_flag_unittest.cc
|
| index 13c74bcbd45ffa7b12703e93ab4c5065ccbff88b..b0dfd47e4653970789739e24126946bd5c790a1f 100644
|
| --- a/base/synchronization/cancellation_flag_unittest.cc
|
| +++ b/base/synchronization/atomic_flag_unittest.cc
|
| @@ -2,43 +2,51 @@
|
| // 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 "build/build_config.h"
|
| #include "testing/gtest/include/gtest/gtest.h"
|
| -#include "testing/platform_test.h"
|
| +
|
| +// Death tests misbehave on Android.
|
| +// TODO(fdoray): Remove this when https://codereview.chromium.org/2162053006
|
| +// lands.
|
| +#if DCHECK_IS_ON() && defined(GTEST_HAS_DEATH_TEST) && !defined(OS_ANDROID)
|
| +#define EXPECT_DCHECK_DEATH(statement, regex) EXPECT_DEATH(statement, regex)
|
| +#else
|
| +#define EXPECT_DCHECK_DEATH(statement, regex)
|
| +#endif
|
|
|
| namespace base {
|
|
|
| namespace {
|
|
|
| -//------------------------------------------------------------------------------
|
| -// Define our test class.
|
| -//------------------------------------------------------------------------------
|
| +void ExpectSetFlagDeath(AtomicFlag* flag) {
|
| + ASSERT_TRUE(flag);
|
| + EXPECT_DCHECK_DEATH(flag->Set(), "");
|
| +}
|
|
|
| -void CancelHelper(CancellationFlag* flag) {
|
| -#if GTEST_HAS_DEATH_TEST
|
| - ASSERT_DEBUG_DEATH(flag->Set(), "");
|
| -#endif
|
| +void BusyWaitUntilFlagIsSet(AtomicFlag* flag) {
|
| + while (!flag->IsSet())
|
| + PlatformThread::YieldCurrentThread();
|
| }
|
|
|
| -TEST(CancellationFlagTest, SimpleSingleThreadedTest) {
|
| - CancellationFlag flag;
|
| +} // 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 +54,34 @@ 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();
|
| +
|
| + // The |thread|'s destructor will block until the posted task completes, so
|
| + // the test will time out if it fails to see the flag be 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
|
|
|