test_pending_task.h 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. #ifndef BASE_TEST_TEST_PENDING_TASK_H_
  5. #define BASE_TEST_TEST_PENDING_TASK_H_
  6. #include <string>
  7. #include "base/callback.h"
  8. #include "base/location.h"
  9. #include "base/time/time.h"
  10. #include "base/trace_event/base_tracing_forward.h"
  11. namespace base {
  12. // TestPendingTask is a helper class for test TaskRunner
  13. // implementations. See test_simple_task_runner.h for example usage.
  14. struct TestPendingTask {
  15. enum TestNestability { NESTABLE, NON_NESTABLE };
  16. TestPendingTask();
  17. TestPendingTask(const Location& location,
  18. OnceClosure task,
  19. TimeTicks post_time,
  20. TimeDelta delay,
  21. TestNestability nestability);
  22. TestPendingTask(const TestPendingTask&) = delete;
  23. TestPendingTask& operator=(const TestPendingTask&) = delete;
  24. TestPendingTask(TestPendingTask&& other);
  25. ~TestPendingTask();
  26. TestPendingTask& operator=(TestPendingTask&& other);
  27. // Returns post_time + delay.
  28. TimeTicks GetTimeToRun() const;
  29. // Returns true if this task is nestable and |other| isn't, or if
  30. // this task's time to run is strictly earlier than |other|'s time
  31. // to run.
  32. //
  33. // Note that two tasks may both have the same nestability and delay.
  34. // In that case, the caller must use some other criterion (probably
  35. // the position in some queue) to break the tie. Conveniently, the
  36. // following STL functions already do so:
  37. //
  38. // - std::min_element
  39. // - std::stable_sort
  40. //
  41. // but the following STL functions don't:
  42. //
  43. // - std::max_element
  44. // - std::sort.
  45. bool ShouldRunBefore(const TestPendingTask& other) const;
  46. Location location;
  47. OnceClosure task;
  48. TimeTicks post_time;
  49. TimeDelta delay;
  50. TestNestability nestability;
  51. // Functions for using test pending task with tracing, useful in unit
  52. // testing.
  53. void AsValueInto(base::trace_event::TracedValue* state) const;
  54. std::unique_ptr<base::trace_event::ConvertableToTraceFormat> AsValue() const;
  55. std::string ToString() const;
  56. };
  57. // gtest helpers which allow pretty printing of the tasks, very useful in unit
  58. // testing.
  59. std::ostream& operator<<(std::ostream& os, const TestPendingTask& task);
  60. void PrintTo(const TestPendingTask& task, std::ostream* os);
  61. } // namespace base
  62. #endif // BASE_TEST_TEST_PENDING_TASK_H_