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 #include <weave/provider/test/fake_task_runner.h> 6 7 namespace weave { 8 namespace provider { 9 namespace test { 10 11 class FakeTaskRunner::TestClock : public base::Clock { 12 public: Now()13 base::Time Now() override { return now_; } 14 SetNow(base::Time now)15 void SetNow(base::Time now) { now_ = now; } 16 17 private: 18 base::Time now_{base::Time::Now()}; 19 }; 20 FakeTaskRunner()21FakeTaskRunner::FakeTaskRunner() : test_clock_{new TestClock} {} 22 ~FakeTaskRunner()23FakeTaskRunner::~FakeTaskRunner() {} 24 RunOnce()25bool FakeTaskRunner::RunOnce() { 26 if (queue_.empty()) 27 return false; 28 auto top = queue_.top(); 29 queue_.pop(); 30 test_clock_->SetNow(std::max(test_clock_->Now(), top.first.first)); 31 top.second.Run(); 32 return true; 33 } 34 Run(size_t number_of_iterations)35void FakeTaskRunner::Run(size_t number_of_iterations) { 36 break_ = false; 37 for (size_t i = 0; i < number_of_iterations && !break_ && RunOnce(); ++i) { 38 } 39 } 40 Break()41void FakeTaskRunner::Break() { 42 break_ = true; 43 } 44 GetClock()45base::Clock* FakeTaskRunner::GetClock() { 46 return test_clock_.get(); 47 } 48 PostDelayedTask(const tracked_objects::Location & from_here,const base::Closure & task,base::TimeDelta delay)49void FakeTaskRunner::PostDelayedTask(const tracked_objects::Location& from_here, 50 const base::Closure& task, 51 base::TimeDelta delay) { 52 queue_.emplace(std::make_pair(test_clock_->Now() + delay, ++counter_), task); 53 } 54 GetTaskQueueSize() const55size_t FakeTaskRunner::GetTaskQueueSize() const { 56 return queue_.size(); 57 } 58 59 } // namespace test 60 } // namespace provider 61 } // namespace weave 62