[email protected] | d9b1478 | 2010-04-15 08:08:07 | [diff] [blame] | 1 | // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
[email protected] | baeb22f5 | 2009-12-03 21:11:34 | [diff] [blame] | 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 CancellationFlag class. |
| 6 | |
| 7 | #include "base/cancellation_flag.h" |
| 8 | |
| 9 | #include "base/logging.h" |
[email protected] | d9b1478 | 2010-04-15 08:08:07 | [diff] [blame] | 10 | #include "base/message_loop.h" |
[email protected] | baeb22f5 | 2009-12-03 21:11:34 | [diff] [blame] | 11 | #include "base/spin_wait.h" |
| 12 | #include "base/time.h" |
| 13 | #include "base/thread.h" |
| 14 | #include "testing/gtest/include/gtest/gtest.h" |
| 15 | #include "testing/platform_test.h" |
| 16 | |
| 17 | using base::CancellationFlag; |
| 18 | using base::TimeDelta; |
| 19 | using base::Thread; |
| 20 | |
| 21 | namespace { |
| 22 | |
| 23 | //------------------------------------------------------------------------------ |
| 24 | // Define our test class. |
| 25 | //------------------------------------------------------------------------------ |
| 26 | |
| 27 | class CancelTask : public Task { |
| 28 | public: |
| 29 | explicit CancelTask(CancellationFlag* flag) : flag_(flag) {} |
| 30 | virtual void Run() { |
| 31 | ASSERT_DEBUG_DEATH(flag_->Set(), ""); |
| 32 | } |
| 33 | private: |
| 34 | CancellationFlag* flag_; |
| 35 | }; |
| 36 | |
| 37 | TEST(CancellationFlagTest, SimpleSingleThreadedTest) { |
| 38 | CancellationFlag flag; |
| 39 | ASSERT_FALSE(flag.IsSet()); |
| 40 | flag.Set(); |
| 41 | ASSERT_TRUE(flag.IsSet()); |
| 42 | } |
| 43 | |
| 44 | TEST(CancellationFlagTest, DoubleSetTest) { |
| 45 | CancellationFlag flag; |
| 46 | ASSERT_FALSE(flag.IsSet()); |
| 47 | flag.Set(); |
| 48 | ASSERT_TRUE(flag.IsSet()); |
| 49 | flag.Set(); |
| 50 | ASSERT_TRUE(flag.IsSet()); |
| 51 | } |
| 52 | |
[email protected] | 31a80f2 | 2009-12-10 20:33:02 | [diff] [blame] | 53 | TEST(CancellationFlagTest, SetOnDifferentThreadDeathTest) { |
[email protected] | baeb22f5 | 2009-12-03 21:11:34 | [diff] [blame] | 54 | // Checks that Set() can't be called from any other thread. |
[email protected] | 31a80f2 | 2009-12-10 20:33:02 | [diff] [blame] | 55 | // CancellationFlag should die on a DCHECK if Set() is called from |
| 56 | // other thread. |
[email protected] | baeb22f5 | 2009-12-03 21:11:34 | [diff] [blame] | 57 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 58 | Thread t("CancellationFlagTest.SetOnDifferentThreadDeathTest"); |
| 59 | ASSERT_TRUE(t.Start()); |
| 60 | ASSERT_TRUE(t.message_loop()); |
| 61 | ASSERT_TRUE(t.IsRunning()); |
| 62 | |
| 63 | CancellationFlag flag; |
| 64 | t.message_loop()->PostTask(FROM_HERE, new CancelTask(&flag)); |
| 65 | } |
| 66 | |
| 67 | } // namespace |