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