[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 | |
| 5 | #include "base/message_loop.h" |
[email protected] | 44f9c95 | 2011-01-02 06:05:39 | [diff] [blame] | 6 | #include "base/synchronization/waitable_event.h" |
| 7 | #include "base/synchronization/waitable_event_watcher.h" |
[email protected] | ce072a7 | 2010-12-31 20:02:16 | [diff] [blame] | 8 | #include "base/threading/platform_thread.h" |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 9 | #include "testing/gtest/include/gtest/gtest.h" |
| 10 | |
[email protected] | 44f9c95 | 2011-01-02 06:05:39 | [diff] [blame] | 11 | namespace base { |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 12 | |
| 13 | namespace { |
| 14 | |
[email protected] | 840246b | 2012-07-18 08:06:50 | [diff] [blame^] | 15 | // The message loops on which each waitable event timer should be tested. |
| 16 | const MessageLoop::Type testing_message_loops[] = { |
| 17 | MessageLoop::TYPE_DEFAULT, |
| 18 | MessageLoop::TYPE_IO, |
| 19 | #if !defined(OS_IOS) // iOS does not allow direct running of the UI loop. |
| 20 | MessageLoop::TYPE_UI, |
| 21 | #endif |
| 22 | }; |
| 23 | |
| 24 | const int kNumTestingMessageLoops = arraysize(testing_message_loops); |
| 25 | |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 26 | class QuitDelegate : public WaitableEventWatcher::Delegate { |
| 27 | public: |
[email protected] | 4410618 | 2012-04-06 03:53:02 | [diff] [blame] | 28 | virtual void OnWaitableEventSignaled(WaitableEvent* event) OVERRIDE { |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 29 | MessageLoop::current()->Quit(); |
| 30 | } |
| 31 | }; |
| 32 | |
| 33 | class DecrementCountDelegate : public WaitableEventWatcher::Delegate { |
| 34 | public: |
[email protected] | 2fdc86a | 2010-01-26 23:08:02 | [diff] [blame] | 35 | explicit DecrementCountDelegate(int* counter) : counter_(counter) { |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 36 | } |
[email protected] | 4410618 | 2012-04-06 03:53:02 | [diff] [blame] | 37 | virtual void OnWaitableEventSignaled(WaitableEvent* object) OVERRIDE { |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 38 | --(*counter_); |
| 39 | } |
| 40 | private: |
| 41 | int* counter_; |
| 42 | }; |
| 43 | |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 44 | void RunTest_BasicSignal(MessageLoop::Type message_loop_type) { |
| 45 | MessageLoop message_loop(message_loop_type); |
| 46 | |
| 47 | // A manual-reset event that is not yet signaled. |
| 48 | WaitableEvent event(true, false); |
| 49 | |
| 50 | WaitableEventWatcher watcher; |
[email protected] | 9b6fee1 | 2009-09-29 18:13:07 | [diff] [blame] | 51 | EXPECT_TRUE(watcher.GetWatchedEvent() == NULL); |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 52 | |
| 53 | QuitDelegate delegate; |
| 54 | watcher.StartWatching(&event, &delegate); |
[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 | |
| 72 | QuitDelegate delegate; |
| 73 | watcher.StartWatching(&event, &delegate); |
| 74 | |
| 75 | watcher.StopWatching(); |
| 76 | } |
| 77 | |
| 78 | void RunTest_CancelAfterSet(MessageLoop::Type message_loop_type) { |
| 79 | MessageLoop message_loop(message_loop_type); |
| 80 | |
| 81 | // A manual-reset event that is not yet signaled. |
| 82 | WaitableEvent event(true, false); |
| 83 | |
| 84 | WaitableEventWatcher watcher; |
| 85 | |
| 86 | int counter = 1; |
| 87 | DecrementCountDelegate delegate(&counter); |
| 88 | |
| 89 | watcher.StartWatching(&event, &delegate); |
| 90 | |
| 91 | event.Signal(); |
| 92 | |
| 93 | // Let the background thread do its business |
[email protected] | a1b75b94 | 2011-12-31 22:53:51 | [diff] [blame] | 94 | base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(30)); |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 95 | |
| 96 | watcher.StopWatching(); |
| 97 | |
| 98 | MessageLoop::current()->RunAllPending(); |
| 99 | |
| 100 | // Our delegate should not have fired. |
| 101 | EXPECT_EQ(1, counter); |
| 102 | } |
| 103 | |
| 104 | void RunTest_OutlivesMessageLoop(MessageLoop::Type message_loop_type) { |
| 105 | // Simulate a MessageLoop that dies before an WaitableEventWatcher. This |
| 106 | // ordinarily doesn't happen when people use the Thread class, but it can |
| 107 | // happen when people use the Singleton pattern or atexit. |
| 108 | WaitableEvent event(true, false); |
| 109 | { |
| 110 | WaitableEventWatcher watcher; |
| 111 | { |
| 112 | MessageLoop message_loop(message_loop_type); |
| 113 | |
| 114 | QuitDelegate delegate; |
| 115 | watcher.StartWatching(&event, &delegate); |
| 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); |
| 130 | QuitDelegate delegate; |
| 131 | watcher.StartWatching(event, &delegate); |
| 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 |