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