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

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

Issue 12087120: Revert 179987 (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 7 years, 10 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 | Annotate | Revision Log
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/bind.h"
6 #include "base/callback.h"
7 #include "base/message_loop.h" 5 #include "base/message_loop.h"
8 #include "base/synchronization/waitable_event.h" 6 #include "base/synchronization/waitable_event.h"
9 #include "base/synchronization/waitable_event_watcher.h" 7 #include "base/synchronization/waitable_event_watcher.h"
10 #include "base/threading/platform_thread.h" 8 #include "base/threading/platform_thread.h"
11 #include "testing/gtest/include/gtest/gtest.h" 9 #include "testing/gtest/include/gtest/gtest.h"
12 10
13 namespace base { 11 namespace base {
14 12
15 namespace { 13 namespace {
16 14
17 // The message loops on which each waitable event timer should be tested. 15 // The message loops on which each waitable event timer should be tested.
18 const MessageLoop::Type testing_message_loops[] = { 16 const MessageLoop::Type testing_message_loops[] = {
19 MessageLoop::TYPE_DEFAULT, 17 MessageLoop::TYPE_DEFAULT,
20 MessageLoop::TYPE_IO, 18 MessageLoop::TYPE_IO,
21 #if !defined(OS_IOS) // iOS does not allow direct running of the UI loop. 19 #if !defined(OS_IOS) // iOS does not allow direct running of the UI loop.
22 MessageLoop::TYPE_UI, 20 MessageLoop::TYPE_UI,
23 #endif 21 #endif
24 }; 22 };
25 23
26 const int kNumTestingMessageLoops = arraysize(testing_message_loops); 24 const int kNumTestingMessageLoops = arraysize(testing_message_loops);
27 25
28 void QuitWhenSignaled(WaitableEvent* event) { 26 class QuitDelegate : public WaitableEventWatcher::Delegate {
29 MessageLoop::current()->QuitWhenIdle(); 27 public:
30 } 28 virtual void OnWaitableEventSignaled(WaitableEvent* event) OVERRIDE {
29 MessageLoop::current()->QuitWhenIdle();
30 }
31 };
31 32
32 class DecrementCountContainer { 33 class DecrementCountDelegate : public WaitableEventWatcher::Delegate {
33 public: 34 public:
34 explicit DecrementCountContainer(int* counter) : counter_(counter) { 35 explicit DecrementCountDelegate(int* counter) : counter_(counter) {
35 } 36 }
36 void OnWaitableEventSignaled(WaitableEvent* object) { 37 virtual void OnWaitableEventSignaled(WaitableEvent* object) OVERRIDE {
37 --(*counter_); 38 --(*counter_);
38 } 39 }
39 private: 40 private:
40 int* counter_; 41 int* counter_;
41 }; 42 };
42 43
43 void RunTest_BasicSignal(MessageLoop::Type message_loop_type) { 44 void RunTest_BasicSignal(MessageLoop::Type message_loop_type) {
44 MessageLoop message_loop(message_loop_type); 45 MessageLoop message_loop(message_loop_type);
45 46
46 // A manual-reset event that is not yet signaled. 47 // A manual-reset event that is not yet signaled.
47 WaitableEvent event(true, false); 48 WaitableEvent event(true, false);
48 49
49 WaitableEventWatcher watcher; 50 WaitableEventWatcher watcher;
50 EXPECT_TRUE(watcher.GetWatchedEvent() == NULL); 51 EXPECT_TRUE(watcher.GetWatchedEvent() == NULL);
51 52
52 watcher.StartWatching(&event, Bind(&QuitWhenSignaled)); 53 QuitDelegate delegate;
54 watcher.StartWatching(&event, &delegate);
53 EXPECT_EQ(&event, watcher.GetWatchedEvent()); 55 EXPECT_EQ(&event, watcher.GetWatchedEvent());
54 56
55 event.Signal(); 57 event.Signal();
56 58
57 MessageLoop::current()->Run(); 59 MessageLoop::current()->Run();
58 60
59 EXPECT_TRUE(watcher.GetWatchedEvent() == NULL); 61 EXPECT_TRUE(watcher.GetWatchedEvent() == NULL);
60 } 62 }
61 63
62 void RunTest_BasicCancel(MessageLoop::Type message_loop_type) { 64 void RunTest_BasicCancel(MessageLoop::Type message_loop_type) {
63 MessageLoop message_loop(message_loop_type); 65 MessageLoop message_loop(message_loop_type);
64 66
65 // A manual-reset event that is not yet signaled. 67 // A manual-reset event that is not yet signaled.
66 WaitableEvent event(true, false); 68 WaitableEvent event(true, false);
67 69
68 WaitableEventWatcher watcher; 70 WaitableEventWatcher watcher;
69 71
70 watcher.StartWatching(&event, Bind(&QuitWhenSignaled)); 72 QuitDelegate delegate;
73 watcher.StartWatching(&event, &delegate);
71 74
72 watcher.StopWatching(); 75 watcher.StopWatching();
73 } 76 }
74 77
75 void RunTest_CancelAfterSet(MessageLoop::Type message_loop_type) { 78 void RunTest_CancelAfterSet(MessageLoop::Type message_loop_type) {
76 MessageLoop message_loop(message_loop_type); 79 MessageLoop message_loop(message_loop_type);
77 80
78 // A manual-reset event that is not yet signaled. 81 // A manual-reset event that is not yet signaled.
79 WaitableEvent event(true, false); 82 WaitableEvent event(true, false);
80 83
81 WaitableEventWatcher watcher; 84 WaitableEventWatcher watcher;
82 85
83 int counter = 1; 86 int counter = 1;
84 DecrementCountContainer delegate(&counter); 87 DecrementCountDelegate delegate(&counter);
85 WaitableEventWatcher::EventCallback callback = 88
86 Bind(&DecrementCountContainer::OnWaitableEventSignaled, 89 watcher.StartWatching(&event, &delegate);
87 Unretained(&delegate));
88 watcher.StartWatching(&event, callback);
89 90
90 event.Signal(); 91 event.Signal();
91 92
92 // Let the background thread do its business 93 // Let the background thread do its business
93 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(30)); 94 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(30));
94 95
95 watcher.StopWatching(); 96 watcher.StopWatching();
96 97
97 MessageLoop::current()->RunUntilIdle(); 98 MessageLoop::current()->RunUntilIdle();
98 99
99 // Our delegate should not have fired. 100 // Our delegate should not have fired.
100 EXPECT_EQ(1, counter); 101 EXPECT_EQ(1, counter);
101 } 102 }
102 103
103 void RunTest_OutlivesMessageLoop(MessageLoop::Type message_loop_type) { 104 void RunTest_OutlivesMessageLoop(MessageLoop::Type message_loop_type) {
104 // Simulate a MessageLoop that dies before an WaitableEventWatcher. This 105 // Simulate a MessageLoop that dies before an WaitableEventWatcher. This
105 // ordinarily doesn't happen when people use the Thread class, but it can 106 // ordinarily doesn't happen when people use the Thread class, but it can
106 // happen when people use the Singleton pattern or atexit. 107 // happen when people use the Singleton pattern or atexit.
107 WaitableEvent event(true, false); 108 WaitableEvent event(true, false);
108 { 109 {
109 WaitableEventWatcher watcher; 110 WaitableEventWatcher watcher;
110 { 111 {
111 MessageLoop message_loop(message_loop_type); 112 MessageLoop message_loop(message_loop_type);
112 113
113 watcher.StartWatching(&event, Bind(&QuitWhenSignaled)); 114 QuitDelegate delegate;
115 watcher.StartWatching(&event, &delegate);
114 } 116 }
115 } 117 }
116 } 118 }
117 119
118 void RunTest_DeleteUnder(MessageLoop::Type message_loop_type) { 120 void RunTest_DeleteUnder(MessageLoop::Type message_loop_type) {
119 // Delete the WaitableEvent out from under the Watcher. This is explictly 121 // Delete the WaitableEvent out from under the Watcher. This is explictly
120 // allowed by the interface. 122 // allowed by the interface.
121 123
122 MessageLoop message_loop(message_loop_type); 124 MessageLoop message_loop(message_loop_type);
123 125
124 { 126 {
125 WaitableEventWatcher watcher; 127 WaitableEventWatcher watcher;
126 128
127 WaitableEvent* event = new WaitableEvent(false, false); 129 WaitableEvent* event = new WaitableEvent(false, false);
128 130 QuitDelegate delegate;
129 watcher.StartWatching(event, Bind(&QuitWhenSignaled)); 131 watcher.StartWatching(event, &delegate);
130 delete event; 132 delete event;
131 } 133 }
132 } 134 }
133 135
134 } // namespace 136 } // namespace
135 137
136 //----------------------------------------------------------------------------- 138 //-----------------------------------------------------------------------------
137 139
138 TEST(WaitableEventWatcherTest, BasicSignal) { 140 TEST(WaitableEventWatcherTest, BasicSignal) {
139 for (int i = 0; i < kNumTestingMessageLoops; i++) { 141 for (int i = 0; i < kNumTestingMessageLoops; i++) {
(...skipping 25 matching lines...) Expand all
165 #else 167 #else
166 #define MAYBE_DeleteUnder DeleteUnder 168 #define MAYBE_DeleteUnder DeleteUnder
167 #endif 169 #endif
168 TEST(WaitableEventWatcherTest, MAYBE_DeleteUnder) { 170 TEST(WaitableEventWatcherTest, MAYBE_DeleteUnder) {
169 for (int i = 0; i < kNumTestingMessageLoops; i++) { 171 for (int i = 0; i < kNumTestingMessageLoops; i++) {
170 RunTest_DeleteUnder(testing_message_loops[i]); 172 RunTest_DeleteUnder(testing_message_loops[i]);
171 } 173 }
172 } 174 }
173 175
174 } // namespace base 176 } // namespace base
OLDNEW
« no previous file with comments | « base/synchronization/waitable_event_watcher_posix.cc ('k') | base/synchronization/waitable_event_watcher_win.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698