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