• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "base/test/test_io_thread.h"
6 
7 #include "base/logging.h"
8 #include "base/message_loop/message_loop.h"
9 
10 namespace base {
11 
TestIOThread(Mode mode)12 TestIOThread::TestIOThread(Mode mode)
13     : io_thread_("test_io_thread"), io_thread_started_(false) {
14   switch (mode) {
15     case kAutoStart:
16       Start();
17       return;
18     case kManualStart:
19       return;
20   }
21   CHECK(false) << "Invalid mode";
22 }
23 
~TestIOThread()24 TestIOThread::~TestIOThread() {
25   Stop();
26 }
27 
Start()28 void TestIOThread::Start() {
29   CHECK(!io_thread_started_);
30   io_thread_started_ = true;
31   CHECK(io_thread_.StartWithOptions(
32       base::Thread::Options(base::MessageLoop::TYPE_IO, 0)));
33 }
34 
Stop()35 void TestIOThread::Stop() {
36   // Note: It's okay to call |Stop()| even if the thread isn't running.
37   io_thread_.Stop();
38   io_thread_started_ = false;
39 }
40 
PostTask(const Location & from_here,base::OnceClosure task)41 void TestIOThread::PostTask(const Location& from_here, base::OnceClosure task) {
42   task_runner()->PostTask(from_here, std::move(task));
43 }
44 
45 }  // namespace base
46