fake_single_thread_task_runner.h 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. // Copyright 2014 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. #ifndef MEDIA_BASE_FAKE_SINGLE_THREAD_TASK_RUNNER_H_
  5. #define MEDIA_BASE_FAKE_SINGLE_THREAD_TASK_RUNNER_H_
  6. #include <map>
  7. #include "base/callback_forward.h"
  8. #include "base/memory/raw_ptr.h"
  9. #include "base/task/single_thread_task_runner.h"
  10. #include "base/test/simple_test_tick_clock.h"
  11. #include "base/time/time.h"
  12. namespace media {
  13. class FakeSingleThreadTaskRunner final : public base::SingleThreadTaskRunner {
  14. public:
  15. explicit FakeSingleThreadTaskRunner(base::SimpleTestTickClock* clock);
  16. FakeSingleThreadTaskRunner(const FakeSingleThreadTaskRunner&) = delete;
  17. FakeSingleThreadTaskRunner& operator=(const FakeSingleThreadTaskRunner&) =
  18. delete;
  19. void RunTasks();
  20. // Note: Advances |clock_|.
  21. void Sleep(base::TimeDelta t);
  22. // base::SingleThreadTaskRunner implementation.
  23. bool PostDelayedTask(const base::Location& from_here,
  24. base::OnceClosure task,
  25. base::TimeDelta delay) final;
  26. bool RunsTasksInCurrentSequence() const final;
  27. // This function is currently not used, and will return false.
  28. bool PostNonNestableDelayedTask(const base::Location& from_here,
  29. base::OnceClosure task,
  30. base::TimeDelta delay) final;
  31. protected:
  32. ~FakeSingleThreadTaskRunner() final;
  33. private:
  34. const raw_ptr<base::SimpleTestTickClock> clock_;
  35. // A compound key is used to ensure FIFO execution of delayed tasks scheduled
  36. // for the same point-in-time. The second part of the key is simply a FIFO
  37. // sequence number.
  38. using TaskKey = std::pair<base::TimeTicks, unsigned int>;
  39. // Note: The std::map data structure was chosen because the entire
  40. // cast_unittests suite performed 20% faster than when using
  41. // std::priority_queue. http://crbug.com/530842
  42. std::map<TaskKey, base::OnceClosure> tasks_;
  43. bool fail_on_next_task_;
  44. };
  45. } // namespace media
  46. #endif // MEDIA_BASE_FAKE_SINGLE_THREAD_TASK_RUNNER_H_