• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2012 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 #ifndef BASE_TEST_TEST_SIMPLE_TASK_RUNNER_H_
6 #define BASE_TEST_TEST_SIMPLE_TASK_RUNNER_H_
7 
8 #include <deque>
9 
10 #include "base/callback.h"
11 #include "base/compiler_specific.h"
12 #include "base/macros.h"
13 #include "base/single_thread_task_runner.h"
14 #include "base/synchronization/lock.h"
15 #include "base/test/test_pending_task.h"
16 #include "base/threading/platform_thread.h"
17 
18 namespace base {
19 
20 class TimeDelta;
21 
22 // TestSimpleTaskRunner is a simple TaskRunner implementation that can
23 // be used for testing.  It implements SingleThreadTaskRunner as that
24 // interface implements SequencedTaskRunner, which in turn implements
25 // TaskRunner, so TestSimpleTaskRunner can be passed in to a function
26 // that accepts any *TaskRunner object.
27 //
28 // TestSimpleTaskRunner has the following properties which make it simple:
29 //
30 //   - Tasks are simply stored in a queue in FIFO order, ignoring delay
31 //     and nestability.
32 //   - Tasks aren't guaranteed to be destroyed immediately after
33 //     they're run.
34 //
35 // However, TestSimpleTaskRunner allows for reentrancy, in that it
36 // handles the running of tasks that in turn call back into itself
37 // (e.g., to post more tasks).
38 //
39 // Note that, like any TaskRunner, TestSimpleTaskRunner is
40 // ref-counted.
41 class TestSimpleTaskRunner : public SingleThreadTaskRunner {
42  public:
43   TestSimpleTaskRunner();
44 
45   // SingleThreadTaskRunner implementation.
46   bool PostDelayedTask(const tracked_objects::Location& from_here,
47                        OnceClosure task,
48                        TimeDelta delay) override;
49   bool PostNonNestableDelayedTask(const tracked_objects::Location& from_here,
50                                   OnceClosure task,
51                                   TimeDelta delay) override;
52 
53   bool RunsTasksOnCurrentThread() const override;
54 
55   std::deque<TestPendingTask> TakePendingTasks();
56   size_t NumPendingTasks() const;
57   bool HasPendingTask() const;
58   base::TimeDelta NextPendingTaskDelay() const;
59   base::TimeDelta FinalPendingTaskDelay() const;
60 
61   // Clears the queue of pending tasks without running them.
62   void ClearPendingTasks();
63 
64   // Runs each current pending task in order and clears the queue. Tasks posted
65   // by the tasks that run within this call do not run within this call. Can
66   // only be called on the thread that created this TestSimpleTaskRunner.
67   void RunPendingTasks();
68 
69   // Runs pending tasks until the queue is empty. Can only be called on the
70   // thread that created this TestSimpleTaskRunner.
71   void RunUntilIdle();
72 
73  protected:
74   ~TestSimpleTaskRunner() override;
75 
76  private:
77   // Thread on which this was instantiated.
78   const PlatformThreadRef thread_ref_ = PlatformThread::CurrentRef();
79 
80   // Synchronizes access to |pending_tasks_|.
81   mutable Lock lock_;
82 
83   std::deque<TestPendingTask> pending_tasks_;
84 
85   DISALLOW_COPY_AND_ASSIGN(TestSimpleTaskRunner);
86 };
87 
88 }  // namespace base
89 
90 #endif  // BASE_TEST_TEST_SIMPLE_TASK_RUNNER_H_
91