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