| OLD | NEW |
| 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 // Tests of CancellationFlag class. |
| 6 | 6 |
| 7 #include "base/synchronization/cancellation_flag.h" | 7 #include "base/synchronization/cancellation_flag.h" |
| 8 | 8 |
| 9 #include "base/bind.h" |
| 9 #include "base/logging.h" | 10 #include "base/logging.h" |
| 10 #include "base/message_loop.h" | 11 #include "base/message_loop.h" |
| 11 #include "base/spin_wait.h" | 12 #include "base/spin_wait.h" |
| 12 #include "base/time.h" | 13 #include "base/time.h" |
| 13 #include "base/threading/thread.h" | 14 #include "base/threading/thread.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
| 15 #include "testing/platform_test.h" | 16 #include "testing/platform_test.h" |
| 16 | 17 |
| 17 namespace base { | 18 namespace base { |
| 18 | 19 |
| 19 namespace { | 20 namespace { |
| 20 | 21 |
| 21 //------------------------------------------------------------------------------ | 22 //------------------------------------------------------------------------------ |
| 22 // Define our test class. | 23 // Define our test class. |
| 23 //------------------------------------------------------------------------------ | 24 //------------------------------------------------------------------------------ |
| 24 | 25 |
| 25 class CancelTask : public Task { | 26 void CancelHelper(CancellationFlag* flag) { |
| 26 public: | |
| 27 explicit CancelTask(CancellationFlag* flag) : flag_(flag) {} | |
| 28 virtual void Run() { | |
| 29 #if GTEST_HAS_DEATH_TEST | 27 #if GTEST_HAS_DEATH_TEST |
| 30 ASSERT_DEBUG_DEATH(flag_->Set(), ""); | 28 ASSERT_DEBUG_DEATH(flag->Set(), ""); |
| 31 #endif | 29 #endif |
| 32 } | 30 } |
| 33 private: | |
| 34 CancellationFlag* flag_; | |
| 35 }; | |
| 36 | 31 |
| 37 TEST(CancellationFlagTest, SimpleSingleThreadedTest) { | 32 TEST(CancellationFlagTest, SimpleSingleThreadedTest) { |
| 38 CancellationFlag flag; | 33 CancellationFlag flag; |
| 39 ASSERT_FALSE(flag.IsSet()); | 34 ASSERT_FALSE(flag.IsSet()); |
| 40 flag.Set(); | 35 flag.Set(); |
| 41 ASSERT_TRUE(flag.IsSet()); | 36 ASSERT_TRUE(flag.IsSet()); |
| 42 } | 37 } |
| 43 | 38 |
| 44 TEST(CancellationFlagTest, DoubleSetTest) { | 39 TEST(CancellationFlagTest, DoubleSetTest) { |
| 45 CancellationFlag flag; | 40 CancellationFlag flag; |
| 46 ASSERT_FALSE(flag.IsSet()); | 41 ASSERT_FALSE(flag.IsSet()); |
| 47 flag.Set(); | 42 flag.Set(); |
| 48 ASSERT_TRUE(flag.IsSet()); | 43 ASSERT_TRUE(flag.IsSet()); |
| 49 flag.Set(); | 44 flag.Set(); |
| 50 ASSERT_TRUE(flag.IsSet()); | 45 ASSERT_TRUE(flag.IsSet()); |
| 51 } | 46 } |
| 52 | 47 |
| 53 TEST(CancellationFlagTest, SetOnDifferentThreadDeathTest) { | 48 TEST(CancellationFlagTest, SetOnDifferentThreadDeathTest) { |
| 54 // Checks that Set() can't be called from any other thread. | 49 // Checks that Set() can't be called from any other thread. |
| 55 // CancellationFlag should die on a DCHECK if Set() is called from | 50 // CancellationFlag should die on a DCHECK if Set() is called from |
| 56 // other thread. | 51 // other thread. |
| 57 ::testing::FLAGS_gtest_death_test_style = "threadsafe"; | 52 ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 58 Thread t("CancellationFlagTest.SetOnDifferentThreadDeathTest"); | 53 Thread t("CancellationFlagTest.SetOnDifferentThreadDeathTest"); |
| 59 ASSERT_TRUE(t.Start()); | 54 ASSERT_TRUE(t.Start()); |
| 60 ASSERT_TRUE(t.message_loop()); | 55 ASSERT_TRUE(t.message_loop()); |
| 61 ASSERT_TRUE(t.IsRunning()); | 56 ASSERT_TRUE(t.IsRunning()); |
| 62 | 57 |
| 63 CancellationFlag flag; | 58 CancellationFlag flag; |
| 64 t.message_loop()->PostTask(FROM_HERE, new CancelTask(&flag)); | 59 t.message_loop()->PostTask(FROM_HERE, base::Bind(&CancelHelper, &flag)); |
| 65 } | 60 } |
| 66 | 61 |
| 67 } // namespace | 62 } // namespace |
| 68 | 63 |
| 69 } // namespace base | 64 } // namespace base |
| OLD | NEW |