OLD | NEW |
| (Empty) |
1 // Copyright (c) 2010 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 CancellationFlag class. | |
6 | |
7 #include "base/cancellation_flag.h" | |
8 | |
9 #include "base/logging.h" | |
10 #include "base/message_loop.h" | |
11 #include "base/spin_wait.h" | |
12 #include "base/time.h" | |
13 #include "base/threading/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 | |
53 TEST(CancellationFlagTest, SetOnDifferentThreadDeathTest) { | |
54 // Checks that Set() can't be called from any other thread. | |
55 // CancellationFlag should die on a DCHECK if Set() is called from | |
56 // other thread. | |
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 | |
OLD | NEW |