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

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

Issue 12094106: Refactor: Simplify WaitableEventWatcher. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
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
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 watcher.StartWatching(&event, Bind(&QuitWhenSignaled));
54 watcher.StartWatching(&event, &delegate);
55 EXPECT_EQ(&event, watcher.GetWatchedEvent()); 53 EXPECT_EQ(&event, watcher.GetWatchedEvent());
56 54
57 event.Signal(); 55 event.Signal();
58 56
59 MessageLoop::current()->Run(); 57 MessageLoop::current()->Run();
60 58
61 EXPECT_TRUE(watcher.GetWatchedEvent() == NULL); 59 EXPECT_TRUE(watcher.GetWatchedEvent() == NULL);
62 } 60 }
63 61
64 void RunTest_BasicCancel(MessageLoop::Type message_loop_type) { 62 void RunTest_BasicCancel(MessageLoop::Type message_loop_type) {
65 MessageLoop message_loop(message_loop_type); 63 MessageLoop message_loop(message_loop_type);
66 64
67 // A manual-reset event that is not yet signaled. 65 // A manual-reset event that is not yet signaled.
68 WaitableEvent event(true, false); 66 WaitableEvent event(true, false);
69 67
70 WaitableEventWatcher watcher; 68 WaitableEventWatcher watcher;
71 69
72 QuitDelegate delegate; 70 watcher.StartWatching(&event, Bind(&QuitWhenSignaled));
73 watcher.StartWatching(&event, &delegate);
74 71
75 watcher.StopWatching(); 72 watcher.StopWatching();
76 } 73 }
77 74
78 void RunTest_CancelAfterSet(MessageLoop::Type message_loop_type) { 75 void RunTest_CancelAfterSet(MessageLoop::Type message_loop_type) {
79 MessageLoop message_loop(message_loop_type); 76 MessageLoop message_loop(message_loop_type);
80 77
81 // A manual-reset event that is not yet signaled. 78 // A manual-reset event that is not yet signaled.
82 WaitableEvent event(true, false); 79 WaitableEvent event(true, false);
83 80
84 WaitableEventWatcher watcher; 81 WaitableEventWatcher watcher;
85 82
86 int counter = 1; 83 int counter = 1;
87 DecrementCountDelegate delegate(&counter); 84 DecrementCountContainer delegate(&counter);
88 85 WaitableEventWatcher::EventCallback callback =
89 watcher.StartWatching(&event, &delegate); 86 Bind(&DecrementCountContainer::OnWaitableEventSignaled,
87 Unretained(&delegate));
88 watcher.StartWatching(&event, callback);
90 89
91 event.Signal(); 90 event.Signal();
92 91
93 // Let the background thread do its business 92 // Let the background thread do its business
94 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(30)); 93 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(30));
95 94
96 watcher.StopWatching(); 95 watcher.StopWatching();
97 96
98 MessageLoop::current()->RunUntilIdle(); 97 MessageLoop::current()->RunUntilIdle();
99 98
100 // Our delegate should not have fired. 99 // Our delegate should not have fired.
101 EXPECT_EQ(1, counter); 100 EXPECT_EQ(1, counter);
102 } 101 }
103 102
104 void RunTest_OutlivesMessageLoop(MessageLoop::Type message_loop_type) { 103 void RunTest_OutlivesMessageLoop(MessageLoop::Type message_loop_type) {
105 // Simulate a MessageLoop that dies before an WaitableEventWatcher. This 104 // Simulate a MessageLoop that dies before an WaitableEventWatcher. This
106 // ordinarily doesn't happen when people use the Thread class, but it can 105 // ordinarily doesn't happen when people use the Thread class, but it can
107 // happen when people use the Singleton pattern or atexit. 106 // happen when people use the Singleton pattern or atexit.
108 WaitableEvent event(true, false); 107 WaitableEvent event(true, false);
109 { 108 {
110 WaitableEventWatcher watcher; 109 WaitableEventWatcher watcher;
111 { 110 {
112 MessageLoop message_loop(message_loop_type); 111 MessageLoop message_loop(message_loop_type);
113 112
114 QuitDelegate delegate; 113 watcher.StartWatching(&event, Bind(&QuitWhenSignaled));
115 watcher.StartWatching(&event, &delegate);
116 } 114 }
117 } 115 }
118 } 116 }
119 117
120 void RunTest_DeleteUnder(MessageLoop::Type message_loop_type) { 118 void RunTest_DeleteUnder(MessageLoop::Type message_loop_type) {
121 // Delete the WaitableEvent out from under the Watcher. This is explictly 119 // Delete the WaitableEvent out from under the Watcher. This is explictly
122 // allowed by the interface. 120 // allowed by the interface.
123 121
124 MessageLoop message_loop(message_loop_type); 122 MessageLoop message_loop(message_loop_type);
125 123
126 { 124 {
127 WaitableEventWatcher watcher; 125 WaitableEventWatcher watcher;
128 126
129 WaitableEvent* event = new WaitableEvent(false, false); 127 WaitableEvent* event = new WaitableEvent(false, false);
130 QuitDelegate delegate; 128
131 watcher.StartWatching(event, &delegate); 129 watcher.StartWatching(event, Bind(&QuitWhenSignaled));
132 delete event; 130 delete event;
133 } 131 }
134 } 132 }
135 133
136 } // namespace 134 } // namespace
137 135
138 //----------------------------------------------------------------------------- 136 //-----------------------------------------------------------------------------
139 137
140 TEST(WaitableEventWatcherTest, BasicSignal) { 138 TEST(WaitableEventWatcherTest, BasicSignal) {
141 for (int i = 0; i < kNumTestingMessageLoops; i++) { 139 for (int i = 0; i < kNumTestingMessageLoops; i++) {
(...skipping 25 matching lines...) Expand all
167 #else 165 #else
168 #define MAYBE_DeleteUnder DeleteUnder 166 #define MAYBE_DeleteUnder DeleteUnder
169 #endif 167 #endif
170 TEST(WaitableEventWatcherTest, MAYBE_DeleteUnder) { 168 TEST(WaitableEventWatcherTest, MAYBE_DeleteUnder) {
171 for (int i = 0; i < kNumTestingMessageLoops; i++) { 169 for (int i = 0; i < kNumTestingMessageLoops; i++) {
172 RunTest_DeleteUnder(testing_message_loops[i]); 170 RunTest_DeleteUnder(testing_message_loops[i]);
173 } 171 }
174 } 172 }
175 173
176 } // namespace base 174 } // 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