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

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

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

Powered by Google App Engine
This is Rietveld 408576698