test_io_thread.cc 1.1 KB

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