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

Side by Side Diff: mojo/system/simple_dispatcher_unittest.cc

Issue 281893002: Mojo: Base our epsilon timeouts off of TestTimeouts::tiny_timeout(). (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 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 | Annotate | Revision Log
« no previous file with comments | « mojo/system/message_pipe_dispatcher_unittest.cc ('k') | mojo/system/test_utils.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 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 // NOTE(vtl): Some of these tests are inherently flaky (e.g., if run on a 5 // NOTE(vtl): Some of these tests are inherently flaky (e.g., if run on a
6 // heavily-loaded system). Sorry. |kEpsilonMicros| may be increased to increase 6 // heavily-loaded system). Sorry. |test::EpsilonTimeout()| may be increased to
7 // tolerance and reduce observed flakiness. 7 // increase tolerance and reduce observed flakiness (though doing so reduces the
8 // meaningfulness of the test).
8 9
9 #include "mojo/system/simple_dispatcher.h" 10 #include "mojo/system/simple_dispatcher.h"
10 11
11 #include "base/basictypes.h" 12 #include "base/basictypes.h"
12 #include "base/logging.h" 13 #include "base/logging.h"
13 #include "base/memory/ref_counted.h" 14 #include "base/memory/ref_counted.h"
14 #include "base/memory/scoped_vector.h" 15 #include "base/memory/scoped_vector.h"
15 #include "base/synchronization/lock.h" 16 #include "base/synchronization/lock.h"
16 #include "base/threading/platform_thread.h" // For |Sleep()|. 17 #include "base/threading/platform_thread.h" // For |Sleep()|.
17 #include "base/time/time.h" 18 #include "base/time/time.h"
18 #include "mojo/system/test_utils.h" 19 #include "mojo/system/test_utils.h"
19 #include "mojo/system/waiter.h" 20 #include "mojo/system/waiter.h"
20 #include "mojo/system/waiter_test_utils.h" 21 #include "mojo/system/waiter_test_utils.h"
21 #include "testing/gtest/include/gtest/gtest.h" 22 #include "testing/gtest/include/gtest/gtest.h"
22 23
23 namespace mojo { 24 namespace mojo {
24 namespace system { 25 namespace system {
25 namespace { 26 namespace {
26 27
27 const int64_t kMicrosPerMs = 1000;
28 const int64_t kEpsilonMicros = 30 * kMicrosPerMs; // 30 ms.
29
30 class MockSimpleDispatcher : public SimpleDispatcher { 28 class MockSimpleDispatcher : public SimpleDispatcher {
31 public: 29 public:
32 MockSimpleDispatcher() 30 MockSimpleDispatcher()
33 : satisfied_flags_(MOJO_WAIT_FLAG_NONE), 31 : satisfied_flags_(MOJO_WAIT_FLAG_NONE),
34 satisfiable_flags_(MOJO_WAIT_FLAG_READABLE | MOJO_WAIT_FLAG_WRITABLE) {} 32 satisfiable_flags_(MOJO_WAIT_FLAG_READABLE | MOJO_WAIT_FLAG_WRITABLE) {}
35 33
36 void SetSatisfiedFlags(MojoWaitFlags new_satisfied_flags) { 34 void SetSatisfiedFlags(MojoWaitFlags new_satisfied_flags) {
37 base::AutoLock locker(lock()); 35 base::AutoLock locker(lock());
38 36
39 // Any new flags that are set should be satisfiable. 37 // Any new flags that are set should be satisfiable.
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
86 84
87 // Protected by |lock()|: 85 // Protected by |lock()|:
88 MojoWaitFlags satisfied_flags_; 86 MojoWaitFlags satisfied_flags_;
89 MojoWaitFlags satisfiable_flags_; 87 MojoWaitFlags satisfiable_flags_;
90 88
91 DISALLOW_COPY_AND_ASSIGN(MockSimpleDispatcher); 89 DISALLOW_COPY_AND_ASSIGN(MockSimpleDispatcher);
92 }; 90 };
93 91
94 TEST(SimpleDispatcherTest, Basic) { 92 TEST(SimpleDispatcherTest, Basic) {
95 test::Stopwatch stopwatch; 93 test::Stopwatch stopwatch;
96 int64_t elapsed_micros;
97 94
98 scoped_refptr<MockSimpleDispatcher> d(new MockSimpleDispatcher()); 95 scoped_refptr<MockSimpleDispatcher> d(new MockSimpleDispatcher());
99 Waiter w; 96 Waiter w;
100 97
101 // Try adding a readable waiter when already readable. 98 // Try adding a readable waiter when already readable.
102 w.Init(); 99 w.Init();
103 d->SetSatisfiedFlags(MOJO_WAIT_FLAG_READABLE); 100 d->SetSatisfiedFlags(MOJO_WAIT_FLAG_READABLE);
104 EXPECT_EQ(MOJO_RESULT_ALREADY_EXISTS, 101 EXPECT_EQ(MOJO_RESULT_ALREADY_EXISTS,
105 d->AddWaiter(&w, MOJO_WAIT_FLAG_READABLE, 0)); 102 d->AddWaiter(&w, MOJO_WAIT_FLAG_READABLE, 0));
106 // Shouldn't need to remove the waiter (it was not added). 103 // Shouldn't need to remove the waiter (it was not added).
107 104
108 // Wait (forever) for writable when already writable. 105 // Wait (forever) for writable when already writable.
109 w.Init(); 106 w.Init();
110 d->SetSatisfiedFlags(MOJO_WAIT_FLAG_READABLE); 107 d->SetSatisfiedFlags(MOJO_WAIT_FLAG_READABLE);
111 EXPECT_EQ(MOJO_RESULT_OK, d->AddWaiter(&w, MOJO_WAIT_FLAG_WRITABLE, 1)); 108 EXPECT_EQ(MOJO_RESULT_OK, d->AddWaiter(&w, MOJO_WAIT_FLAG_WRITABLE, 1));
112 d->SetSatisfiedFlags(MOJO_WAIT_FLAG_WRITABLE); 109 d->SetSatisfiedFlags(MOJO_WAIT_FLAG_WRITABLE);
113 stopwatch.Start(); 110 stopwatch.Start();
114 EXPECT_EQ(1, w.Wait(MOJO_DEADLINE_INDEFINITE)); 111 EXPECT_EQ(1, w.Wait(MOJO_DEADLINE_INDEFINITE));
115 elapsed_micros = stopwatch.Elapsed(); 112 EXPECT_LT(stopwatch.Elapsed(), test::EpsilonTimeout());
116 EXPECT_LT(elapsed_micros, kEpsilonMicros);
117 d->RemoveWaiter(&w); 113 d->RemoveWaiter(&w);
118 114
119 // Wait for zero time for writable when already writable. 115 // Wait for zero time for writable when already writable.
120 w.Init(); 116 w.Init();
121 d->SetSatisfiedFlags(MOJO_WAIT_FLAG_READABLE); 117 d->SetSatisfiedFlags(MOJO_WAIT_FLAG_READABLE);
122 EXPECT_EQ(MOJO_RESULT_OK, d->AddWaiter(&w, MOJO_WAIT_FLAG_WRITABLE, 2)); 118 EXPECT_EQ(MOJO_RESULT_OK, d->AddWaiter(&w, MOJO_WAIT_FLAG_WRITABLE, 2));
123 d->SetSatisfiedFlags(MOJO_WAIT_FLAG_WRITABLE); 119 d->SetSatisfiedFlags(MOJO_WAIT_FLAG_WRITABLE);
124 stopwatch.Start(); 120 stopwatch.Start();
125 EXPECT_EQ(2, w.Wait(0)); 121 EXPECT_EQ(2, w.Wait(0));
126 elapsed_micros = stopwatch.Elapsed(); 122 EXPECT_LT(stopwatch.Elapsed(), test::EpsilonTimeout());
127 EXPECT_LT(elapsed_micros, kEpsilonMicros);
128 d->RemoveWaiter(&w); 123 d->RemoveWaiter(&w);
129 124
130 // Wait for non-zero, finite time for writable when already writable. 125 // Wait for non-zero, finite time for writable when already writable.
131 w.Init(); 126 w.Init();
132 d->SetSatisfiedFlags(MOJO_WAIT_FLAG_READABLE); 127 d->SetSatisfiedFlags(MOJO_WAIT_FLAG_READABLE);
133 EXPECT_EQ(MOJO_RESULT_OK, d->AddWaiter(&w, MOJO_WAIT_FLAG_WRITABLE, 3)); 128 EXPECT_EQ(MOJO_RESULT_OK, d->AddWaiter(&w, MOJO_WAIT_FLAG_WRITABLE, 3));
134 d->SetSatisfiedFlags(MOJO_WAIT_FLAG_WRITABLE); 129 d->SetSatisfiedFlags(MOJO_WAIT_FLAG_WRITABLE);
135 stopwatch.Start(); 130 stopwatch.Start();
136 EXPECT_EQ(3, w.Wait(2 * kEpsilonMicros)); 131 EXPECT_EQ(3, w.Wait(2 * test::EpsilonTimeout().InMicroseconds()));
137 elapsed_micros = stopwatch.Elapsed(); 132 EXPECT_LT(stopwatch.Elapsed(), test::EpsilonTimeout());
138 EXPECT_LT(elapsed_micros, kEpsilonMicros);
139 d->RemoveWaiter(&w); 133 d->RemoveWaiter(&w);
140 134
141 // Wait for zero time for writable when not writable (will time out). 135 // Wait for zero time for writable when not writable (will time out).
142 w.Init(); 136 w.Init();
143 d->SetSatisfiedFlags(MOJO_WAIT_FLAG_READABLE); 137 d->SetSatisfiedFlags(MOJO_WAIT_FLAG_READABLE);
144 EXPECT_EQ(MOJO_RESULT_OK, d->AddWaiter(&w, MOJO_WAIT_FLAG_WRITABLE, 4)); 138 EXPECT_EQ(MOJO_RESULT_OK, d->AddWaiter(&w, MOJO_WAIT_FLAG_WRITABLE, 4));
145 stopwatch.Start(); 139 stopwatch.Start();
146 EXPECT_EQ(MOJO_RESULT_DEADLINE_EXCEEDED, w.Wait(0)); 140 EXPECT_EQ(MOJO_RESULT_DEADLINE_EXCEEDED, w.Wait(0));
147 elapsed_micros = stopwatch.Elapsed(); 141 EXPECT_LT(stopwatch.Elapsed(), test::EpsilonTimeout());
148 EXPECT_LT(elapsed_micros, kEpsilonMicros);
149 d->RemoveWaiter(&w); 142 d->RemoveWaiter(&w);
150 143
151 // Wait for non-zero, finite time for writable when not writable (will time 144 // Wait for non-zero, finite time for writable when not writable (will time
152 // out). 145 // out).
153 w.Init(); 146 w.Init();
154 d->SetSatisfiedFlags(MOJO_WAIT_FLAG_READABLE); 147 d->SetSatisfiedFlags(MOJO_WAIT_FLAG_READABLE);
155 EXPECT_EQ(MOJO_RESULT_OK, d->AddWaiter(&w, MOJO_WAIT_FLAG_WRITABLE, 4)); 148 EXPECT_EQ(MOJO_RESULT_OK, d->AddWaiter(&w, MOJO_WAIT_FLAG_WRITABLE, 4));
156 stopwatch.Start(); 149 stopwatch.Start();
157 EXPECT_EQ(MOJO_RESULT_DEADLINE_EXCEEDED, w.Wait(2 * kEpsilonMicros)); 150 EXPECT_EQ(MOJO_RESULT_DEADLINE_EXCEEDED,
158 elapsed_micros = stopwatch.Elapsed(); 151 w.Wait(2 * test::EpsilonTimeout().InMicroseconds()));
159 EXPECT_GT(elapsed_micros, (2-1) * kEpsilonMicros); 152 base::TimeDelta elapsed = stopwatch.Elapsed();
160 EXPECT_LT(elapsed_micros, (2+1) * kEpsilonMicros); 153 EXPECT_GT(elapsed, (2-1) * test::EpsilonTimeout());
154 EXPECT_LT(elapsed, (2+1) * test::EpsilonTimeout());
161 d->RemoveWaiter(&w); 155 d->RemoveWaiter(&w);
162 156
163 EXPECT_EQ(MOJO_RESULT_OK, d->Close()); 157 EXPECT_EQ(MOJO_RESULT_OK, d->Close());
164 } 158 }
165 159
166 TEST(SimpleDispatcherTest, BasicUnsatisfiable) { 160 TEST(SimpleDispatcherTest, BasicUnsatisfiable) {
167 test::Stopwatch stopwatch; 161 test::Stopwatch stopwatch;
168 int64_t elapsed_micros;
169 162
170 scoped_refptr<MockSimpleDispatcher> d(new MockSimpleDispatcher()); 163 scoped_refptr<MockSimpleDispatcher> d(new MockSimpleDispatcher());
171 Waiter w; 164 Waiter w;
172 165
173 // Try adding a writable waiter when it can never be writable. 166 // Try adding a writable waiter when it can never be writable.
174 w.Init(); 167 w.Init();
175 d->SetSatisfiableFlags(MOJO_WAIT_FLAG_READABLE); 168 d->SetSatisfiableFlags(MOJO_WAIT_FLAG_READABLE);
176 d->SetSatisfiedFlags(0); 169 d->SetSatisfiedFlags(0);
177 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, 170 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION,
178 d->AddWaiter(&w, MOJO_WAIT_FLAG_WRITABLE, 5)); 171 d->AddWaiter(&w, MOJO_WAIT_FLAG_WRITABLE, 5));
179 // Shouldn't need to remove the waiter (it was not added). 172 // Shouldn't need to remove the waiter (it was not added).
180 173
181 // Wait (forever) for writable and then it becomes never writable. 174 // Wait (forever) for writable and then it becomes never writable.
182 w.Init(); 175 w.Init();
183 d->SetSatisfiableFlags(MOJO_WAIT_FLAG_READABLE | MOJO_WAIT_FLAG_WRITABLE); 176 d->SetSatisfiableFlags(MOJO_WAIT_FLAG_READABLE | MOJO_WAIT_FLAG_WRITABLE);
184 EXPECT_EQ(MOJO_RESULT_OK, d->AddWaiter(&w, MOJO_WAIT_FLAG_WRITABLE, 6)); 177 EXPECT_EQ(MOJO_RESULT_OK, d->AddWaiter(&w, MOJO_WAIT_FLAG_WRITABLE, 6));
185 d->SetSatisfiableFlags(MOJO_WAIT_FLAG_READABLE); 178 d->SetSatisfiableFlags(MOJO_WAIT_FLAG_READABLE);
186 stopwatch.Start(); 179 stopwatch.Start();
187 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, w.Wait(MOJO_DEADLINE_INDEFINITE)); 180 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, w.Wait(MOJO_DEADLINE_INDEFINITE));
188 elapsed_micros = stopwatch.Elapsed(); 181 EXPECT_LT(stopwatch.Elapsed(), test::EpsilonTimeout());
189 EXPECT_LT(elapsed_micros, kEpsilonMicros);
190 d->RemoveWaiter(&w); 182 d->RemoveWaiter(&w);
191 183
192 // Wait for zero time for writable and then it becomes never writable. 184 // Wait for zero time for writable and then it becomes never writable.
193 w.Init(); 185 w.Init();
194 d->SetSatisfiableFlags(MOJO_WAIT_FLAG_READABLE | MOJO_WAIT_FLAG_WRITABLE); 186 d->SetSatisfiableFlags(MOJO_WAIT_FLAG_READABLE | MOJO_WAIT_FLAG_WRITABLE);
195 EXPECT_EQ(MOJO_RESULT_OK, d->AddWaiter(&w, MOJO_WAIT_FLAG_WRITABLE, 6)); 187 EXPECT_EQ(MOJO_RESULT_OK, d->AddWaiter(&w, MOJO_WAIT_FLAG_WRITABLE, 6));
196 d->SetSatisfiableFlags(MOJO_WAIT_FLAG_READABLE); 188 d->SetSatisfiableFlags(MOJO_WAIT_FLAG_READABLE);
197 stopwatch.Start(); 189 stopwatch.Start();
198 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, w.Wait(0)); 190 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, w.Wait(0));
199 elapsed_micros = stopwatch.Elapsed(); 191 EXPECT_LT(stopwatch.Elapsed(), test::EpsilonTimeout());
200 EXPECT_LT(elapsed_micros, kEpsilonMicros);
201 d->RemoveWaiter(&w); 192 d->RemoveWaiter(&w);
202 193
203 // Wait for non-zero, finite time for writable and then it becomes never 194 // Wait for non-zero, finite time for writable and then it becomes never
204 // writable. 195 // writable.
205 w.Init(); 196 w.Init();
206 d->SetSatisfiableFlags(MOJO_WAIT_FLAG_READABLE | MOJO_WAIT_FLAG_WRITABLE); 197 d->SetSatisfiableFlags(MOJO_WAIT_FLAG_READABLE | MOJO_WAIT_FLAG_WRITABLE);
207 EXPECT_EQ(MOJO_RESULT_OK, d->AddWaiter(&w, MOJO_WAIT_FLAG_WRITABLE, 7)); 198 EXPECT_EQ(MOJO_RESULT_OK, d->AddWaiter(&w, MOJO_WAIT_FLAG_WRITABLE, 7));
208 d->SetSatisfiableFlags(MOJO_WAIT_FLAG_READABLE); 199 d->SetSatisfiableFlags(MOJO_WAIT_FLAG_READABLE);
209 stopwatch.Start(); 200 stopwatch.Start();
210 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, w.Wait(2 * kEpsilonMicros)); 201 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION,
211 elapsed_micros = stopwatch.Elapsed(); 202 w.Wait(2 * test::EpsilonTimeout().InMicroseconds()));
212 EXPECT_LT(elapsed_micros, kEpsilonMicros); 203 EXPECT_LT(stopwatch.Elapsed(), test::EpsilonTimeout());
213 d->RemoveWaiter(&w); 204 d->RemoveWaiter(&w);
214 205
215 EXPECT_EQ(MOJO_RESULT_OK, d->Close()); 206 EXPECT_EQ(MOJO_RESULT_OK, d->Close());
216 } 207 }
217 208
218 TEST(SimpleDispatcherTest, BasicClosed) { 209 TEST(SimpleDispatcherTest, BasicClosed) {
219 test::Stopwatch stopwatch; 210 test::Stopwatch stopwatch;
220 int64_t elapsed_micros;
221 211
222 scoped_refptr<MockSimpleDispatcher> d; 212 scoped_refptr<MockSimpleDispatcher> d;
223 Waiter w; 213 Waiter w;
224 214
225 // Try adding a writable waiter when the dispatcher has been closed. 215 // Try adding a writable waiter when the dispatcher has been closed.
226 d = new MockSimpleDispatcher(); 216 d = new MockSimpleDispatcher();
227 w.Init(); 217 w.Init();
228 EXPECT_EQ(MOJO_RESULT_OK, d->Close()); 218 EXPECT_EQ(MOJO_RESULT_OK, d->Close());
229 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, 219 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT,
230 d->AddWaiter(&w, MOJO_WAIT_FLAG_WRITABLE, 8)); 220 d->AddWaiter(&w, MOJO_WAIT_FLAG_WRITABLE, 8));
231 // Shouldn't need to remove the waiter (it was not added). 221 // Shouldn't need to remove the waiter (it was not added).
232 222
233 // Wait (forever) for writable and then the dispatcher is closed. 223 // Wait (forever) for writable and then the dispatcher is closed.
234 d = new MockSimpleDispatcher(); 224 d = new MockSimpleDispatcher();
235 w.Init(); 225 w.Init();
236 EXPECT_EQ(MOJO_RESULT_OK, d->AddWaiter(&w, MOJO_WAIT_FLAG_WRITABLE, 9)); 226 EXPECT_EQ(MOJO_RESULT_OK, d->AddWaiter(&w, MOJO_WAIT_FLAG_WRITABLE, 9));
237 EXPECT_EQ(MOJO_RESULT_OK, d->Close()); 227 EXPECT_EQ(MOJO_RESULT_OK, d->Close());
238 stopwatch.Start(); 228 stopwatch.Start();
239 EXPECT_EQ(MOJO_RESULT_CANCELLED, w.Wait(MOJO_DEADLINE_INDEFINITE)); 229 EXPECT_EQ(MOJO_RESULT_CANCELLED, w.Wait(MOJO_DEADLINE_INDEFINITE));
240 elapsed_micros = stopwatch.Elapsed(); 230 EXPECT_LT(stopwatch.Elapsed(), test::EpsilonTimeout());
241 EXPECT_LT(elapsed_micros, kEpsilonMicros);
242 // Don't need to remove waiters from closed dispatchers. 231 // Don't need to remove waiters from closed dispatchers.
243 232
244 // Wait for zero time for writable and then the dispatcher is closed. 233 // Wait for zero time for writable and then the dispatcher is closed.
245 d = new MockSimpleDispatcher(); 234 d = new MockSimpleDispatcher();
246 w.Init(); 235 w.Init();
247 EXPECT_EQ(MOJO_RESULT_OK, d->AddWaiter(&w, MOJO_WAIT_FLAG_WRITABLE, 10)); 236 EXPECT_EQ(MOJO_RESULT_OK, d->AddWaiter(&w, MOJO_WAIT_FLAG_WRITABLE, 10));
248 EXPECT_EQ(MOJO_RESULT_OK, d->Close()); 237 EXPECT_EQ(MOJO_RESULT_OK, d->Close());
249 stopwatch.Start(); 238 stopwatch.Start();
250 EXPECT_EQ(MOJO_RESULT_CANCELLED, w.Wait(0)); 239 EXPECT_EQ(MOJO_RESULT_CANCELLED, w.Wait(0));
251 elapsed_micros = stopwatch.Elapsed(); 240 EXPECT_LT(stopwatch.Elapsed(), test::EpsilonTimeout());
252 EXPECT_LT(elapsed_micros, kEpsilonMicros);
253 // Don't need to remove waiters from closed dispatchers. 241 // Don't need to remove waiters from closed dispatchers.
254 242
255 // Wait for non-zero, finite time for writable and then the dispatcher is 243 // Wait for non-zero, finite time for writable and then the dispatcher is
256 // closed. 244 // closed.
257 d = new MockSimpleDispatcher(); 245 d = new MockSimpleDispatcher();
258 w.Init(); 246 w.Init();
259 EXPECT_EQ(MOJO_RESULT_OK, d->AddWaiter(&w, MOJO_WAIT_FLAG_WRITABLE, 11)); 247 EXPECT_EQ(MOJO_RESULT_OK, d->AddWaiter(&w, MOJO_WAIT_FLAG_WRITABLE, 11));
260 EXPECT_EQ(MOJO_RESULT_OK, d->Close()); 248 EXPECT_EQ(MOJO_RESULT_OK, d->Close());
261 stopwatch.Start(); 249 stopwatch.Start();
262 EXPECT_EQ(MOJO_RESULT_CANCELLED, w.Wait(2 * kEpsilonMicros)); 250 EXPECT_EQ(MOJO_RESULT_CANCELLED,
263 elapsed_micros = stopwatch.Elapsed(); 251 w.Wait(2 * test::EpsilonTimeout().InMicroseconds()));
264 EXPECT_LT(elapsed_micros, kEpsilonMicros); 252 EXPECT_LT(stopwatch.Elapsed(), test::EpsilonTimeout());
265 // Don't need to remove waiters from closed dispatchers. 253 // Don't need to remove waiters from closed dispatchers.
266 } 254 }
267 255
268 TEST(SimpleDispatcherTest, BasicThreaded) { 256 TEST(SimpleDispatcherTest, BasicThreaded) {
269 test::Stopwatch stopwatch; 257 test::Stopwatch stopwatch;
270 bool did_wait; 258 bool did_wait;
271 MojoResult result; 259 MojoResult result;
272 int64_t elapsed_micros;
273 260
274 // Wait for readable (already readable). 261 // Wait for readable (already readable).
275 { 262 {
276 scoped_refptr<MockSimpleDispatcher> d(new MockSimpleDispatcher()); 263 scoped_refptr<MockSimpleDispatcher> d(new MockSimpleDispatcher());
277 { 264 {
278 d->SetSatisfiedFlags(MOJO_WAIT_FLAG_READABLE); 265 d->SetSatisfiedFlags(MOJO_WAIT_FLAG_READABLE);
279 test::WaiterThread thread(d, 266 test::WaiterThread thread(d,
280 MOJO_WAIT_FLAG_READABLE, 267 MOJO_WAIT_FLAG_READABLE,
281 MOJO_DEADLINE_INDEFINITE, 268 MOJO_DEADLINE_INDEFINITE,
282 0, 269 0,
283 &did_wait, &result); 270 &did_wait, &result);
284 stopwatch.Start(); 271 stopwatch.Start();
285 thread.Start(); 272 thread.Start();
286 } // Joins the thread. 273 } // Joins the thread.
287 // If we closed earlier, then probably we'd get a |MOJO_RESULT_CANCELLED|. 274 // If we closed earlier, then probably we'd get a |MOJO_RESULT_CANCELLED|.
288 EXPECT_EQ(MOJO_RESULT_OK, d->Close()); 275 EXPECT_EQ(MOJO_RESULT_OK, d->Close());
289 } 276 }
290 elapsed_micros = stopwatch.Elapsed(); 277 EXPECT_LT(stopwatch.Elapsed(), test::EpsilonTimeout());
291 EXPECT_FALSE(did_wait); 278 EXPECT_FALSE(did_wait);
292 EXPECT_EQ(MOJO_RESULT_ALREADY_EXISTS, result); 279 EXPECT_EQ(MOJO_RESULT_ALREADY_EXISTS, result);
293 EXPECT_LT(elapsed_micros, kEpsilonMicros);
294 280
295 // Wait for readable and becomes readable after some time. 281 // Wait for readable and becomes readable after some time.
296 { 282 {
297 scoped_refptr<MockSimpleDispatcher> d(new MockSimpleDispatcher()); 283 scoped_refptr<MockSimpleDispatcher> d(new MockSimpleDispatcher());
298 test::WaiterThread thread(d, 284 test::WaiterThread thread(d,
299 MOJO_WAIT_FLAG_READABLE, 285 MOJO_WAIT_FLAG_READABLE,
300 MOJO_DEADLINE_INDEFINITE, 286 MOJO_DEADLINE_INDEFINITE,
301 1, 287 1,
302 &did_wait, &result); 288 &did_wait, &result);
303 stopwatch.Start(); 289 stopwatch.Start();
304 thread.Start(); 290 thread.Start();
305 base::PlatformThread::Sleep( 291 base::PlatformThread::Sleep(2 * test::EpsilonTimeout());
306 base::TimeDelta::FromMicroseconds(2 * kEpsilonMicros));
307 d->SetSatisfiedFlags(MOJO_WAIT_FLAG_READABLE); 292 d->SetSatisfiedFlags(MOJO_WAIT_FLAG_READABLE);
308 EXPECT_EQ(MOJO_RESULT_OK, d->Close()); 293 EXPECT_EQ(MOJO_RESULT_OK, d->Close());
309 } // Joins the thread. 294 } // Joins the thread.
310 elapsed_micros = stopwatch.Elapsed(); 295 base::TimeDelta elapsed = stopwatch.Elapsed();
296 EXPECT_GT(elapsed, (2-1) * test::EpsilonTimeout());
297 EXPECT_LT(elapsed, (2+1) * test::EpsilonTimeout());
311 EXPECT_TRUE(did_wait); 298 EXPECT_TRUE(did_wait);
312 EXPECT_EQ(1, result); 299 EXPECT_EQ(1, result);
313 EXPECT_GT(elapsed_micros, (2-1) * kEpsilonMicros);
314 EXPECT_LT(elapsed_micros, (2+1) * kEpsilonMicros);
315 300
316 // Wait for readable and becomes never-readable after some time. 301 // Wait for readable and becomes never-readable after some time.
317 { 302 {
318 scoped_refptr<MockSimpleDispatcher> d(new MockSimpleDispatcher()); 303 scoped_refptr<MockSimpleDispatcher> d(new MockSimpleDispatcher());
319 test::WaiterThread thread(d, 304 test::WaiterThread thread(d,
320 MOJO_WAIT_FLAG_READABLE, 305 MOJO_WAIT_FLAG_READABLE,
321 MOJO_DEADLINE_INDEFINITE, 306 MOJO_DEADLINE_INDEFINITE,
322 2, 307 2,
323 &did_wait, &result); 308 &did_wait, &result);
324 stopwatch.Start(); 309 stopwatch.Start();
325 thread.Start(); 310 thread.Start();
326 base::PlatformThread::Sleep( 311 base::PlatformThread::Sleep(2 * test::EpsilonTimeout());
327 base::TimeDelta::FromMicroseconds(2 * kEpsilonMicros));
328 d->SetSatisfiableFlags(MOJO_WAIT_FLAG_NONE); 312 d->SetSatisfiableFlags(MOJO_WAIT_FLAG_NONE);
329 EXPECT_EQ(MOJO_RESULT_OK, d->Close()); 313 EXPECT_EQ(MOJO_RESULT_OK, d->Close());
330 } // Joins the thread. 314 } // Joins the thread.
331 elapsed_micros = stopwatch.Elapsed(); 315 elapsed = stopwatch.Elapsed();
316 EXPECT_GT(elapsed, (2-1) * test::EpsilonTimeout());
317 EXPECT_LT(elapsed, (2+1) * test::EpsilonTimeout());
332 EXPECT_TRUE(did_wait); 318 EXPECT_TRUE(did_wait);
333 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, result); 319 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, result);
334 EXPECT_GT(elapsed_micros, (2-1) * kEpsilonMicros);
335 EXPECT_LT(elapsed_micros, (2+1) * kEpsilonMicros);
336 320
337 // Wait for readable and dispatcher gets closed. 321 // Wait for readable and dispatcher gets closed.
338 { 322 {
339 scoped_refptr<MockSimpleDispatcher> d(new MockSimpleDispatcher()); 323 scoped_refptr<MockSimpleDispatcher> d(new MockSimpleDispatcher());
340 test::WaiterThread thread(d, 324 test::WaiterThread thread(d,
341 MOJO_WAIT_FLAG_READABLE, 325 MOJO_WAIT_FLAG_READABLE,
342 MOJO_DEADLINE_INDEFINITE, 326 MOJO_DEADLINE_INDEFINITE,
343 3, 327 3,
344 &did_wait, &result); 328 &did_wait, &result);
345 stopwatch.Start(); 329 stopwatch.Start();
346 thread.Start(); 330 thread.Start();
347 base::PlatformThread::Sleep( 331 base::PlatformThread::Sleep(2 * test::EpsilonTimeout());
348 base::TimeDelta::FromMicroseconds(2 * kEpsilonMicros));
349 EXPECT_EQ(MOJO_RESULT_OK, d->Close()); 332 EXPECT_EQ(MOJO_RESULT_OK, d->Close());
350 } // Joins the thread. 333 } // Joins the thread.
351 elapsed_micros = stopwatch.Elapsed(); 334 elapsed = stopwatch.Elapsed();
335 EXPECT_GT(elapsed, (2-1) * test::EpsilonTimeout());
336 EXPECT_LT(elapsed, (2+1) * test::EpsilonTimeout());
352 EXPECT_TRUE(did_wait); 337 EXPECT_TRUE(did_wait);
353 EXPECT_EQ(MOJO_RESULT_CANCELLED, result); 338 EXPECT_EQ(MOJO_RESULT_CANCELLED, result);
354 EXPECT_GT(elapsed_micros, (2-1) * kEpsilonMicros);
355 EXPECT_LT(elapsed_micros, (2+1) * kEpsilonMicros);
356 339
357 // Wait for readable and times out. 340 // Wait for readable and times out.
358 { 341 {
359 scoped_refptr<MockSimpleDispatcher> d(new MockSimpleDispatcher()); 342 scoped_refptr<MockSimpleDispatcher> d(new MockSimpleDispatcher());
360 { 343 {
361 test::WaiterThread thread(d, 344 test::WaiterThread thread(d,
362 MOJO_WAIT_FLAG_READABLE, 345 MOJO_WAIT_FLAG_READABLE,
363 2 * kEpsilonMicros, 346 2 * test::EpsilonTimeout().InMicroseconds(),
364 4, 347 4,
365 &did_wait, &result); 348 &did_wait, &result);
366 stopwatch.Start(); 349 stopwatch.Start();
367 thread.Start(); 350 thread.Start();
368 base::PlatformThread::Sleep( 351 base::PlatformThread::Sleep(1 * test::EpsilonTimeout());
369 base::TimeDelta::FromMicroseconds(1 * kEpsilonMicros));
370 // Not what we're waiting for. 352 // Not what we're waiting for.
371 d->SetSatisfiedFlags(MOJO_WAIT_FLAG_WRITABLE); 353 d->SetSatisfiedFlags(MOJO_WAIT_FLAG_WRITABLE);
372 } // Joins the thread (after its wait times out). 354 } // Joins the thread (after its wait times out).
373 // If we closed earlier, then probably we'd get a |MOJO_RESULT_CANCELLED|. 355 // If we closed earlier, then probably we'd get a |MOJO_RESULT_CANCELLED|.
374 EXPECT_EQ(MOJO_RESULT_OK, d->Close()); 356 EXPECT_EQ(MOJO_RESULT_OK, d->Close());
375 } 357 }
376 elapsed_micros = stopwatch.Elapsed(); 358 elapsed = stopwatch.Elapsed();
359 EXPECT_GT(elapsed, (2-1) * test::EpsilonTimeout());
360 EXPECT_LT(elapsed, (2+1) * test::EpsilonTimeout());
377 EXPECT_TRUE(did_wait); 361 EXPECT_TRUE(did_wait);
378 EXPECT_EQ(MOJO_RESULT_DEADLINE_EXCEEDED, result); 362 EXPECT_EQ(MOJO_RESULT_DEADLINE_EXCEEDED, result);
379 EXPECT_GT(elapsed_micros, (2-1) * kEpsilonMicros);
380 EXPECT_LT(elapsed_micros, (2+1) * kEpsilonMicros);
381 } 363 }
382 364
383 TEST(SimpleDispatcherTest, MultipleWaiters) { 365 TEST(SimpleDispatcherTest, MultipleWaiters) {
384 static const size_t kNumWaiters = 20; 366 static const size_t kNumWaiters = 20;
385 367
386 bool did_wait[kNumWaiters]; 368 bool did_wait[kNumWaiters];
387 MojoResult result[kNumWaiters]; 369 MojoResult result[kNumWaiters];
388 370
389 // All wait for readable and becomes readable after some time. 371 // All wait for readable and becomes readable after some time.
390 { 372 {
391 scoped_refptr<MockSimpleDispatcher> d(new MockSimpleDispatcher()); 373 scoped_refptr<MockSimpleDispatcher> d(new MockSimpleDispatcher());
392 ScopedVector<test::WaiterThread> threads; 374 ScopedVector<test::WaiterThread> threads;
393 for (size_t i = 0; i < kNumWaiters; i++) { 375 for (size_t i = 0; i < kNumWaiters; i++) {
394 threads.push_back(new test::WaiterThread(d, 376 threads.push_back(new test::WaiterThread(d,
395 MOJO_WAIT_FLAG_READABLE, 377 MOJO_WAIT_FLAG_READABLE,
396 MOJO_DEADLINE_INDEFINITE, 378 MOJO_DEADLINE_INDEFINITE,
397 static_cast<MojoResult>(i), 379 static_cast<MojoResult>(i),
398 &did_wait[i], &result[i])); 380 &did_wait[i], &result[i]));
399 threads.back()->Start(); 381 threads.back()->Start();
400 } 382 }
401 base::PlatformThread::Sleep( 383 base::PlatformThread::Sleep(2 * test::EpsilonTimeout());
402 base::TimeDelta::FromMicroseconds(2 * kEpsilonMicros));
403 d->SetSatisfiedFlags(MOJO_WAIT_FLAG_READABLE); 384 d->SetSatisfiedFlags(MOJO_WAIT_FLAG_READABLE);
404 EXPECT_EQ(MOJO_RESULT_OK, d->Close()); 385 EXPECT_EQ(MOJO_RESULT_OK, d->Close());
405 } // Joins the threads. 386 } // Joins the threads.
406 for (size_t i = 0; i < kNumWaiters; i++) { 387 for (size_t i = 0; i < kNumWaiters; i++) {
407 EXPECT_TRUE(did_wait[i]); 388 EXPECT_TRUE(did_wait[i]);
408 EXPECT_EQ(static_cast<MojoResult>(i), result[i]); 389 EXPECT_EQ(static_cast<MojoResult>(i), result[i]);
409 } 390 }
410 391
411 // Some wait for readable, some for writable, and becomes readable after some 392 // Some wait for readable, some for writable, and becomes readable after some
412 // time. 393 // time.
413 { 394 {
414 scoped_refptr<MockSimpleDispatcher> d(new MockSimpleDispatcher()); 395 scoped_refptr<MockSimpleDispatcher> d(new MockSimpleDispatcher());
415 ScopedVector<test::WaiterThread> threads; 396 ScopedVector<test::WaiterThread> threads;
416 for (size_t i = 0; i < kNumWaiters / 2; i++) { 397 for (size_t i = 0; i < kNumWaiters / 2; i++) {
417 threads.push_back(new test::WaiterThread(d, 398 threads.push_back(new test::WaiterThread(d,
418 MOJO_WAIT_FLAG_READABLE, 399 MOJO_WAIT_FLAG_READABLE,
419 MOJO_DEADLINE_INDEFINITE, 400 MOJO_DEADLINE_INDEFINITE,
420 static_cast<MojoResult>(i), 401 static_cast<MojoResult>(i),
421 &did_wait[i], &result[i])); 402 &did_wait[i], &result[i]));
422 threads.back()->Start(); 403 threads.back()->Start();
423 } 404 }
424 for (size_t i = kNumWaiters / 2; i < kNumWaiters; i++) { 405 for (size_t i = kNumWaiters / 2; i < kNumWaiters; i++) {
425 threads.push_back(new test::WaiterThread(d, 406 threads.push_back(new test::WaiterThread(d,
426 MOJO_WAIT_FLAG_WRITABLE, 407 MOJO_WAIT_FLAG_WRITABLE,
427 MOJO_DEADLINE_INDEFINITE, 408 MOJO_DEADLINE_INDEFINITE,
428 static_cast<MojoResult>(i), 409 static_cast<MojoResult>(i),
429 &did_wait[i], &result[i])); 410 &did_wait[i], &result[i]));
430 threads.back()->Start(); 411 threads.back()->Start();
431 } 412 }
432 base::PlatformThread::Sleep( 413 base::PlatformThread::Sleep(2 * test::EpsilonTimeout());
433 base::TimeDelta::FromMicroseconds(2 * kEpsilonMicros));
434 d->SetSatisfiedFlags(MOJO_WAIT_FLAG_READABLE); 414 d->SetSatisfiedFlags(MOJO_WAIT_FLAG_READABLE);
435 // This will wake up the ones waiting to write. 415 // This will wake up the ones waiting to write.
436 EXPECT_EQ(MOJO_RESULT_OK, d->Close()); 416 EXPECT_EQ(MOJO_RESULT_OK, d->Close());
437 } // Joins the threads. 417 } // Joins the threads.
438 for (size_t i = 0; i < kNumWaiters / 2; i++) { 418 for (size_t i = 0; i < kNumWaiters / 2; i++) {
439 EXPECT_TRUE(did_wait[i]); 419 EXPECT_TRUE(did_wait[i]);
440 EXPECT_EQ(static_cast<MojoResult>(i), result[i]); 420 EXPECT_EQ(static_cast<MojoResult>(i), result[i]);
441 } 421 }
442 for (size_t i = kNumWaiters / 2; i < kNumWaiters; i++) { 422 for (size_t i = kNumWaiters / 2; i < kNumWaiters; i++) {
443 EXPECT_TRUE(did_wait[i]); 423 EXPECT_TRUE(did_wait[i]);
(...skipping 14 matching lines...) Expand all
458 threads.back()->Start(); 438 threads.back()->Start();
459 } 439 }
460 for (size_t i = kNumWaiters / 2; i < kNumWaiters; i++) { 440 for (size_t i = kNumWaiters / 2; i < kNumWaiters; i++) {
461 threads.push_back(new test::WaiterThread(d, 441 threads.push_back(new test::WaiterThread(d,
462 MOJO_WAIT_FLAG_WRITABLE, 442 MOJO_WAIT_FLAG_WRITABLE,
463 MOJO_DEADLINE_INDEFINITE, 443 MOJO_DEADLINE_INDEFINITE,
464 static_cast<MojoResult>(i), 444 static_cast<MojoResult>(i),
465 &did_wait[i], &result[i])); 445 &did_wait[i], &result[i]));
466 threads.back()->Start(); 446 threads.back()->Start();
467 } 447 }
468 base::PlatformThread::Sleep( 448 base::PlatformThread::Sleep(1 * test::EpsilonTimeout());
469 base::TimeDelta::FromMicroseconds(1 * kEpsilonMicros));
470 d->SetSatisfiableFlags(MOJO_WAIT_FLAG_READABLE); 449 d->SetSatisfiableFlags(MOJO_WAIT_FLAG_READABLE);
471 base::PlatformThread::Sleep( 450 base::PlatformThread::Sleep(1 * test::EpsilonTimeout());
472 base::TimeDelta::FromMicroseconds(1 * kEpsilonMicros));
473 d->SetSatisfiedFlags(MOJO_WAIT_FLAG_READABLE); 451 d->SetSatisfiedFlags(MOJO_WAIT_FLAG_READABLE);
474 EXPECT_EQ(MOJO_RESULT_OK, d->Close()); 452 EXPECT_EQ(MOJO_RESULT_OK, d->Close());
475 } // Joins the threads. 453 } // Joins the threads.
476 for (size_t i = 0; i < kNumWaiters / 2; i++) { 454 for (size_t i = 0; i < kNumWaiters / 2; i++) {
477 EXPECT_TRUE(did_wait[i]); 455 EXPECT_TRUE(did_wait[i]);
478 EXPECT_EQ(static_cast<MojoResult>(i), result[i]); 456 EXPECT_EQ(static_cast<MojoResult>(i), result[i]);
479 } 457 }
480 for (size_t i = kNumWaiters / 2; i < kNumWaiters; i++) { 458 for (size_t i = kNumWaiters / 2; i < kNumWaiters; i++) {
481 EXPECT_TRUE(did_wait[i]); 459 EXPECT_TRUE(did_wait[i]);
482 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, result[i]); 460 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, result[i]);
483 } 461 }
484 462
485 // Some wait for readable, some for writable, and becomes readable after some 463 // Some wait for readable, some for writable, and becomes readable after some
486 // time. 464 // time.
487 { 465 {
488 scoped_refptr<MockSimpleDispatcher> d(new MockSimpleDispatcher()); 466 scoped_refptr<MockSimpleDispatcher> d(new MockSimpleDispatcher());
489 ScopedVector<test::WaiterThread> threads; 467 ScopedVector<test::WaiterThread> threads;
490 for (size_t i = 0; i < kNumWaiters / 2; i++) { 468 for (size_t i = 0; i < kNumWaiters / 2; i++) {
491 threads.push_back(new test::WaiterThread(d, 469 threads.push_back(
492 MOJO_WAIT_FLAG_READABLE, 470 new test::WaiterThread(d,
493 3 * kEpsilonMicros, 471 MOJO_WAIT_FLAG_READABLE,
494 static_cast<MojoResult>(i), 472 3 * test::EpsilonTimeout().InMicroseconds(),
495 &did_wait[i], &result[i])); 473 static_cast<MojoResult>(i),
474 &did_wait[i], &result[i]));
496 threads.back()->Start(); 475 threads.back()->Start();
497 } 476 }
498 for (size_t i = kNumWaiters / 2; i < kNumWaiters; i++) { 477 for (size_t i = kNumWaiters / 2; i < kNumWaiters; i++) {
499 threads.push_back(new test::WaiterThread(d, 478 threads.push_back(
500 MOJO_WAIT_FLAG_WRITABLE, 479 new test::WaiterThread(d,
501 1 * kEpsilonMicros, 480 MOJO_WAIT_FLAG_WRITABLE,
502 static_cast<MojoResult>(i), 481 1 * test::EpsilonTimeout().InMicroseconds(),
503 &did_wait[i], &result[i])); 482 static_cast<MojoResult>(i),
483 &did_wait[i], &result[i]));
504 threads.back()->Start(); 484 threads.back()->Start();
505 } 485 }
506 base::PlatformThread::Sleep( 486 base::PlatformThread::Sleep(2 * test::EpsilonTimeout());
507 base::TimeDelta::FromMicroseconds(2 * kEpsilonMicros));
508 d->SetSatisfiedFlags(MOJO_WAIT_FLAG_READABLE); 487 d->SetSatisfiedFlags(MOJO_WAIT_FLAG_READABLE);
509 // All those waiting for writable should have timed out. 488 // All those waiting for writable should have timed out.
510 EXPECT_EQ(MOJO_RESULT_OK, d->Close()); 489 EXPECT_EQ(MOJO_RESULT_OK, d->Close());
511 } // Joins the threads. 490 } // Joins the threads.
512 for (size_t i = 0; i < kNumWaiters / 2; i++) { 491 for (size_t i = 0; i < kNumWaiters / 2; i++) {
513 EXPECT_TRUE(did_wait[i]); 492 EXPECT_TRUE(did_wait[i]);
514 EXPECT_EQ(static_cast<MojoResult>(i), result[i]); 493 EXPECT_EQ(static_cast<MojoResult>(i), result[i]);
515 } 494 }
516 for (size_t i = kNumWaiters / 2; i < kNumWaiters; i++) { 495 for (size_t i = kNumWaiters / 2; i < kNumWaiters; i++) {
517 EXPECT_TRUE(did_wait[i]); 496 EXPECT_TRUE(did_wait[i]);
518 EXPECT_EQ(MOJO_RESULT_DEADLINE_EXCEEDED, result[i]); 497 EXPECT_EQ(MOJO_RESULT_DEADLINE_EXCEEDED, result[i]);
519 } 498 }
520 } 499 }
521 500
522 // TODO(vtl): Stress test? 501 // TODO(vtl): Stress test?
523 502
524 } // namespace 503 } // namespace
525 } // namespace system 504 } // namespace system
526 } // namespace mojo 505 } // namespace mojo
OLDNEW
« no previous file with comments | « mojo/system/message_pipe_dispatcher_unittest.cc ('k') | mojo/system/test_utils.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698