QUIC - cleanup changes to sync chromium tree with internal source.
[chromium-blink-merge.git] / base / test / test_io_thread.cc
blob48c1e16531640e8990f5d6b8a3b6490cb0d4aa5b
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"
7 #include "base/bind.h"
8 #include "base/callback.h"
9 #include "base/synchronization/waitable_event.h"
11 namespace {
13 void PostTaskAndWaitHelper(base::WaitableEvent* event,
14 const base::Closure& task) {
15 task.Run();
16 event->Signal();
19 } // namespace
21 namespace base {
23 TestIOThread::TestIOThread(Mode mode)
24 : io_thread_("test_io_thread"), io_thread_started_(false) {
25 switch (mode) {
26 case kAutoStart:
27 Start();
28 return;
29 case kManualStart:
30 return;
32 CHECK(false) << "Invalid mode";
35 TestIOThread::~TestIOThread() {
36 Stop();
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.
48 io_thread_.Stop();
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));
62 event.Wait();
65 } // namespace base