xref: /aosp_15_r20/external/libchrome/base/test/test_io_thread.cc (revision 635a864187cb8b6c713ff48b7e790a6b21769273)
1*635a8641SAndroid Build Coastguard Worker // Copyright 2013 The Chromium Authors. All rights reserved.
2*635a8641SAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be
3*635a8641SAndroid Build Coastguard Worker // found in the LICENSE file.
4*635a8641SAndroid Build Coastguard Worker 
5*635a8641SAndroid Build Coastguard Worker #include "base/test/test_io_thread.h"
6*635a8641SAndroid Build Coastguard Worker 
7*635a8641SAndroid Build Coastguard Worker #include "base/logging.h"
8*635a8641SAndroid Build Coastguard Worker #include "base/message_loop/message_loop.h"
9*635a8641SAndroid Build Coastguard Worker 
10*635a8641SAndroid Build Coastguard Worker namespace base {
11*635a8641SAndroid Build Coastguard Worker 
TestIOThread(Mode mode)12*635a8641SAndroid Build Coastguard Worker TestIOThread::TestIOThread(Mode mode)
13*635a8641SAndroid Build Coastguard Worker     : io_thread_("test_io_thread"), io_thread_started_(false) {
14*635a8641SAndroid Build Coastguard Worker   switch (mode) {
15*635a8641SAndroid Build Coastguard Worker     case kAutoStart:
16*635a8641SAndroid Build Coastguard Worker       Start();
17*635a8641SAndroid Build Coastguard Worker       return;
18*635a8641SAndroid Build Coastguard Worker     case kManualStart:
19*635a8641SAndroid Build Coastguard Worker       return;
20*635a8641SAndroid Build Coastguard Worker   }
21*635a8641SAndroid Build Coastguard Worker   CHECK(false) << "Invalid mode";
22*635a8641SAndroid Build Coastguard Worker }
23*635a8641SAndroid Build Coastguard Worker 
~TestIOThread()24*635a8641SAndroid Build Coastguard Worker TestIOThread::~TestIOThread() {
25*635a8641SAndroid Build Coastguard Worker   Stop();
26*635a8641SAndroid Build Coastguard Worker }
27*635a8641SAndroid Build Coastguard Worker 
Start()28*635a8641SAndroid Build Coastguard Worker void TestIOThread::Start() {
29*635a8641SAndroid Build Coastguard Worker   CHECK(!io_thread_started_);
30*635a8641SAndroid Build Coastguard Worker   io_thread_started_ = true;
31*635a8641SAndroid Build Coastguard Worker   CHECK(io_thread_.StartWithOptions(
32*635a8641SAndroid Build Coastguard Worker       base::Thread::Options(base::MessageLoop::TYPE_IO, 0)));
33*635a8641SAndroid Build Coastguard Worker }
34*635a8641SAndroid Build Coastguard Worker 
Stop()35*635a8641SAndroid Build Coastguard Worker void TestIOThread::Stop() {
36*635a8641SAndroid Build Coastguard Worker   // Note: It's okay to call |Stop()| even if the thread isn't running.
37*635a8641SAndroid Build Coastguard Worker   io_thread_.Stop();
38*635a8641SAndroid Build Coastguard Worker   io_thread_started_ = false;
39*635a8641SAndroid Build Coastguard Worker }
40*635a8641SAndroid Build Coastguard Worker 
PostTask(const Location & from_here,base::OnceClosure task)41*635a8641SAndroid Build Coastguard Worker void TestIOThread::PostTask(const Location& from_here, base::OnceClosure task) {
42*635a8641SAndroid Build Coastguard Worker   task_runner()->PostTask(from_here, std::move(task));
43*635a8641SAndroid Build Coastguard Worker }
44*635a8641SAndroid Build Coastguard Worker 
45*635a8641SAndroid Build Coastguard Worker }  // namespace base
46