OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 // Tests of AtomicFlag class. |
| 6 |
| 7 #include "base/atomic_flag.h" |
| 8 #include "base/logging.h" |
| 9 #include "base/spin_wait.h" |
| 10 #include "base/time.h" |
| 11 #include "base/thread.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" |
| 13 #include "testing/platform_test.h" |
| 14 |
| 15 using base::AtomicFlag; |
| 16 using base::TimeDelta; |
| 17 using base::Thread; |
| 18 |
| 19 namespace { |
| 20 |
| 21 //------------------------------------------------------------------------------ |
| 22 // Define our test class. |
| 23 //------------------------------------------------------------------------------ |
| 24 |
| 25 class NotifyTask : public Task { |
| 26 public: |
| 27 explicit NotifyTask(AtomicFlag* flag) : flag_(flag) { |
| 28 } |
| 29 virtual void Run() { |
| 30 flag_->Set(); |
| 31 } |
| 32 private: |
| 33 AtomicFlag* flag_; |
| 34 }; |
| 35 |
| 36 TEST(AtomicFlagTest, SimpleSingleThreadedTest) { |
| 37 AtomicFlag flag; |
| 38 CHECK(!flag.IsSet()); |
| 39 flag.Set(); |
| 40 CHECK(flag.IsSet()); |
| 41 } |
| 42 |
| 43 TEST(AtomicFlagTest, SimpleSingleThreadedTestPrenotified) { |
| 44 AtomicFlag flag(true); |
| 45 CHECK(flag.IsSet()); |
| 46 } |
| 47 |
| 48 #if defined(OS_WIN) |
| 49 #define DISABLED_ON_WIN(x) DISABLED_##x |
| 50 #else |
| 51 #define DISABLED_ON_WIN(x) x |
| 52 #endif |
| 53 |
| 54 // AtomicFlag should die on a DCHECK if Set() is called more than once. |
| 55 // This test isn't Windows-friendly yet since ASSERT_DEATH doesn't catch tests |
| 56 // failed on DCHECK. See http://crbug.com/24885 for the details. |
| 57 TEST(AtomicFlagTest, DISABLED_ON_WIN(DoubleSetDeathTest)) { |
| 58 // Checks that Set() can't be called more than once. |
| 59 ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 60 AtomicFlag flag; |
| 61 flag.Set(); |
| 62 ASSERT_DEBUG_DEATH(flag.Set(), ""); |
| 63 } |
| 64 |
| 65 TEST(AtomicFlagTest, SimpleThreadedTest) { |
| 66 Thread t("AtomicFlagTest.SimpleThreadedTest"); |
| 67 EXPECT_TRUE(t.Start()); |
| 68 EXPECT_TRUE(t.message_loop()); |
| 69 EXPECT_TRUE(t.IsRunning()); |
| 70 |
| 71 AtomicFlag flag; |
| 72 CHECK(!flag.IsSet()); |
| 73 t.message_loop()->PostTask(FROM_HERE, new NotifyTask(&flag)); |
| 74 SPIN_FOR_TIMEDELTA_OR_UNTIL_TRUE(TimeDelta::FromSeconds(10), |
| 75 flag.IsSet()); |
| 76 CHECK(flag.IsSet()); |
| 77 } |
| 78 |
| 79 } // namespace |
OLD | NEW |