Chromium Code Reviews| 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/bind.h" |
| 10 #include "base/location.h" | 10 #include "base/location.h" |
| 11 #include "base/logging.h" | |
| 12 #include "base/single_thread_task_runner.h" | 11 #include "base/single_thread_task_runner.h" |
| 13 #include "base/synchronization/spin_wait.h" | 12 #include "base/synchronization/spin_wait.h" |
| 13 #include "base/test/gtest_util.h" | |
| 14 #include "base/threading/thread.h" | 14 #include "base/threading/thread.h" |
| 15 #include "base/time/time.h" | 15 #include "base/time/time.h" |
| 16 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
| 17 #include "testing/platform_test.h" | 17 #include "testing/platform_test.h" |
| 18 | 18 |
| 19 namespace base { | 19 namespace base { |
| 20 | 20 |
| 21 namespace { | |
| 22 | |
| 23 //------------------------------------------------------------------------------ | |
| 24 // Define our test class. | |
| 25 //------------------------------------------------------------------------------ | |
| 26 | |
| 27 void CancelHelper(CancellationFlag* flag) { | |
| 28 #if GTEST_HAS_DEATH_TEST | |
| 29 ASSERT_DEBUG_DEATH(flag->Set(), ""); | |
| 30 #endif | |
| 31 } | |
| 32 | |
| 33 TEST(CancellationFlagTest, SimpleSingleThreadedTest) { | 21 TEST(CancellationFlagTest, SimpleSingleThreadedTest) { |
| 34 CancellationFlag flag; | 22 CancellationFlag flag; |
| 35 ASSERT_FALSE(flag.IsSet()); | 23 ASSERT_FALSE(flag.IsSet()); |
| 36 flag.Set(); | 24 flag.Set(); |
| 37 ASSERT_TRUE(flag.IsSet()); | 25 ASSERT_TRUE(flag.IsSet()); |
| 38 } | 26 } |
| 39 | 27 |
| 40 TEST(CancellationFlagTest, DoubleSetTest) { | 28 TEST(CancellationFlagTest, DoubleSetTest) { |
| 41 CancellationFlag flag; | 29 CancellationFlag flag; |
| 42 ASSERT_FALSE(flag.IsSet()); | 30 ASSERT_FALSE(flag.IsSet()); |
| 43 flag.Set(); | 31 flag.Set(); |
| 44 ASSERT_TRUE(flag.IsSet()); | 32 ASSERT_TRUE(flag.IsSet()); |
| 45 flag.Set(); | 33 flag.Set(); |
| 46 ASSERT_TRUE(flag.IsSet()); | 34 ASSERT_TRUE(flag.IsSet()); |
| 47 } | 35 } |
| 48 | 36 |
| 37 namespace { | |
| 38 | |
| 39 void CancelHelper(CancellationFlag* flag) { | |
| 40 EXPECT_DCHECK_DEATH(flag->Set(), ""); | |
| 41 } | |
| 42 | |
| 43 } // namespace | |
| 44 | |
| 49 TEST(CancellationFlagTest, SetOnDifferentThreadDeathTest) { | 45 TEST(CancellationFlagTest, SetOnDifferentThreadDeathTest) { |
| 50 // Checks that Set() can't be called from any other thread. | 46 // Checks that Set() can't be called from any other thread. |
| 51 // CancellationFlag should die on a DCHECK if Set() is called from | 47 // CancellationFlag should die on a DCHECK if Set() is called from |
| 52 // other thread. | 48 // other thread. |
| 53 ::testing::FLAGS_gtest_death_test_style = "threadsafe"; | 49 ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 54 Thread t("CancellationFlagTest.SetOnDifferentThreadDeathTest"); | 50 Thread t("CancellationFlagTest.SetOnDifferentThreadDeathTest"); |
| 55 ASSERT_TRUE(t.Start()); | 51 ASSERT_TRUE(t.Start()); |
| 56 ASSERT_TRUE(t.message_loop()); | 52 ASSERT_TRUE(t.message_loop()); |
| 57 ASSERT_TRUE(t.IsRunning()); | 53 ASSERT_TRUE(t.IsRunning()); |
| 58 | 54 |
| 59 CancellationFlag flag; | 55 CancellationFlag flag; |
| 60 t.task_runner()->PostTask(FROM_HERE, base::Bind(&CancelHelper, &flag)); | 56 t.task_runner()->PostTask(FROM_HERE, base::Bind(&CancelHelper, &flag)); |
| 61 } | 57 } |
| 62 | 58 |
| 63 } // namespace | |
|
danakj
2016/07/20 19:33:45
I think it is usual to wrap a unit test file an a
gab
2016/07/20 21:21:08
Done.
| |
| 64 | |
| 65 } // namespace base | 59 } // namespace base |
| OLD | NEW |