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

Side by Side Diff: mojo/edk/system/waiter_unittest.cc

Issue 1639093002: Add //mojo/edk/platform/thread_utils.* containing "yield" and "sleep". (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Created 4 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
« no previous file with comments | « mojo/edk/system/test/sleep_unittest.cc ('k') | mojo/edk/util/cond_var_unittest.cc » ('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. |test::EpsilonTimeout()| may be increased to 6 // heavily-loaded system). Sorry. |test::EpsilonTimeout()| may be increased to
7 // increase tolerance and reduce observed flakiness (though doing so reduces the 7 // increase tolerance and reduce observed flakiness (though doing so reduces the
8 // meaningfulness of the test). 8 // meaningfulness of the test).
9 9
10 #include "mojo/edk/system/waiter.h" 10 #include "mojo/edk/system/waiter.h"
11 11
12 #include <stdint.h> 12 #include <stdint.h>
13 13
14 #include "mojo/edk/platform/thread_utils.h"
14 #include "mojo/edk/system/test/simple_test_thread.h" 15 #include "mojo/edk/system/test/simple_test_thread.h"
15 #include "mojo/edk/system/test/sleep.h"
16 #include "mojo/edk/system/test/stopwatch.h" 16 #include "mojo/edk/system/test/stopwatch.h"
17 #include "mojo/edk/system/test/timeouts.h" 17 #include "mojo/edk/system/test/timeouts.h"
18 #include "mojo/edk/util/mutex.h" 18 #include "mojo/edk/util/mutex.h"
19 #include "mojo/public/cpp/system/macros.h" 19 #include "mojo/public/cpp/system/macros.h"
20 #include "testing/gtest/include/gtest/gtest.h" 20 #include "testing/gtest/include/gtest/gtest.h"
21 21
22 using mojo::platform::ThreadSleep;
22 using mojo::util::Mutex; 23 using mojo::util::Mutex;
23 using mojo::util::MutexLocker; 24 using mojo::util::MutexLocker;
24 25
25 namespace mojo { 26 namespace mojo {
26 namespace system { 27 namespace system {
27 namespace { 28 namespace {
28 29
29 const unsigned kPollTimeMs = 10; 30 const unsigned kPollTimeMs = 10;
30 31
31 class WaitingThread : public test::SimpleTestThread { 32 class WaitingThread : public test::SimpleTestThread {
(...skipping 15 matching lines...) Expand all
47 { 48 {
48 MutexLocker locker(&mutex_); 49 MutexLocker locker(&mutex_);
49 if (done_) { 50 if (done_) {
50 *result = result_; 51 *result = result_;
51 *context = context_; 52 *context = context_;
52 *elapsed = elapsed_; 53 *elapsed = elapsed_;
53 break; 54 break;
54 } 55 }
55 } 56 }
56 57
57 test::SleepMilliseconds(kPollTimeMs); 58 ThreadSleep(test::DeadlineFromMilliseconds(kPollTimeMs));
58 } 59 }
59 } 60 }
60 61
61 Waiter* waiter() { return &waiter_; } 62 Waiter* waiter() { return &waiter_; }
62 63
63 private: 64 private:
64 void Run() override { 65 void Run() override {
65 test::Stopwatch stopwatch; 66 test::Stopwatch stopwatch;
66 MojoResult result; 67 MojoResult result;
67 uint32_t context = static_cast<uint32_t>(-1); 68 uint32_t context = static_cast<uint32_t>(-1);
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
118 thread.WaitUntilDone(&result, &context, &elapsed); 119 thread.WaitUntilDone(&result, &context, &elapsed);
119 EXPECT_EQ(MOJO_RESULT_CANCELLED, result); 120 EXPECT_EQ(MOJO_RESULT_CANCELLED, result);
120 EXPECT_EQ(2u, context); 121 EXPECT_EQ(2u, context);
121 EXPECT_LT(elapsed, test::EpsilonTimeout()); 122 EXPECT_LT(elapsed, test::EpsilonTimeout());
122 } 123 }
123 124
124 // Awake some time after thread start. 125 // Awake some time after thread start.
125 { 126 {
126 WaitingThread thread(10 * test::EpsilonTimeout()); 127 WaitingThread thread(10 * test::EpsilonTimeout());
127 thread.Start(); 128 thread.Start();
128 test::Sleep(2 * test::EpsilonTimeout()); 129 ThreadSleep(2 * test::EpsilonTimeout());
129 thread.waiter()->Awake(1, 3); 130 thread.waiter()->Awake(1, 3);
130 thread.WaitUntilDone(&result, &context, &elapsed); 131 thread.WaitUntilDone(&result, &context, &elapsed);
131 EXPECT_EQ(1u, result); 132 EXPECT_EQ(1u, result);
132 EXPECT_EQ(3u, context); 133 EXPECT_EQ(3u, context);
133 EXPECT_GT(elapsed, (2 - 1) * test::EpsilonTimeout()); 134 EXPECT_GT(elapsed, (2 - 1) * test::EpsilonTimeout());
134 EXPECT_LT(elapsed, (2 + 1) * test::EpsilonTimeout()); 135 EXPECT_LT(elapsed, (2 + 1) * test::EpsilonTimeout());
135 } 136 }
136 137
137 // Awake some longer time after thread start. 138 // Awake some longer time after thread start.
138 { 139 {
139 WaitingThread thread(10 * test::EpsilonTimeout()); 140 WaitingThread thread(10 * test::EpsilonTimeout());
140 thread.Start(); 141 thread.Start();
141 test::Sleep(5 * test::EpsilonTimeout()); 142 ThreadSleep(5 * test::EpsilonTimeout());
142 thread.waiter()->Awake(2, 4); 143 thread.waiter()->Awake(2, 4);
143 thread.WaitUntilDone(&result, &context, &elapsed); 144 thread.WaitUntilDone(&result, &context, &elapsed);
144 EXPECT_EQ(2u, result); 145 EXPECT_EQ(2u, result);
145 EXPECT_EQ(4u, context); 146 EXPECT_EQ(4u, context);
146 EXPECT_GT(elapsed, (5 - 1) * test::EpsilonTimeout()); 147 EXPECT_GT(elapsed, (5 - 1) * test::EpsilonTimeout());
147 EXPECT_LT(elapsed, (5 + 1) * test::EpsilonTimeout()); 148 EXPECT_LT(elapsed, (5 + 1) * test::EpsilonTimeout());
148 } 149 }
149 150
150 // Don't awake -- time out (on another thread). 151 // Don't awake -- time out (on another thread).
151 { 152 {
(...skipping 27 matching lines...) Expand all
179 thread.WaitUntilDone(&result, &context, &elapsed); 180 thread.WaitUntilDone(&result, &context, &elapsed);
180 EXPECT_EQ(MOJO_RESULT_CANCELLED, result); 181 EXPECT_EQ(MOJO_RESULT_CANCELLED, result);
181 EXPECT_EQ(6u, context); 182 EXPECT_EQ(6u, context);
182 EXPECT_LT(elapsed, test::EpsilonTimeout()); 183 EXPECT_LT(elapsed, test::EpsilonTimeout());
183 } 184 }
184 185
185 // Awake some time after thread start. 186 // Awake some time after thread start.
186 { 187 {
187 WaitingThread thread(MOJO_DEADLINE_INDEFINITE); 188 WaitingThread thread(MOJO_DEADLINE_INDEFINITE);
188 thread.Start(); 189 thread.Start();
189 test::Sleep(2 * test::EpsilonTimeout()); 190 ThreadSleep(2 * test::EpsilonTimeout());
190 thread.waiter()->Awake(1, 7); 191 thread.waiter()->Awake(1, 7);
191 thread.WaitUntilDone(&result, &context, &elapsed); 192 thread.WaitUntilDone(&result, &context, &elapsed);
192 EXPECT_EQ(1u, result); 193 EXPECT_EQ(1u, result);
193 EXPECT_EQ(7u, context); 194 EXPECT_EQ(7u, context);
194 EXPECT_GT(elapsed, (2 - 1) * test::EpsilonTimeout()); 195 EXPECT_GT(elapsed, (2 - 1) * test::EpsilonTimeout());
195 EXPECT_LT(elapsed, (2 + 1) * test::EpsilonTimeout()); 196 EXPECT_LT(elapsed, (2 + 1) * test::EpsilonTimeout());
196 } 197 }
197 198
198 // Awake some longer time after thread start. 199 // Awake some longer time after thread start.
199 { 200 {
200 WaitingThread thread(MOJO_DEADLINE_INDEFINITE); 201 WaitingThread thread(MOJO_DEADLINE_INDEFINITE);
201 thread.Start(); 202 thread.Start();
202 test::Sleep(5 * test::EpsilonTimeout()); 203 ThreadSleep(5 * test::EpsilonTimeout());
203 thread.waiter()->Awake(2, 8); 204 thread.waiter()->Awake(2, 8);
204 thread.WaitUntilDone(&result, &context, &elapsed); 205 thread.WaitUntilDone(&result, &context, &elapsed);
205 EXPECT_EQ(2u, result); 206 EXPECT_EQ(2u, result);
206 EXPECT_EQ(8u, context); 207 EXPECT_EQ(8u, context);
207 EXPECT_GT(elapsed, (5 - 1) * test::EpsilonTimeout()); 208 EXPECT_GT(elapsed, (5 - 1) * test::EpsilonTimeout());
208 EXPECT_LT(elapsed, (5 + 1) * test::EpsilonTimeout()); 209 EXPECT_LT(elapsed, (5 + 1) * test::EpsilonTimeout());
209 } 210 }
210 } 211 }
211 212
212 TEST(WaiterTest, TimeOut) { 213 TEST(WaiterTest, TimeOut) {
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
267 thread.WaitUntilDone(&result, &context, &elapsed); 268 thread.WaitUntilDone(&result, &context, &elapsed);
268 EXPECT_EQ(1u, result); 269 EXPECT_EQ(1u, result);
269 EXPECT_EQ(3u, context); 270 EXPECT_EQ(3u, context);
270 EXPECT_LT(elapsed, test::EpsilonTimeout()); 271 EXPECT_LT(elapsed, test::EpsilonTimeout());
271 } 272 }
272 273
273 { 274 {
274 WaitingThread thread(MOJO_DEADLINE_INDEFINITE); 275 WaitingThread thread(MOJO_DEADLINE_INDEFINITE);
275 thread.Start(); 276 thread.Start();
276 thread.waiter()->Awake(10, 5); 277 thread.waiter()->Awake(10, 5);
277 test::Sleep(2 * test::EpsilonTimeout()); 278 ThreadSleep(2 * test::EpsilonTimeout());
278 thread.waiter()->Awake(20, 6); 279 thread.waiter()->Awake(20, 6);
279 thread.WaitUntilDone(&result, &context, &elapsed); 280 thread.WaitUntilDone(&result, &context, &elapsed);
280 EXPECT_EQ(10u, result); 281 EXPECT_EQ(10u, result);
281 EXPECT_EQ(5u, context); 282 EXPECT_EQ(5u, context);
282 EXPECT_LT(elapsed, test::EpsilonTimeout()); 283 EXPECT_LT(elapsed, test::EpsilonTimeout());
283 } 284 }
284 285
285 { 286 {
286 WaitingThread thread(10 * test::EpsilonTimeout()); 287 WaitingThread thread(10 * test::EpsilonTimeout());
287 thread.Start(); 288 thread.Start();
288 test::Sleep(1 * test::EpsilonTimeout()); 289 ThreadSleep(1 * test::EpsilonTimeout());
289 thread.waiter()->Awake(MOJO_RESULT_FAILED_PRECONDITION, 7); 290 thread.waiter()->Awake(MOJO_RESULT_FAILED_PRECONDITION, 7);
290 test::Sleep(2 * test::EpsilonTimeout()); 291 ThreadSleep(2 * test::EpsilonTimeout());
291 thread.waiter()->Awake(MOJO_RESULT_OK, 8); 292 thread.waiter()->Awake(MOJO_RESULT_OK, 8);
292 thread.WaitUntilDone(&result, &context, &elapsed); 293 thread.WaitUntilDone(&result, &context, &elapsed);
293 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, result); 294 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, result);
294 EXPECT_EQ(7u, context); 295 EXPECT_EQ(7u, context);
295 EXPECT_GT(elapsed, (1 - 1) * test::EpsilonTimeout()); 296 EXPECT_GT(elapsed, (1 - 1) * test::EpsilonTimeout());
296 EXPECT_LT(elapsed, (1 + 1) * test::EpsilonTimeout()); 297 EXPECT_LT(elapsed, (1 + 1) * test::EpsilonTimeout());
297 } 298 }
298 } 299 }
299 300
300 } // namespace 301 } // namespace
301 } // namespace system 302 } // namespace system
302 } // namespace mojo 303 } // namespace mojo
OLDNEW
« no previous file with comments | « mojo/edk/system/test/sleep_unittest.cc ('k') | mojo/edk/util/cond_var_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698