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 3// found in the LICENSE file. 4 5#include "mojo/system/test_utils.h" 6 7#include "base/bind.h" 8#include "base/callback.h" 9#include "base/synchronization/waitable_event.h" 10#include "base/test/test_timeouts.h" 11#include "build/build_config.h" 12 13namespace mojo { 14namespace system { 15namespace test { 16 17namespace { 18 19void PostTaskAndWaitHelper(base::WaitableEvent* event, 20 const base::Closure& task) { 21 task.Run(); 22 event->Signal(); 23} 24 25} // namespace 26 27void PostTaskAndWait(scoped_refptr<base::TaskRunner> task_runner, 28 const tracked_objects::Location& from_here, 29 const base::Closure& task) { 30 base::WaitableEvent event(false, false); 31 task_runner->PostTask(from_here, 32 base::Bind(&PostTaskAndWaitHelper, &event, task)); 33 event.Wait(); 34} 35 36base::TimeDelta EpsilonTimeout() { 37 // Originally, our epsilon timeout was 10 ms, which was mostly fine but flaky 38 // on some Windows bots. I don't recall ever seeing flakes on other bots. At 39 // 30 ms tests seem reliable on Windows bots, but not at 25 ms. We'd like this 40 // timeout to be as small as possible (see the description in the .h file). 41 // 42 // Currently, |tiny_timeout()| is usually 100 ms (possibly scaled under ASAN, 43 // etc.). Based on this, set it to (usually be) 30 ms on Windows and 20 ms 44 // elsewhere. 45#if defined(OS_WIN) 46 return (TestTimeouts::tiny_timeout() * 3) / 10; 47#else 48 return (TestTimeouts::tiny_timeout() * 2) / 10; 49#endif 50} 51 52// TestIOThread ---------------------------------------------------------------- 53 54TestIOThread::TestIOThread(Mode mode) 55 : io_thread_("test_io_thread"), 56 io_thread_started_(false) { 57 switch (mode) { 58 case kAutoStart: 59 Start(); 60 return; 61 case kManualStart: 62 return; 63 } 64 CHECK(false) << "Invalid mode"; 65} 66 67TestIOThread::~TestIOThread() { 68 Stop(); 69} 70 71void TestIOThread::Start() { 72 CHECK(!io_thread_started_); 73 io_thread_started_ = true; 74 CHECK(io_thread_.StartWithOptions( 75 base::Thread::Options(base::MessageLoop::TYPE_IO, 0))); 76} 77 78void TestIOThread::Stop() { 79 // Note: It's okay to call |Stop()| even if the thread isn't running. 80 io_thread_.Stop(); 81 io_thread_started_ = false; 82} 83 84void TestIOThread::PostTask(const tracked_objects::Location& from_here, 85 const base::Closure& task) { 86 task_runner()->PostTask(from_here, task); 87} 88 89void TestIOThread::PostTaskAndWait(const tracked_objects::Location& from_here, 90 const base::Closure& task) { 91 ::mojo::system::test::PostTaskAndWait(task_runner(), from_here, task); 92} 93 94} // namespace test 95} // namespace system 96} // namespace mojo 97