blob: 8fa4e7b242568efe4a226fb41f65159b21023252 [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
5#include "base/message_loop.h"
[email protected]44f9c952011-01-02 06:05:396#include "base/synchronization/waitable_event.h"
7#include "base/synchronization/waitable_event_watcher.h"
[email protected]ce072a72010-12-31 20:02:168#include "base/threading/platform_thread.h"
[email protected]1c4947f2009-01-15 22:25:119#include "testing/gtest/include/gtest/gtest.h"
10
[email protected]44f9c952011-01-02 06:05:3911namespace base {
[email protected]1c4947f2009-01-15 22:25:1112
13namespace {
14
[email protected]840246b2012-07-18 08:06:5015// The message loops on which each waitable event timer should be tested.
16const 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
24const int kNumTestingMessageLoops = arraysize(testing_message_loops);
25
[email protected]1c4947f2009-01-15 22:25:1126class QuitDelegate : public WaitableEventWatcher::Delegate {
27 public:
[email protected]44106182012-04-06 03:53:0228 virtual void OnWaitableEventSignaled(WaitableEvent* event) OVERRIDE {
[email protected]1c4947f2009-01-15 22:25:1129 MessageLoop::current()->Quit();
30 }
31};
32
33class DecrementCountDelegate : public WaitableEventWatcher::Delegate {
34 public:
[email protected]2fdc86a2010-01-26 23:08:0235 explicit DecrementCountDelegate(int* counter) : counter_(counter) {
[email protected]1c4947f2009-01-15 22:25:1136 }
[email protected]44106182012-04-06 03:53:0237 virtual void OnWaitableEventSignaled(WaitableEvent* object) OVERRIDE {
[email protected]1c4947f2009-01-15 22:25:1138 --(*counter_);
39 }
40 private:
41 int* counter_;
42};
43
[email protected]1c4947f2009-01-15 22:25:1144void 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]9b6fee12009-09-29 18:13:0751 EXPECT_TRUE(watcher.GetWatchedEvent() == NULL);
[email protected]1c4947f2009-01-15 22:25:1152
53 QuitDelegate delegate;
54 watcher.StartWatching(&event, &delegate);
[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
72 QuitDelegate delegate;
73 watcher.StartWatching(&event, &delegate);
74
75 watcher.StopWatching();
76}
77
78void 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]a1b75b942011-12-31 22:53:5194 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(30));
[email protected]1c4947f2009-01-15 22:25:1195
96 watcher.StopWatching();
97
98 MessageLoop::current()->RunAllPending();
99
100 // Our delegate should not have fired.
101 EXPECT_EQ(1, counter);
102}
103
104void 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]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);
130 QuitDelegate delegate;
131 watcher.StartWatching(event, &delegate);
132 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