diff options
author | viettrungluu@chromium.org <viettrungluu@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-05-14 02:14:27 +0000 |
---|---|---|
committer | viettrungluu@chromium.org <viettrungluu@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-05-14 02:14:27 +0000 |
commit | 0e377ae742bb09e28d129747ab759e4721cb9c78 (patch) | |
tree | 53a4a4352eabe9c7feb23eab9d303ef0663cb5b6 | |
parent | 4c35abcf25ae1e30a4335b36265e9d286253cc81 (diff) | |
download | chromium_src-0e377ae742bb09e28d129747ab759e4721cb9c78.zip chromium_src-0e377ae742bb09e28d129747ab759e4721cb9c78.tar.gz chromium_src-0e377ae742bb09e28d129747ab759e4721cb9c78.tar.bz2 |
Mojo: Base our epsilon timeouts off of TestTimeouts::tiny_timeout().
It's still a very small timeout, but at least it'll be adjusted
(somewhat) for ASAN, and can be controlled via a command-line flag.
Also switch to using base::TimeDelta instead of int64_t micros.
R=sky@chromium.org
Review URL: https://codereview.chromium.org/281893002
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@270303 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r-- | mojo/system/message_pipe_dispatcher_unittest.cc | 56 | ||||
-rw-r--r-- | mojo/system/simple_dispatcher_unittest.cc | 137 | ||||
-rw-r--r-- | mojo/system/test_utils.cc | 9 | ||||
-rw-r--r-- | mojo/system/test_utils.h | 27 | ||||
-rw-r--r-- | mojo/system/waiter_list_unittest.cc | 41 | ||||
-rw-r--r-- | mojo/system/waiter_unittest.cc | 146 |
6 files changed, 193 insertions, 223 deletions
diff --git a/mojo/system/message_pipe_dispatcher_unittest.cc b/mojo/system/message_pipe_dispatcher_unittest.cc index bfd4ddb..f683348 100644 --- a/mojo/system/message_pipe_dispatcher_unittest.cc +++ b/mojo/system/message_pipe_dispatcher_unittest.cc @@ -3,8 +3,9 @@ // found in the LICENSE file. // NOTE(vtl): Some of these tests are inherently flaky (e.g., if run on a -// heavily-loaded system). Sorry. |kEpsilonMicros| may be increased to increase -// tolerance and reduce observed flakiness. +// heavily-loaded system). Sorry. |test::EpsilonTimeout()| may be increased to +// increase tolerance and reduce observed flakiness (though doing so reduces the +// meaningfulness of the test). #include "mojo/system/message_pipe_dispatcher.h" @@ -28,15 +29,11 @@ namespace mojo { namespace system { namespace { -const int64_t kMicrosPerMs = 1000; -const int64_t kEpsilonMicros = 30 * kMicrosPerMs; // 30 ms. - TEST(MessagePipeDispatcherTest, Basic) { test::Stopwatch stopwatch; int32_t buffer[1]; const uint32_t kBufferSize = static_cast<uint32_t>(sizeof(buffer)); uint32_t buffer_size; - int64_t elapsed_micros; // Run this test both with |d0| as port 0, |d1| as port 1 and vice versa. for (unsigned i = 0; i < 2; i++) { @@ -68,8 +65,7 @@ TEST(MessagePipeDispatcherTest, Basic) { MOJO_WRITE_MESSAGE_FLAG_NONE)); stopwatch.Start(); EXPECT_EQ(1, w.Wait(MOJO_DEADLINE_INDEFINITE)); - elapsed_micros = stopwatch.Elapsed(); - EXPECT_LT(elapsed_micros, kEpsilonMicros); + EXPECT_LT(stopwatch.Elapsed(), test::EpsilonTimeout()); d0->RemoveWaiter(&w); // Try adding a readable waiter when already readable (from above). @@ -94,8 +90,7 @@ TEST(MessagePipeDispatcherTest, Basic) { d0->AddWaiter(&w, MOJO_WAIT_FLAG_READABLE, 3)); stopwatch.Start(); EXPECT_EQ(MOJO_RESULT_DEADLINE_EXCEEDED, w.Wait(0)); - elapsed_micros = stopwatch.Elapsed(); - EXPECT_LT(elapsed_micros, kEpsilonMicros); + EXPECT_LT(stopwatch.Elapsed(), test::EpsilonTimeout()); d0->RemoveWaiter(&w); // Wait for non-zero, finite time for readability on |d0| (will time out). @@ -103,10 +98,11 @@ TEST(MessagePipeDispatcherTest, Basic) { EXPECT_EQ(MOJO_RESULT_OK, d0->AddWaiter(&w, MOJO_WAIT_FLAG_READABLE, 3)); stopwatch.Start(); - EXPECT_EQ(MOJO_RESULT_DEADLINE_EXCEEDED, w.Wait(2 * kEpsilonMicros)); - elapsed_micros = stopwatch.Elapsed(); - EXPECT_GT(elapsed_micros, (2-1) * kEpsilonMicros); - EXPECT_LT(elapsed_micros, (2+1) * kEpsilonMicros); + EXPECT_EQ(MOJO_RESULT_DEADLINE_EXCEEDED, + w.Wait(2 * test::EpsilonTimeout().InMicroseconds())); + base::TimeDelta elapsed = stopwatch.Elapsed(); + EXPECT_GT(elapsed, (2-1) * test::EpsilonTimeout()); + EXPECT_LT(elapsed, (2+1) * test::EpsilonTimeout()); d0->RemoveWaiter(&w); EXPECT_EQ(MOJO_RESULT_OK, d0->Close()); @@ -259,9 +255,9 @@ TEST(MessagePipeDispatcherTest, BasicThreaded) { int32_t buffer[1]; const uint32_t kBufferSize = static_cast<uint32_t>(sizeof(buffer)); uint32_t buffer_size; + base::TimeDelta elapsed; bool did_wait; MojoResult result; - int64_t elapsed_micros; // Run this test both with |d0| as port 0, |d1| as port 1 and vice versa. for (unsigned i = 0; i < 2; i++) { @@ -282,8 +278,7 @@ TEST(MessagePipeDispatcherTest, BasicThreaded) { &did_wait, &result); stopwatch.Start(); thread.Start(); - base::PlatformThread::Sleep( - base::TimeDelta::FromMicroseconds(2 * kEpsilonMicros)); + base::PlatformThread::Sleep(2 * test::EpsilonTimeout()); // Wake it up by writing to |d0|. buffer[0] = 123456789; EXPECT_EQ(MOJO_RESULT_OK, @@ -291,11 +286,11 @@ TEST(MessagePipeDispatcherTest, BasicThreaded) { NULL, MOJO_WRITE_MESSAGE_FLAG_NONE)); } // Joins the thread. - elapsed_micros = stopwatch.Elapsed(); + elapsed = stopwatch.Elapsed(); + EXPECT_GT(elapsed, (2-1) * test::EpsilonTimeout()); + EXPECT_LT(elapsed, (2+1) * test::EpsilonTimeout()); EXPECT_TRUE(did_wait); EXPECT_EQ(0, result); - EXPECT_GT(elapsed_micros, (2-1) * kEpsilonMicros); - EXPECT_LT(elapsed_micros, (2+1) * kEpsilonMicros); // Now |d1| is already readable. Try waiting for it again. { @@ -307,10 +302,9 @@ TEST(MessagePipeDispatcherTest, BasicThreaded) { stopwatch.Start(); thread.Start(); } // Joins the thread. - elapsed_micros = stopwatch.Elapsed(); + EXPECT_LT(stopwatch.Elapsed(), test::EpsilonTimeout()); EXPECT_FALSE(did_wait); EXPECT_EQ(MOJO_RESULT_ALREADY_EXISTS, result); - EXPECT_LT(elapsed_micros, kEpsilonMicros); // Consume what we wrote to |d0|. buffer[0] = 0; @@ -332,15 +326,14 @@ TEST(MessagePipeDispatcherTest, BasicThreaded) { &did_wait, &result); stopwatch.Start(); thread.Start(); - base::PlatformThread::Sleep( - base::TimeDelta::FromMicroseconds(2 * kEpsilonMicros)); + base::PlatformThread::Sleep(2 * test::EpsilonTimeout()); EXPECT_EQ(MOJO_RESULT_OK, d0->Close()); } // Joins the thread. - elapsed_micros = stopwatch.Elapsed(); + elapsed = stopwatch.Elapsed(); + EXPECT_GT(elapsed, (2-1) * test::EpsilonTimeout()); + EXPECT_LT(elapsed, (2+1) * test::EpsilonTimeout()); EXPECT_TRUE(did_wait); EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, result); - EXPECT_GT(elapsed_micros, (2-1) * kEpsilonMicros); - EXPECT_LT(elapsed_micros, (2+1) * kEpsilonMicros); EXPECT_EQ(MOJO_RESULT_OK, d1->Close()); } @@ -364,15 +357,14 @@ TEST(MessagePipeDispatcherTest, BasicThreaded) { &did_wait, &result); stopwatch.Start(); thread.Start(); - base::PlatformThread::Sleep( - base::TimeDelta::FromMicroseconds(2 * kEpsilonMicros)); + base::PlatformThread::Sleep(2 * test::EpsilonTimeout()); EXPECT_EQ(MOJO_RESULT_OK, d1->Close()); } // Joins the thread. - elapsed_micros = stopwatch.Elapsed(); + elapsed = stopwatch.Elapsed(); + EXPECT_GT(elapsed, (2-1) * test::EpsilonTimeout()); + EXPECT_LT(elapsed, (2+1) * test::EpsilonTimeout()); EXPECT_TRUE(did_wait); EXPECT_EQ(MOJO_RESULT_CANCELLED, result); - EXPECT_GT(elapsed_micros, (2-1) * kEpsilonMicros); - EXPECT_LT(elapsed_micros, (2+1) * kEpsilonMicros); EXPECT_EQ(MOJO_RESULT_OK, d0->Close()); } diff --git a/mojo/system/simple_dispatcher_unittest.cc b/mojo/system/simple_dispatcher_unittest.cc index 8c23f6c..6f232693 100644 --- a/mojo/system/simple_dispatcher_unittest.cc +++ b/mojo/system/simple_dispatcher_unittest.cc @@ -3,8 +3,9 @@ // found in the LICENSE file. // NOTE(vtl): Some of these tests are inherently flaky (e.g., if run on a -// heavily-loaded system). Sorry. |kEpsilonMicros| may be increased to increase -// tolerance and reduce observed flakiness. +// heavily-loaded system). Sorry. |test::EpsilonTimeout()| may be increased to +// increase tolerance and reduce observed flakiness (though doing so reduces the +// meaningfulness of the test). #include "mojo/system/simple_dispatcher.h" @@ -24,9 +25,6 @@ namespace mojo { namespace system { namespace { -const int64_t kMicrosPerMs = 1000; -const int64_t kEpsilonMicros = 30 * kMicrosPerMs; // 30 ms. - class MockSimpleDispatcher : public SimpleDispatcher { public: MockSimpleDispatcher() @@ -93,7 +91,6 @@ class MockSimpleDispatcher : public SimpleDispatcher { TEST(SimpleDispatcherTest, Basic) { test::Stopwatch stopwatch; - int64_t elapsed_micros; scoped_refptr<MockSimpleDispatcher> d(new MockSimpleDispatcher()); Waiter w; @@ -112,8 +109,7 @@ TEST(SimpleDispatcherTest, Basic) { d->SetSatisfiedFlags(MOJO_WAIT_FLAG_WRITABLE); stopwatch.Start(); EXPECT_EQ(1, w.Wait(MOJO_DEADLINE_INDEFINITE)); - elapsed_micros = stopwatch.Elapsed(); - EXPECT_LT(elapsed_micros, kEpsilonMicros); + EXPECT_LT(stopwatch.Elapsed(), test::EpsilonTimeout()); d->RemoveWaiter(&w); // Wait for zero time for writable when already writable. @@ -123,8 +119,7 @@ TEST(SimpleDispatcherTest, Basic) { d->SetSatisfiedFlags(MOJO_WAIT_FLAG_WRITABLE); stopwatch.Start(); EXPECT_EQ(2, w.Wait(0)); - elapsed_micros = stopwatch.Elapsed(); - EXPECT_LT(elapsed_micros, kEpsilonMicros); + EXPECT_LT(stopwatch.Elapsed(), test::EpsilonTimeout()); d->RemoveWaiter(&w); // Wait for non-zero, finite time for writable when already writable. @@ -133,9 +128,8 @@ TEST(SimpleDispatcherTest, Basic) { EXPECT_EQ(MOJO_RESULT_OK, d->AddWaiter(&w, MOJO_WAIT_FLAG_WRITABLE, 3)); d->SetSatisfiedFlags(MOJO_WAIT_FLAG_WRITABLE); stopwatch.Start(); - EXPECT_EQ(3, w.Wait(2 * kEpsilonMicros)); - elapsed_micros = stopwatch.Elapsed(); - EXPECT_LT(elapsed_micros, kEpsilonMicros); + EXPECT_EQ(3, w.Wait(2 * test::EpsilonTimeout().InMicroseconds())); + EXPECT_LT(stopwatch.Elapsed(), test::EpsilonTimeout()); d->RemoveWaiter(&w); // Wait for zero time for writable when not writable (will time out). @@ -144,8 +138,7 @@ TEST(SimpleDispatcherTest, Basic) { EXPECT_EQ(MOJO_RESULT_OK, d->AddWaiter(&w, MOJO_WAIT_FLAG_WRITABLE, 4)); stopwatch.Start(); EXPECT_EQ(MOJO_RESULT_DEADLINE_EXCEEDED, w.Wait(0)); - elapsed_micros = stopwatch.Elapsed(); - EXPECT_LT(elapsed_micros, kEpsilonMicros); + EXPECT_LT(stopwatch.Elapsed(), test::EpsilonTimeout()); d->RemoveWaiter(&w); // Wait for non-zero, finite time for writable when not writable (will time @@ -154,10 +147,11 @@ TEST(SimpleDispatcherTest, Basic) { d->SetSatisfiedFlags(MOJO_WAIT_FLAG_READABLE); EXPECT_EQ(MOJO_RESULT_OK, d->AddWaiter(&w, MOJO_WAIT_FLAG_WRITABLE, 4)); stopwatch.Start(); - EXPECT_EQ(MOJO_RESULT_DEADLINE_EXCEEDED, w.Wait(2 * kEpsilonMicros)); - elapsed_micros = stopwatch.Elapsed(); - EXPECT_GT(elapsed_micros, (2-1) * kEpsilonMicros); - EXPECT_LT(elapsed_micros, (2+1) * kEpsilonMicros); + EXPECT_EQ(MOJO_RESULT_DEADLINE_EXCEEDED, + w.Wait(2 * test::EpsilonTimeout().InMicroseconds())); + base::TimeDelta elapsed = stopwatch.Elapsed(); + EXPECT_GT(elapsed, (2-1) * test::EpsilonTimeout()); + EXPECT_LT(elapsed, (2+1) * test::EpsilonTimeout()); d->RemoveWaiter(&w); EXPECT_EQ(MOJO_RESULT_OK, d->Close()); @@ -165,7 +159,6 @@ TEST(SimpleDispatcherTest, Basic) { TEST(SimpleDispatcherTest, BasicUnsatisfiable) { test::Stopwatch stopwatch; - int64_t elapsed_micros; scoped_refptr<MockSimpleDispatcher> d(new MockSimpleDispatcher()); Waiter w; @@ -185,8 +178,7 @@ TEST(SimpleDispatcherTest, BasicUnsatisfiable) { d->SetSatisfiableFlags(MOJO_WAIT_FLAG_READABLE); stopwatch.Start(); EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, w.Wait(MOJO_DEADLINE_INDEFINITE)); - elapsed_micros = stopwatch.Elapsed(); - EXPECT_LT(elapsed_micros, kEpsilonMicros); + EXPECT_LT(stopwatch.Elapsed(), test::EpsilonTimeout()); d->RemoveWaiter(&w); // Wait for zero time for writable and then it becomes never writable. @@ -196,8 +188,7 @@ TEST(SimpleDispatcherTest, BasicUnsatisfiable) { d->SetSatisfiableFlags(MOJO_WAIT_FLAG_READABLE); stopwatch.Start(); EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, w.Wait(0)); - elapsed_micros = stopwatch.Elapsed(); - EXPECT_LT(elapsed_micros, kEpsilonMicros); + EXPECT_LT(stopwatch.Elapsed(), test::EpsilonTimeout()); d->RemoveWaiter(&w); // Wait for non-zero, finite time for writable and then it becomes never @@ -207,9 +198,9 @@ TEST(SimpleDispatcherTest, BasicUnsatisfiable) { EXPECT_EQ(MOJO_RESULT_OK, d->AddWaiter(&w, MOJO_WAIT_FLAG_WRITABLE, 7)); d->SetSatisfiableFlags(MOJO_WAIT_FLAG_READABLE); stopwatch.Start(); - EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, w.Wait(2 * kEpsilonMicros)); - elapsed_micros = stopwatch.Elapsed(); - EXPECT_LT(elapsed_micros, kEpsilonMicros); + EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, + w.Wait(2 * test::EpsilonTimeout().InMicroseconds())); + EXPECT_LT(stopwatch.Elapsed(), test::EpsilonTimeout()); d->RemoveWaiter(&w); EXPECT_EQ(MOJO_RESULT_OK, d->Close()); @@ -217,7 +208,6 @@ TEST(SimpleDispatcherTest, BasicUnsatisfiable) { TEST(SimpleDispatcherTest, BasicClosed) { test::Stopwatch stopwatch; - int64_t elapsed_micros; scoped_refptr<MockSimpleDispatcher> d; Waiter w; @@ -237,8 +227,7 @@ TEST(SimpleDispatcherTest, BasicClosed) { EXPECT_EQ(MOJO_RESULT_OK, d->Close()); stopwatch.Start(); EXPECT_EQ(MOJO_RESULT_CANCELLED, w.Wait(MOJO_DEADLINE_INDEFINITE)); - elapsed_micros = stopwatch.Elapsed(); - EXPECT_LT(elapsed_micros, kEpsilonMicros); + EXPECT_LT(stopwatch.Elapsed(), test::EpsilonTimeout()); // Don't need to remove waiters from closed dispatchers. // Wait for zero time for writable and then the dispatcher is closed. @@ -248,8 +237,7 @@ TEST(SimpleDispatcherTest, BasicClosed) { EXPECT_EQ(MOJO_RESULT_OK, d->Close()); stopwatch.Start(); EXPECT_EQ(MOJO_RESULT_CANCELLED, w.Wait(0)); - elapsed_micros = stopwatch.Elapsed(); - EXPECT_LT(elapsed_micros, kEpsilonMicros); + EXPECT_LT(stopwatch.Elapsed(), test::EpsilonTimeout()); // Don't need to remove waiters from closed dispatchers. // Wait for non-zero, finite time for writable and then the dispatcher is @@ -259,9 +247,9 @@ TEST(SimpleDispatcherTest, BasicClosed) { EXPECT_EQ(MOJO_RESULT_OK, d->AddWaiter(&w, MOJO_WAIT_FLAG_WRITABLE, 11)); EXPECT_EQ(MOJO_RESULT_OK, d->Close()); stopwatch.Start(); - EXPECT_EQ(MOJO_RESULT_CANCELLED, w.Wait(2 * kEpsilonMicros)); - elapsed_micros = stopwatch.Elapsed(); - EXPECT_LT(elapsed_micros, kEpsilonMicros); + EXPECT_EQ(MOJO_RESULT_CANCELLED, + w.Wait(2 * test::EpsilonTimeout().InMicroseconds())); + EXPECT_LT(stopwatch.Elapsed(), test::EpsilonTimeout()); // Don't need to remove waiters from closed dispatchers. } @@ -269,7 +257,6 @@ TEST(SimpleDispatcherTest, BasicThreaded) { test::Stopwatch stopwatch; bool did_wait; MojoResult result; - int64_t elapsed_micros; // Wait for readable (already readable). { @@ -287,10 +274,9 @@ TEST(SimpleDispatcherTest, BasicThreaded) { // If we closed earlier, then probably we'd get a |MOJO_RESULT_CANCELLED|. EXPECT_EQ(MOJO_RESULT_OK, d->Close()); } - elapsed_micros = stopwatch.Elapsed(); + EXPECT_LT(stopwatch.Elapsed(), test::EpsilonTimeout()); EXPECT_FALSE(did_wait); EXPECT_EQ(MOJO_RESULT_ALREADY_EXISTS, result); - EXPECT_LT(elapsed_micros, kEpsilonMicros); // Wait for readable and becomes readable after some time. { @@ -302,16 +288,15 @@ TEST(SimpleDispatcherTest, BasicThreaded) { &did_wait, &result); stopwatch.Start(); thread.Start(); - base::PlatformThread::Sleep( - base::TimeDelta::FromMicroseconds(2 * kEpsilonMicros)); + base::PlatformThread::Sleep(2 * test::EpsilonTimeout()); d->SetSatisfiedFlags(MOJO_WAIT_FLAG_READABLE); EXPECT_EQ(MOJO_RESULT_OK, d->Close()); } // Joins the thread. - elapsed_micros = stopwatch.Elapsed(); + base::TimeDelta elapsed = stopwatch.Elapsed(); + EXPECT_GT(elapsed, (2-1) * test::EpsilonTimeout()); + EXPECT_LT(elapsed, (2+1) * test::EpsilonTimeout()); EXPECT_TRUE(did_wait); EXPECT_EQ(1, result); - EXPECT_GT(elapsed_micros, (2-1) * kEpsilonMicros); - EXPECT_LT(elapsed_micros, (2+1) * kEpsilonMicros); // Wait for readable and becomes never-readable after some time. { @@ -323,16 +308,15 @@ TEST(SimpleDispatcherTest, BasicThreaded) { &did_wait, &result); stopwatch.Start(); thread.Start(); - base::PlatformThread::Sleep( - base::TimeDelta::FromMicroseconds(2 * kEpsilonMicros)); + base::PlatformThread::Sleep(2 * test::EpsilonTimeout()); d->SetSatisfiableFlags(MOJO_WAIT_FLAG_NONE); EXPECT_EQ(MOJO_RESULT_OK, d->Close()); } // Joins the thread. - elapsed_micros = stopwatch.Elapsed(); + elapsed = stopwatch.Elapsed(); + EXPECT_GT(elapsed, (2-1) * test::EpsilonTimeout()); + EXPECT_LT(elapsed, (2+1) * test::EpsilonTimeout()); EXPECT_TRUE(did_wait); EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, result); - EXPECT_GT(elapsed_micros, (2-1) * kEpsilonMicros); - EXPECT_LT(elapsed_micros, (2+1) * kEpsilonMicros); // Wait for readable and dispatcher gets closed. { @@ -344,15 +328,14 @@ TEST(SimpleDispatcherTest, BasicThreaded) { &did_wait, &result); stopwatch.Start(); thread.Start(); - base::PlatformThread::Sleep( - base::TimeDelta::FromMicroseconds(2 * kEpsilonMicros)); + base::PlatformThread::Sleep(2 * test::EpsilonTimeout()); EXPECT_EQ(MOJO_RESULT_OK, d->Close()); } // Joins the thread. - elapsed_micros = stopwatch.Elapsed(); + elapsed = stopwatch.Elapsed(); + EXPECT_GT(elapsed, (2-1) * test::EpsilonTimeout()); + EXPECT_LT(elapsed, (2+1) * test::EpsilonTimeout()); EXPECT_TRUE(did_wait); EXPECT_EQ(MOJO_RESULT_CANCELLED, result); - EXPECT_GT(elapsed_micros, (2-1) * kEpsilonMicros); - EXPECT_LT(elapsed_micros, (2+1) * kEpsilonMicros); // Wait for readable and times out. { @@ -360,24 +343,23 @@ TEST(SimpleDispatcherTest, BasicThreaded) { { test::WaiterThread thread(d, MOJO_WAIT_FLAG_READABLE, - 2 * kEpsilonMicros, + 2 * test::EpsilonTimeout().InMicroseconds(), 4, &did_wait, &result); stopwatch.Start(); thread.Start(); - base::PlatformThread::Sleep( - base::TimeDelta::FromMicroseconds(1 * kEpsilonMicros)); + base::PlatformThread::Sleep(1 * test::EpsilonTimeout()); // Not what we're waiting for. d->SetSatisfiedFlags(MOJO_WAIT_FLAG_WRITABLE); } // Joins the thread (after its wait times out). // If we closed earlier, then probably we'd get a |MOJO_RESULT_CANCELLED|. EXPECT_EQ(MOJO_RESULT_OK, d->Close()); } - elapsed_micros = stopwatch.Elapsed(); + elapsed = stopwatch.Elapsed(); + EXPECT_GT(elapsed, (2-1) * test::EpsilonTimeout()); + EXPECT_LT(elapsed, (2+1) * test::EpsilonTimeout()); EXPECT_TRUE(did_wait); EXPECT_EQ(MOJO_RESULT_DEADLINE_EXCEEDED, result); - EXPECT_GT(elapsed_micros, (2-1) * kEpsilonMicros); - EXPECT_LT(elapsed_micros, (2+1) * kEpsilonMicros); } TEST(SimpleDispatcherTest, MultipleWaiters) { @@ -398,8 +380,7 @@ TEST(SimpleDispatcherTest, MultipleWaiters) { &did_wait[i], &result[i])); threads.back()->Start(); } - base::PlatformThread::Sleep( - base::TimeDelta::FromMicroseconds(2 * kEpsilonMicros)); + base::PlatformThread::Sleep(2 * test::EpsilonTimeout()); d->SetSatisfiedFlags(MOJO_WAIT_FLAG_READABLE); EXPECT_EQ(MOJO_RESULT_OK, d->Close()); } // Joins the threads. @@ -429,8 +410,7 @@ TEST(SimpleDispatcherTest, MultipleWaiters) { &did_wait[i], &result[i])); threads.back()->Start(); } - base::PlatformThread::Sleep( - base::TimeDelta::FromMicroseconds(2 * kEpsilonMicros)); + base::PlatformThread::Sleep(2 * test::EpsilonTimeout()); d->SetSatisfiedFlags(MOJO_WAIT_FLAG_READABLE); // This will wake up the ones waiting to write. EXPECT_EQ(MOJO_RESULT_OK, d->Close()); @@ -465,11 +445,9 @@ TEST(SimpleDispatcherTest, MultipleWaiters) { &did_wait[i], &result[i])); threads.back()->Start(); } - base::PlatformThread::Sleep( - base::TimeDelta::FromMicroseconds(1 * kEpsilonMicros)); + base::PlatformThread::Sleep(1 * test::EpsilonTimeout()); d->SetSatisfiableFlags(MOJO_WAIT_FLAG_READABLE); - base::PlatformThread::Sleep( - base::TimeDelta::FromMicroseconds(1 * kEpsilonMicros)); + base::PlatformThread::Sleep(1 * test::EpsilonTimeout()); d->SetSatisfiedFlags(MOJO_WAIT_FLAG_READABLE); EXPECT_EQ(MOJO_RESULT_OK, d->Close()); } // Joins the threads. @@ -488,23 +466,24 @@ TEST(SimpleDispatcherTest, MultipleWaiters) { scoped_refptr<MockSimpleDispatcher> d(new MockSimpleDispatcher()); ScopedVector<test::WaiterThread> threads; for (size_t i = 0; i < kNumWaiters / 2; i++) { - threads.push_back(new test::WaiterThread(d, - MOJO_WAIT_FLAG_READABLE, - 3 * kEpsilonMicros, - static_cast<MojoResult>(i), - &did_wait[i], &result[i])); + threads.push_back( + new test::WaiterThread(d, + MOJO_WAIT_FLAG_READABLE, + 3 * test::EpsilonTimeout().InMicroseconds(), + static_cast<MojoResult>(i), + &did_wait[i], &result[i])); threads.back()->Start(); } for (size_t i = kNumWaiters / 2; i < kNumWaiters; i++) { - threads.push_back(new test::WaiterThread(d, - MOJO_WAIT_FLAG_WRITABLE, - 1 * kEpsilonMicros, - static_cast<MojoResult>(i), - &did_wait[i], &result[i])); + threads.push_back( + new test::WaiterThread(d, + MOJO_WAIT_FLAG_WRITABLE, + 1 * test::EpsilonTimeout().InMicroseconds(), + static_cast<MojoResult>(i), + &did_wait[i], &result[i])); threads.back()->Start(); } - base::PlatformThread::Sleep( - base::TimeDelta::FromMicroseconds(2 * kEpsilonMicros)); + base::PlatformThread::Sleep(2 * test::EpsilonTimeout()); d->SetSatisfiedFlags(MOJO_WAIT_FLAG_READABLE); // All those waiting for writable should have timed out. EXPECT_EQ(MOJO_RESULT_OK, d->Close()); diff --git a/mojo/system/test_utils.cc b/mojo/system/test_utils.cc index 1ff080b..e19fe35 100644 --- a/mojo/system/test_utils.cc +++ b/mojo/system/test_utils.cc @@ -7,6 +7,7 @@ #include "base/bind.h" #include "base/callback.h" #include "base/synchronization/waitable_event.h" +#include "base/test/test_timeouts.h" namespace mojo { namespace system { @@ -31,6 +32,14 @@ void PostTaskAndWait(scoped_refptr<base::TaskRunner> task_runner, event.Wait(); } +base::TimeDelta EpsilonTimeout() { + // Originally, our epsilon timeout was 10 ms, which was mostly fine but flaky + // on some Windows bots. So I bumped it up to 30 ms, which made things + // reliable. Currently, |tiny_timeout()| is 100 ms, which means that this will + // be 25 ms, which will hopefully be okay. + return TestTimeouts::tiny_timeout() / 4; +} + // TestIOThread ---------------------------------------------------------------- TestIOThread::TestIOThread(Mode mode) diff --git a/mojo/system/test_utils.h b/mojo/system/test_utils.h index 469edaf..c1a1a1e 100644 --- a/mojo/system/test_utils.h +++ b/mojo/system/test_utils.h @@ -23,6 +23,22 @@ namespace mojo { namespace system { namespace test { +// Posts the given task (to the given task runner) and waits for it to complete. +// (Note: Doesn't spin the current thread's message loop, so if you're careless +// this could easily deadlock.) +void PostTaskAndWait(scoped_refptr<base::TaskRunner> task_runner, + const tracked_objects::Location& from_here, + const base::Closure& task); + +// A timeout smaller than |TestTimeouts::tiny_timeout()|. Warning: This may lead +// to flakiness, but this is unavoidable if, e.g., you're trying to ensure that +// functions with timeouts are reasonably accurate. We want this to be as small +// as possible without causing too much flakiness. +base::TimeDelta EpsilonTimeout(); + +// Stopwatch ------------------------------------------------------------------- + +// A simple "stopwatch" for measuring time elapsed from a given starting point. class Stopwatch { public: Stopwatch() {} @@ -32,8 +48,8 @@ class Stopwatch { start_time_ = base::TimeTicks::HighResNow(); } - int64_t Elapsed() { - return (base::TimeTicks::HighResNow() - start_time_).InMicroseconds(); + base::TimeDelta Elapsed() { + return base::TimeTicks::HighResNow() - start_time_; } private: @@ -42,13 +58,6 @@ class Stopwatch { DISALLOW_COPY_AND_ASSIGN(Stopwatch); }; -// Posts the given task (to the given task runner) and waits for it to complete. -// (Note: Doesn't spin the current thread's message loop, so if you're careless -// this could easily deadlock.) -void PostTaskAndWait(scoped_refptr<base::TaskRunner> task_runner, - const tracked_objects::Location& from_here, - const base::Closure& task); - // TestIOThread ---------------------------------------------------------------- class TestIOThread { diff --git a/mojo/system/waiter_list_unittest.cc b/mojo/system/waiter_list_unittest.cc index b9947b6..9eeb4e7 100644 --- a/mojo/system/waiter_list_unittest.cc +++ b/mojo/system/waiter_list_unittest.cc @@ -2,14 +2,16 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -// NOTE(vtl): These tests are inherently flaky (e.g., if run on a heavily-loaded -// system). Sorry. |kEpsilonMicros| may be increased to increase tolerance and -// reduce observed flakiness. +// NOTE(vtl): Some of these tests are inherently flaky (e.g., if run on a +// heavily-loaded system). Sorry. |test::EpsilonTimeout()| may be increased to +// increase tolerance and reduce observed flakiness (though doing so reduces the +// meaningfulness of the test). #include "mojo/system/waiter_list.h" #include "base/threading/platform_thread.h" // For |Sleep()|. #include "base/time/time.h" +#include "mojo/system/test_utils.h" #include "mojo/system/waiter.h" #include "mojo/system/waiter_test_utils.h" #include "testing/gtest/include/gtest/gtest.h" @@ -18,9 +20,6 @@ namespace mojo { namespace system { namespace { -const int64_t kMicrosPerMs = 1000; -const int64_t kEpsilonMicros = 30 * kMicrosPerMs; // 30 ms. - TEST(WaiterListTest, BasicCancel) { MojoResult result; @@ -51,8 +50,7 @@ TEST(WaiterListTest, BasicCancel) { test::SimpleWaiterThread thread(&result); waiter_list.AddWaiter(thread.waiter(), MOJO_WAIT_FLAG_READABLE, 2); thread.Start(); - base::PlatformThread::Sleep( - base::TimeDelta::FromMicroseconds(2 * kEpsilonMicros)); + base::PlatformThread::Sleep(2 * test::EpsilonTimeout()); waiter_list.CancelAllWaiters(); } // Join |thread|. EXPECT_EQ(MOJO_RESULT_CANCELLED, result); @@ -94,8 +92,7 @@ TEST(WaiterListTest, BasicAwakeSatisfied) { test::SimpleWaiterThread thread(&result); waiter_list.AddWaiter(thread.waiter(), MOJO_WAIT_FLAG_READABLE, 2); thread.Start(); - base::PlatformThread::Sleep( - base::TimeDelta::FromMicroseconds(2 * kEpsilonMicros)); + base::PlatformThread::Sleep(2 * test::EpsilonTimeout()); waiter_list.AwakeWaitersForStateChange(MOJO_WAIT_FLAG_READABLE, MOJO_WAIT_FLAG_READABLE | MOJO_WAIT_FLAG_WRITABLE); @@ -136,8 +133,7 @@ TEST(WaiterListTest, BasicAwakeUnsatisfiable) { test::SimpleWaiterThread thread(&result); waiter_list.AddWaiter(thread.waiter(), MOJO_WAIT_FLAG_READABLE, 2); thread.Start(); - base::PlatformThread::Sleep( - base::TimeDelta::FromMicroseconds(2 * kEpsilonMicros)); + base::PlatformThread::Sleep(2 * test::EpsilonTimeout()); waiter_list.AwakeWaitersForStateChange(0, MOJO_WAIT_FLAG_WRITABLE); waiter_list.RemoveWaiter(thread.waiter()); waiter_list.RemoveWaiter(thread.waiter()); // Double-remove okay. @@ -160,8 +156,7 @@ TEST(WaiterListTest, MultipleWaiters) { test::SimpleWaiterThread thread2(&result2); waiter_list.AddWaiter(thread2.waiter(), MOJO_WAIT_FLAG_WRITABLE, 1); thread2.Start(); - base::PlatformThread::Sleep( - base::TimeDelta::FromMicroseconds(2 * kEpsilonMicros)); + base::PlatformThread::Sleep(2 * test::EpsilonTimeout()); waiter_list.CancelAllWaiters(); } // Join threads. EXPECT_EQ(MOJO_RESULT_CANCELLED, result1); @@ -176,8 +171,7 @@ TEST(WaiterListTest, MultipleWaiters) { test::SimpleWaiterThread thread2(&result2); waiter_list.AddWaiter(thread2.waiter(), MOJO_WAIT_FLAG_WRITABLE, 3); thread2.Start(); - base::PlatformThread::Sleep( - base::TimeDelta::FromMicroseconds(2 * kEpsilonMicros)); + base::PlatformThread::Sleep(2 * test::EpsilonTimeout()); waiter_list.AwakeWaitersForStateChange(MOJO_WAIT_FLAG_READABLE, MOJO_WAIT_FLAG_READABLE | MOJO_WAIT_FLAG_WRITABLE); @@ -196,8 +190,7 @@ TEST(WaiterListTest, MultipleWaiters) { test::SimpleWaiterThread thread2(&result2); waiter_list.AddWaiter(thread2.waiter(), MOJO_WAIT_FLAG_WRITABLE, 5); thread2.Start(); - base::PlatformThread::Sleep( - base::TimeDelta::FromMicroseconds(2 * kEpsilonMicros)); + base::PlatformThread::Sleep(2 * test::EpsilonTimeout()); waiter_list.AwakeWaitersForStateChange(0, MOJO_WAIT_FLAG_READABLE); waiter_list.RemoveWaiter(thread2.waiter()); waiter_list.CancelAllWaiters(); @@ -212,8 +205,7 @@ TEST(WaiterListTest, MultipleWaiters) { waiter_list.AddWaiter(thread1.waiter(), MOJO_WAIT_FLAG_READABLE, 6); thread1.Start(); - base::PlatformThread::Sleep( - base::TimeDelta::FromMicroseconds(1 * kEpsilonMicros)); + base::PlatformThread::Sleep(1 * test::EpsilonTimeout()); // Should do nothing. waiter_list.AwakeWaitersForStateChange(0, @@ -224,8 +216,7 @@ TEST(WaiterListTest, MultipleWaiters) { waiter_list.AddWaiter(thread2.waiter(), MOJO_WAIT_FLAG_WRITABLE, 7); thread2.Start(); - base::PlatformThread::Sleep( - base::TimeDelta::FromMicroseconds(1 * kEpsilonMicros)); + base::PlatformThread::Sleep(1 * test::EpsilonTimeout()); // Awake #1. waiter_list.AwakeWaitersForStateChange(MOJO_WAIT_FLAG_READABLE, @@ -233,8 +224,7 @@ TEST(WaiterListTest, MultipleWaiters) { MOJO_WAIT_FLAG_WRITABLE); waiter_list.RemoveWaiter(thread1.waiter()); - base::PlatformThread::Sleep( - base::TimeDelta::FromMicroseconds(1 * kEpsilonMicros)); + base::PlatformThread::Sleep(1 * test::EpsilonTimeout()); test::SimpleWaiterThread thread3(&result3); waiter_list.AddWaiter(thread3.waiter(), MOJO_WAIT_FLAG_WRITABLE, 8); @@ -244,8 +234,7 @@ TEST(WaiterListTest, MultipleWaiters) { waiter_list.AddWaiter(thread4.waiter(), MOJO_WAIT_FLAG_READABLE, 9); thread4.Start(); - base::PlatformThread::Sleep( - base::TimeDelta::FromMicroseconds(1 * kEpsilonMicros)); + base::PlatformThread::Sleep(1 * test::EpsilonTimeout()); // Awake #2 and #3 for unsatisfiability. waiter_list.AwakeWaitersForStateChange(0, MOJO_WAIT_FLAG_READABLE); diff --git a/mojo/system/waiter_unittest.cc b/mojo/system/waiter_unittest.cc index 1b04f2f..1e34e66 100644 --- a/mojo/system/waiter_unittest.cc +++ b/mojo/system/waiter_unittest.cc @@ -2,9 +2,10 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -// NOTE(vtl): These tests are inherently flaky (e.g., if run on a heavily-loaded -// system). Sorry. |kEpsilonMicros| may be increased to increase tolerance and -// reduce observed flakiness. +// NOTE(vtl): Some of these tests are inherently flaky (e.g., if run on a +// heavily-loaded system). Sorry. |test::EpsilonTimeout()| may be increased to +// increase tolerance and reduce observed flakiness (though doing so reduces the +// meaningfulness of the test). #include "mojo/system/waiter.h" @@ -22,7 +23,6 @@ namespace system { namespace { const int64_t kMicrosPerMs = 1000; -const int64_t kEpsilonMicros = 30 * kMicrosPerMs; // 30 ms. const int64_t kPollTimeMicros = 10 * kMicrosPerMs; // 10 ms. class WaitingThread : public base::SimpleThread { @@ -31,8 +31,7 @@ class WaitingThread : public base::SimpleThread { : base::SimpleThread("waiting_thread"), deadline_(deadline), done_(false), - result_(MOJO_RESULT_UNKNOWN), - elapsed_micros_(-1) { + result_(MOJO_RESULT_UNKNOWN) { waiter_.Init(); } @@ -40,13 +39,13 @@ class WaitingThread : public base::SimpleThread { Join(); } - void WaitUntilDone(MojoResult* result, int64_t* elapsed_micros) { + void WaitUntilDone(MojoResult* result, base::TimeDelta* elapsed) { for (;;) { { base::AutoLock locker(lock_); if (done_) { *result = result_; - *elapsed_micros = elapsed_micros_; + *elapsed = elapsed_; break; } } @@ -62,17 +61,17 @@ class WaitingThread : public base::SimpleThread { virtual void Run() OVERRIDE { test::Stopwatch stopwatch; MojoResult result; - int64_t elapsed_micros; + base::TimeDelta elapsed; stopwatch.Start(); result = waiter_.Wait(deadline_); - elapsed_micros = stopwatch.Elapsed(); + elapsed = stopwatch.Elapsed(); { base::AutoLock locker(lock_); done_ = true; result_ = result; - elapsed_micros_ = elapsed_micros; + elapsed_ = elapsed; } } @@ -82,71 +81,69 @@ class WaitingThread : public base::SimpleThread { base::Lock lock_; // Protects the following members. bool done_; MojoResult result_; - int64_t elapsed_micros_; + base::TimeDelta elapsed_; DISALLOW_COPY_AND_ASSIGN(WaitingThread); }; TEST(WaiterTest, Basic) { MojoResult result; - int64_t elapsed_micros; + base::TimeDelta elapsed; // Finite deadline. // Awake immediately after thread start. { - WaitingThread thread(static_cast<MojoDeadline>(10 * kEpsilonMicros)); + WaitingThread thread(10 * test::EpsilonTimeout().InMicroseconds()); thread.Start(); thread.waiter()->Awake(0); - thread.WaitUntilDone(&result, &elapsed_micros); + thread.WaitUntilDone(&result, &elapsed); EXPECT_EQ(0, result); - EXPECT_LT(elapsed_micros, kEpsilonMicros); + EXPECT_LT(elapsed, test::EpsilonTimeout()); } // Awake before after thread start. { - WaitingThread thread(static_cast<MojoDeadline>(10 * kEpsilonMicros)); + WaitingThread thread(10 * test::EpsilonTimeout().InMicroseconds()); thread.waiter()->Awake(MOJO_RESULT_CANCELLED); thread.Start(); - thread.WaitUntilDone(&result, &elapsed_micros); + thread.WaitUntilDone(&result, &elapsed); EXPECT_EQ(MOJO_RESULT_CANCELLED, result); - EXPECT_LT(elapsed_micros, kEpsilonMicros); + EXPECT_LT(elapsed, test::EpsilonTimeout()); } // Awake some time after thread start. { - WaitingThread thread(static_cast<MojoDeadline>(10 * kEpsilonMicros)); + WaitingThread thread(10 * test::EpsilonTimeout().InMicroseconds()); thread.Start(); - base::PlatformThread::Sleep( - base::TimeDelta::FromMicroseconds(2 * kEpsilonMicros)); + base::PlatformThread::Sleep(2 * test::EpsilonTimeout()); thread.waiter()->Awake(1); - thread.WaitUntilDone(&result, &elapsed_micros); + thread.WaitUntilDone(&result, &elapsed); EXPECT_EQ(1, result); - EXPECT_GT(elapsed_micros, (2-1) * kEpsilonMicros); - EXPECT_LT(elapsed_micros, (2+1) * kEpsilonMicros); + EXPECT_GT(elapsed, (2-1) * test::EpsilonTimeout()); + EXPECT_LT(elapsed, (2+1) * test::EpsilonTimeout()); } // Awake some longer time after thread start. { - WaitingThread thread(static_cast<MojoDeadline>(10 * kEpsilonMicros)); + WaitingThread thread(10 * test::EpsilonTimeout().InMicroseconds()); thread.Start(); - base::PlatformThread::Sleep( - base::TimeDelta::FromMicroseconds(5 * kEpsilonMicros)); + base::PlatformThread::Sleep(5 * test::EpsilonTimeout()); thread.waiter()->Awake(1); - thread.WaitUntilDone(&result, &elapsed_micros); + thread.WaitUntilDone(&result, &elapsed); EXPECT_EQ(1, result); - EXPECT_GT(elapsed_micros, (5-1) * kEpsilonMicros); - EXPECT_LT(elapsed_micros, (5+1) * kEpsilonMicros); + EXPECT_GT(elapsed, (5-1) * test::EpsilonTimeout()); + EXPECT_LT(elapsed, (5+1) * test::EpsilonTimeout()); } // Don't awake -- time out (on another thread). { - WaitingThread thread(static_cast<MojoDeadline>(2 * kEpsilonMicros)); + WaitingThread thread(2 * test::EpsilonTimeout().InMicroseconds()); thread.Start(); - thread.WaitUntilDone(&result, &elapsed_micros); + thread.WaitUntilDone(&result, &elapsed); EXPECT_EQ(MOJO_RESULT_DEADLINE_EXCEEDED, result); - EXPECT_GT(elapsed_micros, (2-1) * kEpsilonMicros); - EXPECT_LT(elapsed_micros, (2+1) * kEpsilonMicros); + EXPECT_GT(elapsed, (2-1) * test::EpsilonTimeout()); + EXPECT_LT(elapsed, (2+1) * test::EpsilonTimeout()); } // No (indefinite) deadline. @@ -156,9 +153,9 @@ TEST(WaiterTest, Basic) { WaitingThread thread(MOJO_DEADLINE_INDEFINITE); thread.Start(); thread.waiter()->Awake(0); - thread.WaitUntilDone(&result, &elapsed_micros); + thread.WaitUntilDone(&result, &elapsed); EXPECT_EQ(0, result); - EXPECT_LT(elapsed_micros, kEpsilonMicros); + EXPECT_LT(elapsed, test::EpsilonTimeout()); } // Awake before after thread start. @@ -166,80 +163,78 @@ TEST(WaiterTest, Basic) { WaitingThread thread(MOJO_DEADLINE_INDEFINITE); thread.waiter()->Awake(MOJO_RESULT_CANCELLED); thread.Start(); - thread.WaitUntilDone(&result, &elapsed_micros); + thread.WaitUntilDone(&result, &elapsed); EXPECT_EQ(MOJO_RESULT_CANCELLED, result); - EXPECT_LT(elapsed_micros, kEpsilonMicros); + EXPECT_LT(elapsed, test::EpsilonTimeout()); } // Awake some time after thread start. { WaitingThread thread(MOJO_DEADLINE_INDEFINITE); thread.Start(); - base::PlatformThread::Sleep( - base::TimeDelta::FromMicroseconds(2 * kEpsilonMicros)); + base::PlatformThread::Sleep(2 * test::EpsilonTimeout()); thread.waiter()->Awake(1); - thread.WaitUntilDone(&result, &elapsed_micros); + thread.WaitUntilDone(&result, &elapsed); EXPECT_EQ(1, result); - EXPECT_GT(elapsed_micros, (2-1) * kEpsilonMicros); - EXPECT_LT(elapsed_micros, (2+1) * kEpsilonMicros); + EXPECT_GT(elapsed, (2-1) * test::EpsilonTimeout()); + EXPECT_LT(elapsed, (2+1) * test::EpsilonTimeout()); } // Awake some longer time after thread start. { WaitingThread thread(MOJO_DEADLINE_INDEFINITE); thread.Start(); - base::PlatformThread::Sleep( - base::TimeDelta::FromMicroseconds(5 * kEpsilonMicros)); + base::PlatformThread::Sleep(5 * test::EpsilonTimeout()); thread.waiter()->Awake(1); - thread.WaitUntilDone(&result, &elapsed_micros); + thread.WaitUntilDone(&result, &elapsed); EXPECT_EQ(1, result); - EXPECT_GT(elapsed_micros, (5-1) * kEpsilonMicros); - EXPECT_LT(elapsed_micros, (5+1) * kEpsilonMicros); + EXPECT_GT(elapsed, (5-1) * test::EpsilonTimeout()); + EXPECT_LT(elapsed, (5+1) * test::EpsilonTimeout()); } } TEST(WaiterTest, TimeOut) { test::Stopwatch stopwatch; - int64_t elapsed_micros; + base::TimeDelta elapsed; Waiter waiter; waiter.Init(); stopwatch.Start(); EXPECT_EQ(MOJO_RESULT_DEADLINE_EXCEEDED, waiter.Wait(0)); - elapsed_micros = stopwatch.Elapsed(); - EXPECT_LT(elapsed_micros, kEpsilonMicros); + elapsed = stopwatch.Elapsed(); + EXPECT_LT(elapsed, test::EpsilonTimeout()); waiter.Init(); stopwatch.Start(); EXPECT_EQ(MOJO_RESULT_DEADLINE_EXCEEDED, - waiter.Wait(static_cast<MojoDeadline>(2 * kEpsilonMicros))); - elapsed_micros = stopwatch.Elapsed(); - EXPECT_GT(elapsed_micros, (2-1) * kEpsilonMicros); - EXPECT_LT(elapsed_micros, (2+1) * kEpsilonMicros); + waiter.Wait(2 * test::EpsilonTimeout().InMicroseconds())); + elapsed = stopwatch.Elapsed(); + EXPECT_GT(elapsed, (2-1) * test::EpsilonTimeout()); + EXPECT_LT(elapsed, (2+1) * test::EpsilonTimeout()); waiter.Init(); stopwatch.Start(); EXPECT_EQ(MOJO_RESULT_DEADLINE_EXCEEDED, - waiter.Wait(static_cast<MojoDeadline>(5 * kEpsilonMicros))); - elapsed_micros = stopwatch.Elapsed(); - EXPECT_GT(elapsed_micros, (5-1) * kEpsilonMicros); - EXPECT_LT(elapsed_micros, (5+1) * kEpsilonMicros); + waiter.Wait(5 * test::EpsilonTimeout().InMicroseconds())); + elapsed = stopwatch.Elapsed(); + EXPECT_GT(elapsed, (5-1) * test::EpsilonTimeout()); + EXPECT_LT(elapsed, (5+1) * test::EpsilonTimeout()); } // The first |Awake()| should always win. TEST(WaiterTest, MultipleAwakes) { MojoResult result; - int64_t elapsed_micros; + base::TimeDelta elapsed; { WaitingThread thread(MOJO_DEADLINE_INDEFINITE); thread.Start(); thread.waiter()->Awake(0); thread.waiter()->Awake(1); - thread.WaitUntilDone(&result, &elapsed_micros); + thread.WaitUntilDone(&result, &elapsed); EXPECT_EQ(0, result); - EXPECT_LT(elapsed_micros, kEpsilonMicros); + EXPECT_LT(elapsed, test::EpsilonTimeout()); } { @@ -247,36 +242,33 @@ TEST(WaiterTest, MultipleAwakes) { thread.waiter()->Awake(1); thread.Start(); thread.waiter()->Awake(0); - thread.WaitUntilDone(&result, &elapsed_micros); + thread.WaitUntilDone(&result, &elapsed); EXPECT_EQ(1, result); - EXPECT_LT(elapsed_micros, kEpsilonMicros); + EXPECT_LT(elapsed, test::EpsilonTimeout()); } { WaitingThread thread(MOJO_DEADLINE_INDEFINITE); thread.Start(); thread.waiter()->Awake(10); - base::PlatformThread::Sleep( - base::TimeDelta::FromMicroseconds(2 * kEpsilonMicros)); + base::PlatformThread::Sleep(2 * test::EpsilonTimeout()); thread.waiter()->Awake(20); - thread.WaitUntilDone(&result, &elapsed_micros); + thread.WaitUntilDone(&result, &elapsed); EXPECT_EQ(10, result); - EXPECT_LT(elapsed_micros, kEpsilonMicros); + EXPECT_LT(elapsed, test::EpsilonTimeout()); } { - WaitingThread thread(static_cast<MojoDeadline>(10 * kEpsilonMicros)); + WaitingThread thread(10 * test::EpsilonTimeout().InMicroseconds()); thread.Start(); - base::PlatformThread::Sleep( - base::TimeDelta::FromMicroseconds(1 * kEpsilonMicros)); + base::PlatformThread::Sleep(1 * test::EpsilonTimeout()); thread.waiter()->Awake(MOJO_RESULT_FAILED_PRECONDITION); - base::PlatformThread::Sleep( - base::TimeDelta::FromMicroseconds(2 * kEpsilonMicros)); + base::PlatformThread::Sleep(2 * test::EpsilonTimeout()); thread.waiter()->Awake(0); - thread.WaitUntilDone(&result, &elapsed_micros); + thread.WaitUntilDone(&result, &elapsed); EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, result); - EXPECT_GT(elapsed_micros, (1-1) * kEpsilonMicros); - EXPECT_LT(elapsed_micros, (1+1) * kEpsilonMicros); + EXPECT_GT(elapsed, (1-1) * test::EpsilonTimeout()); + EXPECT_LT(elapsed, (1+1) * test::EpsilonTimeout()); } } |