OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 #include "mojo/message_pump/handle_watcher.h" | 5 #include "mojo/message_pump/handle_watcher.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/at_exit.h" | 9 #include "base/at_exit.h" |
10 #include "base/auto_reset.h" | 10 #include "base/auto_reset.h" |
11 #include "base/bind.h" | 11 #include "base/bind.h" |
12 #include "base/macros.h" | 12 #include "base/macros.h" |
| 13 #include "base/memory/scoped_ptr.h" |
13 #include "base/memory/scoped_vector.h" | 14 #include "base/memory/scoped_vector.h" |
14 #include "base/run_loop.h" | 15 #include "base/run_loop.h" |
15 #include "base/test/simple_test_tick_clock.h" | 16 #include "base/test/simple_test_tick_clock.h" |
16 #include "base/threading/thread.h" | 17 #include "base/threading/thread.h" |
17 #include "mojo/message_pump/message_pump_mojo.h" | 18 #include "mojo/message_pump/message_pump_mojo.h" |
18 #include "mojo/message_pump/time_helper.h" | 19 #include "mojo/message_pump/time_helper.h" |
19 #include "mojo/public/cpp/system/core.h" | 20 #include "mojo/public/cpp/system/core.h" |
20 #include "mojo/public/cpp/test_support/test_utils.h" | 21 #include "mojo/public/cpp/test_support/test_utils.h" |
21 #include "testing/gtest/include/gtest/gtest.h" | 22 #include "testing/gtest/include/gtest/gtest.h" |
22 | 23 |
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
114 base::RunLoop* run_loop_; | 115 base::RunLoop* run_loop_; |
115 | 116 |
116 base::WeakPtrFactory<CallbackHelper> weak_factory_; | 117 base::WeakPtrFactory<CallbackHelper> weak_factory_; |
117 | 118 |
118 private: | 119 private: |
119 DISALLOW_COPY_AND_ASSIGN(CallbackHelper); | 120 DISALLOW_COPY_AND_ASSIGN(CallbackHelper); |
120 }; | 121 }; |
121 | 122 |
122 class HandleWatcherTest : public testing::TestWithParam<MessageLoopConfig> { | 123 class HandleWatcherTest : public testing::TestWithParam<MessageLoopConfig> { |
123 public: | 124 public: |
124 HandleWatcherTest() : message_loop_(CreateMessageLoop(GetParam())) {} | 125 HandleWatcherTest() |
| 126 : at_exit_(new base::ShadowingAtExitManager), |
| 127 message_loop_(CreateMessageLoop(GetParam())) {} |
125 virtual ~HandleWatcherTest() { | 128 virtual ~HandleWatcherTest() { |
| 129 // By explicitly destroying |at_exit_| before resetting the tick clock, it |
| 130 // ensures that the handle watcher thread (if there is one) is shut down, |
| 131 // preventing a race with users of the tick clock in MessagePumpMojo. |
| 132 at_exit_.reset(); |
126 test::SetTickClockForTest(NULL); | 133 test::SetTickClockForTest(NULL); |
127 } | 134 } |
128 | 135 |
129 protected: | 136 protected: |
130 void TearDownMessageLoop() { | 137 void TearDownMessageLoop() { |
131 message_loop_.reset(); | 138 message_loop_.reset(); |
132 } | 139 } |
133 | 140 |
| 141 // This should be called at the beginning of any test that needs it, so that |
| 142 // it is installed before the handle watcher thread starts. |
134 void InstallTickClock() { | 143 void InstallTickClock() { |
135 test::SetTickClockForTest(&tick_clock_); | 144 test::SetTickClockForTest(&tick_clock_); |
136 } | 145 } |
137 | 146 |
138 base::SimpleTestTickClock tick_clock_; | 147 base::SimpleTestTickClock tick_clock_; |
139 | 148 |
140 private: | 149 private: |
141 base::ShadowingAtExitManager at_exit_; | 150 scoped_ptr<base::ShadowingAtExitManager> at_exit_; |
142 scoped_ptr<base::MessageLoop> message_loop_; | 151 scoped_ptr<base::MessageLoop> message_loop_; |
143 | 152 |
144 DISALLOW_COPY_AND_ASSIGN(HandleWatcherTest); | 153 DISALLOW_COPY_AND_ASSIGN(HandleWatcherTest); |
145 }; | 154 }; |
146 | 155 |
147 INSTANTIATE_TEST_CASE_P( | 156 INSTANTIATE_TEST_CASE_P( |
148 MultipleMessageLoopConfigs, HandleWatcherTest, | 157 MultipleMessageLoopConfigs, HandleWatcherTest, |
149 testing::Values(MESSAGE_LOOP_CONFIG_DEFAULT, MESSAGE_LOOP_CONFIG_MOJO)); | 158 testing::Values(MESSAGE_LOOP_CONFIG_DEFAULT, MESSAGE_LOOP_CONFIG_MOJO)); |
150 | 159 |
151 // Trivial test case with a single handle to watch. | 160 // Trivial test case with a single handle to watch. |
(...skipping 323 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
475 message_loop.task_runner(), | 484 message_loop.task_runner(), |
476 &run_loop, &threads_active_counter)); | 485 &run_loop, &threads_active_counter)); |
477 } | 486 } |
478 run_loop.Run(); | 487 run_loop.Run(); |
479 ASSERT_EQ(0, threads_active_counter); | 488 ASSERT_EQ(0, threads_active_counter); |
480 } | 489 } |
481 | 490 |
482 } // namespace test | 491 } // namespace test |
483 } // namespace common | 492 } // namespace common |
484 } // namespace mojo | 493 } // namespace mojo |
OLD | NEW |