[email protected] | 4410618 | 2012-04-06 03:53:02 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [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 | |
[email protected] | f7b98b3 | 2013-02-05 08:14:15 | [diff] [blame] | 5 | #include "base/synchronization/waitable_event_watcher.h" |
| 6 | |
[email protected] | 329be05 | 2013-02-04 18:14:28 | [diff] [blame] | 7 | #include "base/bind.h" |
| 8 | #include "base/callback.h" |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame^] | 9 | #include "base/macros.h" |
[email protected] | 495cad9 | 2013-07-18 08:12:40 | [diff] [blame] | 10 | #include "base/message_loop/message_loop.h" |
[email protected] | f7b98b3 | 2013-02-05 08:14:15 | [diff] [blame] | 11 | #include "base/run_loop.h" |
[email protected] | 44f9c95 | 2011-01-02 06:05:39 | [diff] [blame] | 12 | #include "base/synchronization/waitable_event.h" |
[email protected] | ce072a7 | 2010-12-31 20:02:16 | [diff] [blame] | 13 | #include "base/threading/platform_thread.h" |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame^] | 14 | #include "build/build_config.h" |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 15 | #include "testing/gtest/include/gtest/gtest.h" |
| 16 | |
[email protected] | 44f9c95 | 2011-01-02 06:05:39 | [diff] [blame] | 17 | namespace base { |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 18 | |
| 19 | namespace { |
| 20 | |
[email protected] | 840246b | 2012-07-18 08:06:50 | [diff] [blame] | 21 | // The message loops on which each waitable event timer should be tested. |
| 22 | const MessageLoop::Type testing_message_loops[] = { |
| 23 | MessageLoop::TYPE_DEFAULT, |
| 24 | MessageLoop::TYPE_IO, |
| 25 | #if !defined(OS_IOS) // iOS does not allow direct running of the UI loop. |
| 26 | MessageLoop::TYPE_UI, |
| 27 | #endif |
| 28 | }; |
| 29 | |
| 30 | const int kNumTestingMessageLoops = arraysize(testing_message_loops); |
| 31 | |
[email protected] | 329be05 | 2013-02-04 18:14:28 | [diff] [blame] | 32 | void QuitWhenSignaled(WaitableEvent* event) { |
| 33 | MessageLoop::current()->QuitWhenIdle(); |
| 34 | } |
[email protected] | 1eaa5479 | 2013-01-31 23:14:27 | [diff] [blame] | 35 | |
[email protected] | 329be05 | 2013-02-04 18:14:28 | [diff] [blame] | 36 | class DecrementCountContainer { |
[email protected] | 1eaa5479 | 2013-01-31 23:14:27 | [diff] [blame] | 37 | public: |
[email protected] | 329be05 | 2013-02-04 18:14:28 | [diff] [blame] | 38 | explicit DecrementCountContainer(int* counter) : counter_(counter) { |
[email protected] | 1eaa5479 | 2013-01-31 23:14:27 | [diff] [blame] | 39 | } |
[email protected] | 329be05 | 2013-02-04 18:14:28 | [diff] [blame] | 40 | void OnWaitableEventSignaled(WaitableEvent* object) { |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 41 | --(*counter_); |
| 42 | } |
| 43 | private: |
| 44 | int* counter_; |
| 45 | }; |
| 46 | |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 47 | void RunTest_BasicSignal(MessageLoop::Type message_loop_type) { |
| 48 | MessageLoop message_loop(message_loop_type); |
| 49 | |
| 50 | // A manual-reset event that is not yet signaled. |
| 51 | WaitableEvent event(true, false); |
| 52 | |
| 53 | WaitableEventWatcher watcher; |
[email protected] | 9b6fee1 | 2009-09-29 18:13:07 | [diff] [blame] | 54 | EXPECT_TRUE(watcher.GetWatchedEvent() == NULL); |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 55 | |
[email protected] | 329be05 | 2013-02-04 18:14:28 | [diff] [blame] | 56 | watcher.StartWatching(&event, Bind(&QuitWhenSignaled)); |
[email protected] | 5b7a6ce | 2009-01-15 22:31:17 | [diff] [blame] | 57 | EXPECT_EQ(&event, watcher.GetWatchedEvent()); |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 58 | |
| 59 | event.Signal(); |
| 60 | |
| 61 | MessageLoop::current()->Run(); |
| 62 | |
[email protected] | 9b6fee1 | 2009-09-29 18:13:07 | [diff] [blame] | 63 | EXPECT_TRUE(watcher.GetWatchedEvent() == NULL); |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | void RunTest_BasicCancel(MessageLoop::Type message_loop_type) { |
| 67 | MessageLoop message_loop(message_loop_type); |
| 68 | |
| 69 | // A manual-reset event that is not yet signaled. |
| 70 | WaitableEvent event(true, false); |
| 71 | |
| 72 | WaitableEventWatcher watcher; |
| 73 | |
[email protected] | 329be05 | 2013-02-04 18:14:28 | [diff] [blame] | 74 | watcher.StartWatching(&event, Bind(&QuitWhenSignaled)); |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 75 | |
| 76 | watcher.StopWatching(); |
| 77 | } |
| 78 | |
| 79 | void RunTest_CancelAfterSet(MessageLoop::Type message_loop_type) { |
| 80 | MessageLoop message_loop(message_loop_type); |
| 81 | |
| 82 | // A manual-reset event that is not yet signaled. |
| 83 | WaitableEvent event(true, false); |
| 84 | |
| 85 | WaitableEventWatcher watcher; |
| 86 | |
| 87 | int counter = 1; |
[email protected] | 329be05 | 2013-02-04 18:14:28 | [diff] [blame] | 88 | DecrementCountContainer delegate(&counter); |
| 89 | WaitableEventWatcher::EventCallback callback = |
| 90 | Bind(&DecrementCountContainer::OnWaitableEventSignaled, |
| 91 | Unretained(&delegate)); |
| 92 | watcher.StartWatching(&event, callback); |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 93 | |
| 94 | event.Signal(); |
| 95 | |
| 96 | // Let the background thread do its business |
[email protected] | a1b75b94 | 2011-12-31 22:53:51 | [diff] [blame] | 97 | base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(30)); |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 98 | |
| 99 | watcher.StopWatching(); |
| 100 | |
[email protected] | f7b98b3 | 2013-02-05 08:14:15 | [diff] [blame] | 101 | RunLoop().RunUntilIdle(); |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 102 | |
| 103 | // Our delegate should not have fired. |
| 104 | EXPECT_EQ(1, counter); |
| 105 | } |
| 106 | |
| 107 | void RunTest_OutlivesMessageLoop(MessageLoop::Type message_loop_type) { |
| 108 | // Simulate a MessageLoop that dies before an WaitableEventWatcher. This |
| 109 | // ordinarily doesn't happen when people use the Thread class, but it can |
| 110 | // happen when people use the Singleton pattern or atexit. |
| 111 | WaitableEvent event(true, false); |
| 112 | { |
| 113 | WaitableEventWatcher watcher; |
| 114 | { |
| 115 | MessageLoop message_loop(message_loop_type); |
| 116 | |
[email protected] | 329be05 | 2013-02-04 18:14:28 | [diff] [blame] | 117 | watcher.StartWatching(&event, Bind(&QuitWhenSignaled)); |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 118 | } |
| 119 | } |
| 120 | } |
| 121 | |
[email protected] | c891ab9 | 2009-03-26 18:28:19 | [diff] [blame] | 122 | void RunTest_DeleteUnder(MessageLoop::Type message_loop_type) { |
| 123 | // Delete the WaitableEvent out from under the Watcher. This is explictly |
| 124 | // allowed by the interface. |
| 125 | |
| 126 | MessageLoop message_loop(message_loop_type); |
| 127 | |
| 128 | { |
| 129 | WaitableEventWatcher watcher; |
| 130 | |
| 131 | WaitableEvent* event = new WaitableEvent(false, false); |
[email protected] | 329be05 | 2013-02-04 18:14:28 | [diff] [blame] | 132 | |
| 133 | watcher.StartWatching(event, Bind(&QuitWhenSignaled)); |
[email protected] | c891ab9 | 2009-03-26 18:28:19 | [diff] [blame] | 134 | delete event; |
| 135 | } |
| 136 | } |
| 137 | |
[email protected] | b57d33c5 | 2009-01-15 22:58:53 | [diff] [blame] | 138 | } // namespace |
| 139 | |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 140 | //----------------------------------------------------------------------------- |
| 141 | |
[email protected] | 9480d2ab | 2009-01-15 22:52:38 | [diff] [blame] | 142 | TEST(WaitableEventWatcherTest, BasicSignal) { |
[email protected] | 840246b | 2012-07-18 08:06:50 | [diff] [blame] | 143 | for (int i = 0; i < kNumTestingMessageLoops; i++) { |
| 144 | RunTest_BasicSignal(testing_message_loops[i]); |
| 145 | } |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 146 | } |
| 147 | |
[email protected] | 9480d2ab | 2009-01-15 22:52:38 | [diff] [blame] | 148 | TEST(WaitableEventWatcherTest, BasicCancel) { |
[email protected] | 840246b | 2012-07-18 08:06:50 | [diff] [blame] | 149 | for (int i = 0; i < kNumTestingMessageLoops; i++) { |
| 150 | RunTest_BasicCancel(testing_message_loops[i]); |
| 151 | } |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 152 | } |
| 153 | |
[email protected] | 9480d2ab | 2009-01-15 22:52:38 | [diff] [blame] | 154 | TEST(WaitableEventWatcherTest, CancelAfterSet) { |
[email protected] | 840246b | 2012-07-18 08:06:50 | [diff] [blame] | 155 | for (int i = 0; i < kNumTestingMessageLoops; i++) { |
| 156 | RunTest_CancelAfterSet(testing_message_loops[i]); |
| 157 | } |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 158 | } |
| 159 | |
[email protected] | 9480d2ab | 2009-01-15 22:52:38 | [diff] [blame] | 160 | TEST(WaitableEventWatcherTest, OutlivesMessageLoop) { |
[email protected] | 840246b | 2012-07-18 08:06:50 | [diff] [blame] | 161 | for (int i = 0; i < kNumTestingMessageLoops; i++) { |
| 162 | RunTest_OutlivesMessageLoop(testing_message_loops[i]); |
| 163 | } |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 164 | } |
[email protected] | c891ab9 | 2009-03-26 18:28:19 | [diff] [blame] | 165 | |
[email protected] | a18194a | 2010-11-05 22:03:31 | [diff] [blame] | 166 | #if defined(OS_WIN) |
| 167 | // Crashes sometimes on vista. http://crbug.com/62119 |
| 168 | #define MAYBE_DeleteUnder DISABLED_DeleteUnder |
| 169 | #else |
| 170 | #define MAYBE_DeleteUnder DeleteUnder |
| 171 | #endif |
| 172 | TEST(WaitableEventWatcherTest, MAYBE_DeleteUnder) { |
[email protected] | 840246b | 2012-07-18 08:06:50 | [diff] [blame] | 173 | for (int i = 0; i < kNumTestingMessageLoops; i++) { |
| 174 | RunTest_DeleteUnder(testing_message_loops[i]); |
| 175 | } |
[email protected] | c891ab9 | 2009-03-26 18:28:19 | [diff] [blame] | 176 | } |
[email protected] | 44f9c95 | 2011-01-02 06:05:39 | [diff] [blame] | 177 | |
| 178 | } // namespace base |