1 // Copyright 2015 The Weave 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 LIBWEAVE_INCLUDE_WEAVE_PROVIDER_TEST_FAKE_TASK_RUNNER_H_ 6 #define LIBWEAVE_INCLUDE_WEAVE_PROVIDER_TEST_FAKE_TASK_RUNNER_H_ 7 8 #include <weave/provider/task_runner.h> 9 10 #include <algorithm> 11 #include <queue> 12 #include <utility> 13 #include <vector> 14 15 #include <base/time/clock.h> 16 17 namespace weave { 18 namespace provider { 19 namespace test { 20 21 class FakeTaskRunner : public TaskRunner { 22 public: 23 FakeTaskRunner(); 24 ~FakeTaskRunner() override; 25 26 void PostDelayedTask(const tracked_objects::Location& from_here, 27 const base::Closure& task, 28 base::TimeDelta delay) override; 29 30 bool RunOnce(); 31 void Run(size_t number_of_iterations = 1000); 32 void Break(); 33 base::Clock* GetClock(); 34 size_t GetTaskQueueSize() const; 35 36 private: 37 void SaveTask(const tracked_objects::Location& from_here, 38 const base::Closure& task, 39 base::TimeDelta delay); 40 41 using QueueItem = std::pair<std::pair<base::Time, size_t>, base::Closure>; 42 43 struct Greater { operatorGreater44 bool operator()(const QueueItem& a, const QueueItem& b) const { 45 return a.first > b.first; 46 } 47 }; 48 49 bool break_{false}; 50 size_t counter_{0}; // Keeps order of tasks with the same time. 51 52 class TestClock; 53 std::unique_ptr<TestClock> test_clock_; 54 55 std::priority_queue<QueueItem, 56 std::vector<QueueItem>, 57 FakeTaskRunner::Greater> 58 queue_; 59 }; 60 61 } // namespace test 62 } // namespace provider 63 } // namespace weave 64 65 #endif // LIBWEAVE_INCLUDE_WEAVE_PROVIDER_TEST_FAKE_TASK_RUNNER_H_ 66