summaryrefslogtreecommitdiffstats
path: root/remoting/base/auto_thread_task_runner_unittest.cc
blob: 07ea8bb434f25c889cbeef41dfe10b85a33c1926 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/bind.h"
#include "base/memory/ref_counted.h"
#include "base/message_loop.h"
#include "base/threading/thread.h"
#include "remoting/base/auto_thread_task_runner.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace {

void PostQuitTask(MessageLoop* message_loop) {
  message_loop->PostTask(FROM_HERE, MessageLoop::QuitClosure());
}

void SetFlagTask(
  scoped_refptr<base::SingleThreadTaskRunner> task_runner,
  bool* success) {
  *success = true;
}

}  // namespace

namespace remoting {

TEST(AutoThreadTaskRunnerTest, StartAndStopMain) {
  // Create a task runner.
  MessageLoop message_loop;
  scoped_refptr<AutoThreadTaskRunner> task_runner =
      new AutoThreadTaskRunner(
          message_loop.message_loop_proxy(),
          base::Bind(&PostQuitTask, &message_loop));

  // Post a task to make sure it is executed.
  bool success = false;
  message_loop.PostTask(FROM_HERE, base::Bind(&SetFlagTask, task_runner,
                                              &success));

  task_runner = NULL;
  message_loop.Run();
  EXPECT_TRUE(success);
}

TEST(AutoThreadTaskRunnerTest, StartAndStopWorker) {
  // Create a task runner.
  MessageLoop message_loop;
  scoped_refptr<AutoThreadTaskRunner> task_runner =
      new AutoThreadTaskRunner(
          message_loop.message_loop_proxy(),
          base::Bind(&PostQuitTask, &message_loop));

  // Create a child task runner.
  base::Thread thread("Child task runner");
  thread.Start();
  task_runner = new AutoThreadTaskRunner(thread.message_loop_proxy(),
                                         task_runner);

  // Post a task to make sure it is executed.
  bool success = false;
  message_loop.PostTask(FROM_HERE, base::Bind(&SetFlagTask, task_runner,
                                              &success));

  task_runner = NULL;
  message_loop.Run();
  EXPECT_TRUE(success);

  thread.Stop();
}

}  // namespace remoting