Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(353)

Side by Side Diff: base/synchronization/waitable_event_watcher_unittest.cc

Issue 2839213002: [Synchronization] Fix a crash in WaitableEventWatcher (Closed)
Patch Set: Fix grammar in comments Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/synchronization/waitable_event_watcher.h" 5 #include "base/synchronization/waitable_event_watcher.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/callback.h" 8 #include "base/callback.h"
9 #include "base/macros.h" 9 #include "base/macros.h"
10 #include "base/memory/ptr_util.h"
10 #include "base/message_loop/message_loop.h" 11 #include "base/message_loop/message_loop.h"
11 #include "base/run_loop.h" 12 #include "base/run_loop.h"
12 #include "base/synchronization/waitable_event.h" 13 #include "base/synchronization/waitable_event.h"
13 #include "base/threading/platform_thread.h" 14 #include "base/threading/platform_thread.h"
15 #include "base/threading/sequenced_task_runner_handle.h"
14 #include "build/build_config.h" 16 #include "build/build_config.h"
15 #include "testing/gtest/include/gtest/gtest.h" 17 #include "testing/gtest/include/gtest/gtest.h"
16 18
17 namespace base { 19 namespace base {
18 20
19 namespace { 21 namespace {
20 22
21 // The message loops on which each waitable event timer should be tested. 23 // The message loops on which each waitable event timer should be tested.
22 const MessageLoop::Type testing_message_loops[] = { 24 const MessageLoop::Type testing_message_loops[] = {
23 MessageLoop::TYPE_DEFAULT, 25 MessageLoop::TYPE_DEFAULT,
24 MessageLoop::TYPE_IO, 26 MessageLoop::TYPE_IO,
25 #if !defined(OS_IOS) // iOS does not allow direct running of the UI loop. 27 #if !defined(OS_IOS) // iOS does not allow direct running of the UI loop.
26 MessageLoop::TYPE_UI, 28 MessageLoop::TYPE_UI,
27 #endif 29 #endif
28 }; 30 };
29 31
30 const int kNumTestingMessageLoops = arraysize(testing_message_loops); 32 const int kNumTestingMessageLoops = arraysize(testing_message_loops);
31 33
32 void QuitWhenSignaled(WaitableEvent* event) { 34 void QuitWhenSignaled(WaitableEvent* event) {
33 MessageLoop::current()->QuitWhenIdle(); 35 MessageLoop::current()->QuitWhenIdle();
34 } 36 }
35 37
36 class DecrementCountContainer { 38 class DecrementCountContainer {
37 public: 39 public:
38 explicit DecrementCountContainer(int* counter) : counter_(counter) { 40 explicit DecrementCountContainer(int* counter) : counter_(counter) {
39 } 41 }
40 void OnWaitableEventSignaled(WaitableEvent* object) { 42 void OnWaitableEventSignaled(WaitableEvent* object) {
43 // NOTE: |object| may be already deleted.
41 --(*counter_); 44 --(*counter_);
42 } 45 }
43 private: 46 private:
44 int* counter_; 47 int* counter_;
45 }; 48 };
46 49
47 void RunTest_BasicSignal(MessageLoop::Type message_loop_type) { 50 void RunTest_BasicSignal(MessageLoop::Type message_loop_type) {
48 MessageLoop message_loop(message_loop_type); 51 MessageLoop message_loop(message_loop_type);
49 52
50 // A manual-reset event that is not yet signaled. 53 // A manual-reset event that is not yet signaled.
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
110 { 113 {
111 WaitableEventWatcher watcher; 114 WaitableEventWatcher watcher;
112 { 115 {
113 MessageLoop message_loop(message_loop_type); 116 MessageLoop message_loop(message_loop_type);
114 117
115 watcher.StartWatching(&event, BindOnce(&QuitWhenSignaled)); 118 watcher.StartWatching(&event, BindOnce(&QuitWhenSignaled));
116 } 119 }
117 } 120 }
118 } 121 }
119 122
120 void RunTest_DeleteUnder(MessageLoop::Type message_loop_type) { 123 void RunTest_DeleteUnder(MessageLoop::Type message_loop_type,
124 bool delay_after_delete) {
121 // Delete the WaitableEvent out from under the Watcher. This is explictly 125 // Delete the WaitableEvent out from under the Watcher. This is explictly
122 // allowed by the interface. 126 // allowed by the interface.
123 127
124 MessageLoop message_loop(message_loop_type); 128 MessageLoop message_loop(message_loop_type);
125 129
126 { 130 {
127 WaitableEventWatcher watcher; 131 WaitableEventWatcher watcher;
128 132
129 WaitableEvent* event = 133 auto* event = new WaitableEvent(WaitableEvent::ResetPolicy::AUTOMATIC,
130 new WaitableEvent(WaitableEvent::ResetPolicy::AUTOMATIC, 134 WaitableEvent::InitialState::NOT_SIGNALED);
131 WaitableEvent::InitialState::NOT_SIGNALED);
132 135
133 watcher.StartWatching(event, BindOnce(&QuitWhenSignaled)); 136 watcher.StartWatching(event, BindOnce(&QuitWhenSignaled));
137
138 if (delay_after_delete) {
139 // On Windows that sleep() improves the chance to catch some problems.
140 // It postpones the dtor |watcher| (which immediately cancel the waiting)
141 // and gives some time to run to a created background thread.
142 // Unfortunately, that thread is under OS control and we can't
143 // manipulate it directly.
144 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(30));
145 }
146
134 delete event; 147 delete event;
135 } 148 }
136 } 149 }
137 150
151 void RunTest_SignalAndDelete(MessageLoop::Type message_loop_type,
152 bool delay_after_delete) {
153 // Signal and immediately delete the WaitableEvent out from under the Watcher.
154
155 MessageLoop message_loop(message_loop_type);
156
157 {
158 WaitableEventWatcher watcher;
159
160 auto event = base::MakeUnique<WaitableEvent>(
161 WaitableEvent::ResetPolicy::AUTOMATIC,
162 WaitableEvent::InitialState::NOT_SIGNALED);
163
164 watcher.StartWatching(event.get(), BindOnce(&QuitWhenSignaled));
165 event->Signal();
166 event.reset();
167
168 if (delay_after_delete) {
169 // On Windows that sleep() improves the chance to catch some problems.
170 // It postpones the dtor |watcher| (which immediately cancel the waiting)
171 // and gives some time to run to a created background thread.
172 // Unfortunately, that thread is under OS control and we can't
173 // manipulate it directly.
174 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(30));
175 }
176
177 // Wait for the watcher callback.
178 RunLoop().Run();
179 }
180 }
181
138 } // namespace 182 } // namespace
139 183
140 //----------------------------------------------------------------------------- 184 //-----------------------------------------------------------------------------
141 185
142 TEST(WaitableEventWatcherTest, BasicSignal) { 186 TEST(WaitableEventWatcherTest, BasicSignal) {
143 for (int i = 0; i < kNumTestingMessageLoops; i++) { 187 for (int i = 0; i < kNumTestingMessageLoops; i++) {
144 RunTest_BasicSignal(testing_message_loops[i]); 188 RunTest_BasicSignal(testing_message_loops[i]);
145 } 189 }
146 } 190 }
147 191
(...skipping 10 matching lines...) Expand all
158 } 202 }
159 203
160 TEST(WaitableEventWatcherTest, OutlivesMessageLoop) { 204 TEST(WaitableEventWatcherTest, OutlivesMessageLoop) {
161 for (int i = 0; i < kNumTestingMessageLoops; i++) { 205 for (int i = 0; i < kNumTestingMessageLoops; i++) {
162 RunTest_OutlivesMessageLoop(testing_message_loops[i]); 206 RunTest_OutlivesMessageLoop(testing_message_loops[i]);
163 } 207 }
164 } 208 }
165 209
166 TEST(WaitableEventWatcherTest, DeleteUnder) { 210 TEST(WaitableEventWatcherTest, DeleteUnder) {
167 for (int i = 0; i < kNumTestingMessageLoops; i++) { 211 for (int i = 0; i < kNumTestingMessageLoops; i++) {
168 RunTest_DeleteUnder(testing_message_loops[i]); 212 RunTest_DeleteUnder(testing_message_loops[i], false);
213 RunTest_DeleteUnder(testing_message_loops[i], true);
169 } 214 }
170 } 215 }
171 216
217 TEST(WaitableEventWatcherTest, SignalAndDelete) {
218 for (int i = 0; i < kNumTestingMessageLoops; i++) {
219 RunTest_SignalAndDelete(testing_message_loops[i], false);
220 RunTest_SignalAndDelete(testing_message_loops[i], true);
221 }
222 }
223
172 } // namespace base 224 } // namespace base
OLDNEW
« no previous file with comments | « base/synchronization/waitable_event_watcher.h ('k') | base/synchronization/waitable_event_watcher_win.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698