blob: 5319d1efba3ce3b426d472b28378baf06a2fa549 [file] [log] [blame]
[email protected]44106182012-04-06 03:53:021// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]1c4947f2009-01-15 22:25:112// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]f7b98b32013-02-05 08:14:155#include "base/synchronization/waitable_event_watcher.h"
6
[email protected]329be052013-02-04 18:14:287#include "base/bind.h"
8#include "base/callback.h"
[email protected]495cad92013-07-18 08:12:409#include "base/message_loop/message_loop.h"
[email protected]f7b98b32013-02-05 08:14:1510#include "base/run_loop.h"
[email protected]44f9c952011-01-02 06:05:3911#include "base/synchronization/waitable_event.h"
[email protected]ce072a72010-12-31 20:02:1612#include "base/threading/platform_thread.h"
[email protected]1c4947f2009-01-15 22:25:1113#include "testing/gtest/include/gtest/gtest.h"
14
[email protected]44f9c952011-01-02 06:05:3915namespace base {
[email protected]1c4947f2009-01-15 22:25:1116
17namespace {
18
[email protected]840246b2012-07-18 08:06:5019// The message loops on which each waitable event timer should be tested.
20const 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
28const int kNumTestingMessageLoops = arraysize(testing_message_loops);
29
[email protected]329be052013-02-04 18:14:2830void QuitWhenSignaled(WaitableEvent* event) {
31 MessageLoop::current()->QuitWhenIdle();
32}
[email protected]1eaa54792013-01-31 23:14:2733
[email protected]329be052013-02-04 18:14:2834class DecrementCountContainer {
[email protected]1eaa54792013-01-31 23:14:2735 public:
[email protected]329be052013-02-04 18:14:2836 explicit DecrementCountContainer(int* counter) : counter_(counter) {
[email protected]1eaa54792013-01-31 23:14:2737 }
[email protected]329be052013-02-04 18:14:2838 void OnWaitableEventSignaled(WaitableEvent* object) {
[email protected]1c4947f2009-01-15 22:25:1139 --(*counter_);
40 }
41 private:
42 int* counter_;
43};
44
[email protected]1c4947f2009-01-15 22:25:1145void 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]9b6fee12009-09-29 18:13:0752 EXPECT_TRUE(watcher.GetWatchedEvent() == NULL);
[email protected]1c4947f2009-01-15 22:25:1153
[email protected]329be052013-02-04 18:14:2854 watcher.StartWatching(&event, Bind(&QuitWhenSignaled));
[email protected]5b7a6ce2009-01-15 22:31:1755 EXPECT_EQ(&event, watcher.GetWatchedEvent());
[email protected]1c4947f2009-01-15 22:25:1156
57 event.Signal();
58
59 MessageLoop::current()->Run();
60
[email protected]9b6fee12009-09-29 18:13:0761 EXPECT_TRUE(watcher.GetWatchedEvent() == NULL);
[email protected]1c4947f2009-01-15 22:25:1162}
63
64void 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]329be052013-02-04 18:14:2872 watcher.StartWatching(&event, Bind(&QuitWhenSignaled));
[email protected]1c4947f2009-01-15 22:25:1173
74 watcher.StopWatching();
75}
76
77void 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]329be052013-02-04 18:14:2886 DecrementCountContainer delegate(&counter);
87 WaitableEventWatcher::EventCallback callback =
88 Bind(&DecrementCountContainer::OnWaitableEventSignaled,
89 Unretained(&delegate));
90 watcher.StartWatching(&event, callback);
[email protected]1c4947f2009-01-15 22:25:1191
92 event.Signal();
93
94 // Let the background thread do its business
[email protected]a1b75b942011-12-31 22:53:5195 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(30));
[email protected]1c4947f2009-01-15 22:25:1196
97 watcher.StopWatching();
98
[email protected]f7b98b32013-02-05 08:14:1599 RunLoop().RunUntilIdle();
[email protected]1c4947f2009-01-15 22:25:11100
101 // Our delegate should not have fired.
102 EXPECT_EQ(1, counter);
103}
104
105void 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]329be052013-02-04 18:14:28115 watcher.StartWatching(&event, Bind(&QuitWhenSignaled));
[email protected]1c4947f2009-01-15 22:25:11116 }
117 }
118}
119
[email protected]c891ab92009-03-26 18:28:19120void 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]329be052013-02-04 18:14:28130
131 watcher.StartWatching(event, Bind(&QuitWhenSignaled));
[email protected]c891ab92009-03-26 18:28:19132 delete event;
133 }
134}
135
[email protected]b57d33c52009-01-15 22:58:53136} // namespace
137
[email protected]1c4947f2009-01-15 22:25:11138//-----------------------------------------------------------------------------
139
[email protected]9480d2ab2009-01-15 22:52:38140TEST(WaitableEventWatcherTest, BasicSignal) {
[email protected]840246b2012-07-18 08:06:50141 for (int i = 0; i < kNumTestingMessageLoops; i++) {
142 RunTest_BasicSignal(testing_message_loops[i]);
143 }
[email protected]1c4947f2009-01-15 22:25:11144}
145
[email protected]9480d2ab2009-01-15 22:52:38146TEST(WaitableEventWatcherTest, BasicCancel) {
[email protected]840246b2012-07-18 08:06:50147 for (int i = 0; i < kNumTestingMessageLoops; i++) {
148 RunTest_BasicCancel(testing_message_loops[i]);
149 }
[email protected]1c4947f2009-01-15 22:25:11150}
151
[email protected]9480d2ab2009-01-15 22:52:38152TEST(WaitableEventWatcherTest, CancelAfterSet) {
[email protected]840246b2012-07-18 08:06:50153 for (int i = 0; i < kNumTestingMessageLoops; i++) {
154 RunTest_CancelAfterSet(testing_message_loops[i]);
155 }
[email protected]1c4947f2009-01-15 22:25:11156}
157
[email protected]9480d2ab2009-01-15 22:52:38158TEST(WaitableEventWatcherTest, OutlivesMessageLoop) {
[email protected]840246b2012-07-18 08:06:50159 for (int i = 0; i < kNumTestingMessageLoops; i++) {
160 RunTest_OutlivesMessageLoop(testing_message_loops[i]);
161 }
[email protected]1c4947f2009-01-15 22:25:11162}
[email protected]c891ab92009-03-26 18:28:19163
[email protected]a18194a2010-11-05 22:03:31164#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
170TEST(WaitableEventWatcherTest, MAYBE_DeleteUnder) {
[email protected]840246b2012-07-18 08:06:50171 for (int i = 0; i < kNumTestingMessageLoops; i++) {
172 RunTest_DeleteUnder(testing_message_loops[i]);
173 }
[email protected]c891ab92009-03-26 18:28:19174}
[email protected]44f9c952011-01-02 06:05:39175
176} // namespace base