Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / base / test / test_io_thread.cc
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/bind.h"
8 #include "base/callback.h"
9 #include "base/synchronization/waitable_event.h"
10
11 namespace {
12
13 void PostTaskAndWaitHelper(base::WaitableEvent* event,
14                            const base::Closure& task) {
15   task.Run();
16   event->Signal();
17 }
18
19 }  // namespace
20
21 namespace base {
22
23 TestIOThread::TestIOThread(Mode mode)
24     : io_thread_("test_io_thread"), io_thread_started_(false) {
25   switch (mode) {
26     case kAutoStart:
27       Start();
28       return;
29     case kManualStart:
30       return;
31   }
32   CHECK(false) << "Invalid mode";
33 }
34
35 TestIOThread::~TestIOThread() {
36   Stop();
37 }
38
39 void TestIOThread::Start() {
40   CHECK(!io_thread_started_);
41   io_thread_started_ = true;
42   CHECK(io_thread_.StartWithOptions(
43       base::Thread::Options(base::MessageLoop::TYPE_IO, 0)));
44 }
45
46 void TestIOThread::Stop() {
47   // Note: It's okay to call |Stop()| even if the thread isn't running.
48   io_thread_.Stop();
49   io_thread_started_ = false;
50 }
51
52 void TestIOThread::PostTask(const tracked_objects::Location& from_here,
53                             const base::Closure& task) {
54   task_runner()->PostTask(from_here, task);
55 }
56
57 void TestIOThread::PostTaskAndWait(const tracked_objects::Location& from_here,
58                                    const base::Closure& task) {
59   base::WaitableEvent event(false, false);
60   task_runner()->PostTask(from_here,
61                           base::Bind(&PostTaskAndWaitHelper, &event, task));
62   event.Wait();
63 }
64
65 }  // namespace base