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.
5 #include "base/test/test_io_thread.h"
8 #include "base/callback.h"
9 #include "base/synchronization/waitable_event.h"
13 void PostTaskAndWaitHelper(base::WaitableEvent
* event
,
14 const base::Closure
& task
) {
23 TestIOThread::TestIOThread(Mode mode
)
24 : io_thread_("test_io_thread"), io_thread_started_(false) {
32 CHECK(false) << "Invalid mode";
35 TestIOThread::~TestIOThread() {
39 void TestIOThread::Start() {
40 CHECK(!io_thread_started_
);
41 io_thread_started_
= true;
42 CHECK(io_thread_
.StartWithOptions(
43 base::Thread::Options(base::MessageLoop::TYPE_IO
, 0)));
46 void TestIOThread::Stop() {
47 // Note: It's okay to call |Stop()| even if the thread isn't running.
49 io_thread_started_
= false;
52 void TestIOThread::PostTask(const tracked_objects::Location
& from_here
,
53 const base::Closure
& task
) {
54 task_runner()->PostTask(from_here
, task
);
57 void TestIOThread::PostTaskAndWait(const tracked_objects::Location
& from_here
,
58 const base::Closure
& task
) {
59 base::WaitableEvent
event(false, false);
60 task_runner()->PostTask(from_here
,
61 base::Bind(&PostTaskAndWaitHelper
, &event
, task
));