task_runner_test_template.h 6.69 KB
Newer Older
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
// 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.

// This class defines tests that implementations of TaskRunner should
// pass in order to be conformant.  Here's how you use it to test your
// implementation.
//
// Say your class is called MyTaskRunner.  Then you need to define a
// class called MyTaskRunnerTestDelegate in my_task_runner_unittest.cc
// like this:
//
//   class MyTaskRunnerTestDelegate {
//    public:
//     // Tasks posted to the task runner after this and before
//     // StopTaskRunner() is called is called should run successfully.
//     void StartTaskRunner() {
//       ...
//     }
//
//     // Should return the task runner implementation.  Only called
//     // after StartTaskRunner and before StopTaskRunner.
//     scoped_refptr<MyTaskRunner> GetTaskRunner() {
//       ...
//     }
//
//     // Stop the task runner and make sure all tasks posted before
28 29
//     // this is called are run. Caveat: delayed tasks are not run,
       // they're simply deleted.
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
//     void StopTaskRunner() {
//       ...
//     }
//   };
//
// The TaskRunnerTest test harness will have a member variable of
// this delegate type and will call its functions in the various
// tests.
//
// Then you simply #include this file as well as gtest.h and add the
// following statement to my_task_runner_unittest.cc:
//
//   INSTANTIATE_TYPED_TEST_CASE_P(
//       MyTaskRunner, TaskRunnerTest, MyTaskRunnerTestDelegate);
//
// Easy!

47 48
#ifndef BASE_TEST_TASK_RUNNER_TEST_TEMPLATE_H_
#define BASE_TEST_TASK_RUNNER_TEST_TEMPLATE_H_
49 50 51 52 53 54

#include <cstddef>
#include <map>

#include "base/basictypes.h"
#include "base/bind.h"
55
#include "base/callback.h"
56
#include "base/memory/ref_counted.h"
57
#include "base/synchronization/condition_variable.h"
58 59
#include "base/synchronization/lock.h"
#include "base/task_runner.h"
60
#include "base/threading/thread.h"
61 62 63 64 65
#include "base/tracked_objects.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace base {

66 67 68 69
namespace internal {

// Utility class that keeps track of how many times particular tasks
// are run.
70 71 72 73
class TaskTracker : public RefCountedThreadSafe<TaskTracker> {
 public:
  TaskTracker();

74 75 76 77
  // Returns a closure that runs the given task and increments the run
  // count of |i| by one.  |task| may be null.  It is guaranteed that
  // only one task wrapped by a given tracker will be run at a time.
  Closure WrapTask(const Closure& task, int i);
78 79 80

  std::map<int, int> GetTaskRunCounts() const;

81 82 83
  // Returns after the tracker observes a total of |count| task completions.
  void WaitForCompletedTasks(int count);

84 85 86 87 88
 private:
  friend class RefCountedThreadSafe<TaskTracker>;

  ~TaskTracker();

89 90
  void RunTask(const Closure& task, int i);

91
  mutable Lock lock_;
92
  std::map<int, int> task_run_counts_;
93 94
  int task_runs_;
  ConditionVariable task_runs_cv_;
95 96 97 98

  DISALLOW_COPY_AND_ASSIGN(TaskTracker);
};

99 100
}  // namespace internal

101 102 103
template <typename TaskRunnerTestDelegate>
class TaskRunnerTest : public testing::Test {
 protected:
104
  TaskRunnerTest() : task_tracker_(new internal::TaskTracker()) {}
105

106
  const scoped_refptr<internal::TaskTracker> task_tracker_;
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
  TaskRunnerTestDelegate delegate_;
};

TYPED_TEST_CASE_P(TaskRunnerTest);

// We can't really test much, since TaskRunner provides very few
// guarantees.

// Post a bunch of tasks to the task runner.  They should all
// complete.
TYPED_TEST_P(TaskRunnerTest, Basic) {
  std::map<int, int> expected_task_run_counts;

  this->delegate_.StartTaskRunner();
  scoped_refptr<TaskRunner> task_runner = this->delegate_.GetTaskRunner();
  // Post each ith task i+1 times.
  for (int i = 0; i < 20; ++i) {
124
    const Closure& ith_task = this->task_tracker_->WrapTask(Closure(), i);
125
    for (int j = 0; j < i + 1; ++j) {
126
      task_runner->PostTask(FROM_HERE, ith_task);
127 128 129 130 131 132 133 134 135 136 137 138 139
      ++expected_task_run_counts[i];
    }
  }
  this->delegate_.StopTaskRunner();

  EXPECT_EQ(expected_task_run_counts,
            this->task_tracker_->GetTaskRunCounts());
}

// Post a bunch of delayed tasks to the task runner.  They should all
// complete.
TYPED_TEST_P(TaskRunnerTest, Delayed) {
  std::map<int, int> expected_task_run_counts;
140
  int expected_total_tasks = 0;
141 142 143 144 145

  this->delegate_.StartTaskRunner();
  scoped_refptr<TaskRunner> task_runner = this->delegate_.GetTaskRunner();
  // Post each ith task i+1 times with delays from 0-i.
  for (int i = 0; i < 20; ++i) {
146
    const Closure& ith_task = this->task_tracker_->WrapTask(Closure(), i);
147
    for (int j = 0; j < i + 1; ++j) {
148 149
      task_runner->PostDelayedTask(
          FROM_HERE, ith_task, base::TimeDelta::FromMilliseconds(j));
150
      ++expected_task_run_counts[i];
151
      ++expected_total_tasks;
152 153
    }
  }
154
  this->task_tracker_->WaitForCompletedTasks(expected_total_tasks);
155 156 157 158 159 160
  this->delegate_.StopTaskRunner();

  EXPECT_EQ(expected_task_run_counts,
            this->task_tracker_->GetTaskRunCounts());
}

161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
namespace internal {

// Calls RunsTasksOnCurrentThread() on |task_runner| and expects it to
// equal |expected_value|.
void ExpectRunsTasksOnCurrentThread(
    bool expected_value,
    const scoped_refptr<TaskRunner>& task_runner);

}  // namespace internal

// Post a bunch of tasks to the task runner as well as to a separate
// thread, each checking the value of RunsTasksOnCurrentThread(),
// which should return true for the tasks posted on the task runner
// and false for the tasks posted on the separate thread.
TYPED_TEST_P(TaskRunnerTest, RunsTasksOnCurrentThread) {
  std::map<int, int> expected_task_run_counts;

  Thread thread("Non-task-runner thread");
  ASSERT_TRUE(thread.Start());
  this->delegate_.StartTaskRunner();

  scoped_refptr<TaskRunner> task_runner = this->delegate_.GetTaskRunner();
  // Post each ith task i+1 times on the task runner and i+1 times on
  // the non-task-runner thread.
  for (int i = 0; i < 20; ++i) {
    const Closure& ith_task_runner_task =
        this->task_tracker_->WrapTask(
            Bind(&internal::ExpectRunsTasksOnCurrentThread,
                 true, task_runner),
            i);
    const Closure& ith_non_task_runner_task =
        this->task_tracker_->WrapTask(
            Bind(&internal::ExpectRunsTasksOnCurrentThread,
                 false, task_runner),
            i);
    for (int j = 0; j < i + 1; ++j) {
      task_runner->PostTask(FROM_HERE, ith_task_runner_task);
      thread.message_loop()->PostTask(FROM_HERE, ith_non_task_runner_task);
      expected_task_run_counts[i] += 2;
    }
  }

  this->delegate_.StopTaskRunner();
  thread.Stop();

  EXPECT_EQ(expected_task_run_counts,
            this->task_tracker_->GetTaskRunCounts());
}
209

210 211
REGISTER_TYPED_TEST_CASE_P(
    TaskRunnerTest, Basic, Delayed, RunsTasksOnCurrentThread);
212 213 214

}  // namespace base

danakj's avatar
danakj committed
215
#endif  // BASE_TEST_TASK_RUNNER_TEST_TEMPLATE_H_