xref: /aosp_15_r20/external/cronet/base/test/null_task_runner.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2012 The Chromium Authors
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_NULL_TASK_RUNNER_H_
6 #define BASE_TEST_NULL_TASK_RUNNER_H_
7 
8 #include "base/compiler_specific.h"
9 #include "base/functional/callback.h"
10 #include "base/task/single_thread_task_runner.h"
11 
12 namespace base {
13 
14 // ATTENTION: Prefer SingleThreadTaskEnvironment or TaskEnvironment w/
15 // ThreadPoolExecutionMode::QUEUED over this class. A NullTaskRunner might seem
16 // appealing, but not running tasks is under-testing the potential side-effects
17 // of the code under tests. All tests should be okay if tasks born from their
18 // actions are run or deleted at a later point.
19 //
20 // Helper class for tests that need to provide an implementation of a
21 // *TaskRunner class but don't actually care about tasks being run.
22 class NullTaskRunner : public base::SingleThreadTaskRunner {
23  public:
24   NullTaskRunner();
25 
26   NullTaskRunner(const NullTaskRunner&) = delete;
27   NullTaskRunner& operator=(const NullTaskRunner&) = delete;
28 
29   bool PostDelayedTask(const Location& from_here,
30                        base::OnceClosure task,
31                        base::TimeDelta delay) override;
32   bool PostNonNestableDelayedTask(const Location& from_here,
33                                   base::OnceClosure task,
34                                   base::TimeDelta delay) override;
35   // Always returns true to avoid triggering DCHECKs.
36   bool RunsTasksInCurrentSequence() const override;
37 
38  protected:
39   ~NullTaskRunner() override;
40 };
41 
42 }  // namespace base
43 
44 #endif  // BASE_TEST_NULL_TASK_RUNNER_H_
45