Invoke LeakSanitizer on teardown of RenderFrameImplTest
[chromium-blink-merge.git] / base / synchronization / waitable_event_watcher_unittest.cc
blob5319d1efba3ce3b426d472b28378baf06a2fa549
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "base/synchronization/waitable_event_watcher.h"
7 #include "base/bind.h"
8 #include "base/callback.h"
9 #include "base/message_loop/message_loop.h"
10 #include "base/run_loop.h"
11 #include "base/synchronization/waitable_event.h"
12 #include "base/threading/platform_thread.h"
13 #include "testing/gtest/include/gtest/gtest.h"
15 namespace base {
17 namespace {
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
28 const int kNumTestingMessageLoops = arraysize(testing_message_loops);
30 void QuitWhenSignaled(WaitableEvent* event) {
31 MessageLoop::current()->QuitWhenIdle();
34 class DecrementCountContainer {
35 public:
36 explicit DecrementCountContainer(int* counter) : counter_(counter) {
38 void OnWaitableEventSignaled(WaitableEvent* object) {
39 --(*counter_);
41 private:
42 int* counter_;
45 void RunTest_BasicSignal(MessageLoop::Type message_loop_type) {
46 MessageLoop message_loop(message_loop_type);
48 // A manual-reset event that is not yet signaled.
49 WaitableEvent event(true, false);
51 WaitableEventWatcher watcher;
52 EXPECT_TRUE(watcher.GetWatchedEvent() == NULL);
54 watcher.StartWatching(&event, Bind(&QuitWhenSignaled));
55 EXPECT_EQ(&event, watcher.GetWatchedEvent());
57 event.Signal();
59 MessageLoop::current()->Run();
61 EXPECT_TRUE(watcher.GetWatchedEvent() == NULL);
64 void RunTest_BasicCancel(MessageLoop::Type message_loop_type) {
65 MessageLoop message_loop(message_loop_type);
67 // A manual-reset event that is not yet signaled.
68 WaitableEvent event(true, false);
70 WaitableEventWatcher watcher;
72 watcher.StartWatching(&event, Bind(&QuitWhenSignaled));
74 watcher.StopWatching();
77 void RunTest_CancelAfterSet(MessageLoop::Type message_loop_type) {
78 MessageLoop message_loop(message_loop_type);
80 // A manual-reset event that is not yet signaled.
81 WaitableEvent event(true, false);
83 WaitableEventWatcher watcher;
85 int counter = 1;
86 DecrementCountContainer delegate(&counter);
87 WaitableEventWatcher::EventCallback callback =
88 Bind(&DecrementCountContainer::OnWaitableEventSignaled,
89 Unretained(&delegate));
90 watcher.StartWatching(&event, callback);
92 event.Signal();
94 // Let the background thread do its business
95 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(30));
97 watcher.StopWatching();
99 RunLoop().RunUntilIdle();
101 // Our delegate should not have fired.
102 EXPECT_EQ(1, counter);
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);
111 WaitableEventWatcher watcher;
113 MessageLoop message_loop(message_loop_type);
115 watcher.StartWatching(&event, Bind(&QuitWhenSignaled));
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.
124 MessageLoop message_loop(message_loop_type);
127 WaitableEventWatcher watcher;
129 WaitableEvent* event = new WaitableEvent(false, false);
131 watcher.StartWatching(event, Bind(&QuitWhenSignaled));
132 delete event;
136 } // namespace
138 //-----------------------------------------------------------------------------
140 TEST(WaitableEventWatcherTest, BasicSignal) {
141 for (int i = 0; i < kNumTestingMessageLoops; i++) {
142 RunTest_BasicSignal(testing_message_loops[i]);
146 TEST(WaitableEventWatcherTest, BasicCancel) {
147 for (int i = 0; i < kNumTestingMessageLoops; i++) {
148 RunTest_BasicCancel(testing_message_loops[i]);
152 TEST(WaitableEventWatcherTest, CancelAfterSet) {
153 for (int i = 0; i < kNumTestingMessageLoops; i++) {
154 RunTest_CancelAfterSet(testing_message_loops[i]);
158 TEST(WaitableEventWatcherTest, OutlivesMessageLoop) {
159 for (int i = 0; i < kNumTestingMessageLoops; i++) {
160 RunTest_OutlivesMessageLoop(testing_message_loops[i]);
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) {
171 for (int i = 0; i < kNumTestingMessageLoops; i++) {
172 RunTest_DeleteUnder(testing_message_loops[i]);
176 } // namespace base