Fix simulation sent_time and add a test

It was hard-coded to the actual monotonic_clock::now() call, which
doesn't work well with actual simulation when you don't use fake time.
And we want to kill fake time.

Change-Id: If5785679d54100e5021bea86df763d98d44f7c3d
diff --git a/aos/events/simulated-event-loop.cc b/aos/events/simulated-event-loop.cc
index 5fe1bf5..02d15a6 100644
--- a/aos/events/simulated-event-loop.cc
+++ b/aos/events/simulated-event-loop.cc
@@ -35,7 +35,8 @@
 
 class SimulatedSender : public RawSender {
  public:
-  SimulatedSender(SimulatedQueue *queue) : queue_(queue) {}
+  SimulatedSender(SimulatedQueue *queue, EventLoop *event_loop)
+      : queue_(queue), event_loop_(event_loop) {}
   ~SimulatedSender() {}
 
   SendContext *GetContext() override {
@@ -48,6 +49,12 @@
   }
 
   bool Send(SendContext *context) override {
+    {
+      ::aos::Message *aos_msg = reinterpret_cast<Message *>(context);
+      if (aos_msg->sent_time == monotonic_clock::min_time) {
+        aos_msg->sent_time = event_loop_->monotonic_now();
+      }
+    }
     queue_->Send(RefCountedBuffer(reinterpret_cast<aos::Message *>(context)));
     return true;  // Maybe false instead? :)
   }
@@ -56,9 +63,114 @@
 
  private:
   SimulatedQueue *queue_;
+  EventLoop *event_loop_;
 };
 }  // namespace
 
+class SimulatedTimerHandler : public TimerHandler {
+ public:
+  explicit SimulatedTimerHandler(EventScheduler *scheduler,
+                                 ::std::function<void()> fn)
+      : scheduler_(scheduler), fn_(fn) {}
+  ~SimulatedTimerHandler() {}
+
+  void Setup(monotonic_clock::time_point base,
+             monotonic_clock::duration repeat_offset) override {
+    Disable();
+    const ::aos::monotonic_clock::time_point monotonic_now =
+        scheduler_->monotonic_now();
+    base_ = base;
+    repeat_offset_ = repeat_offset;
+    if (base < monotonic_now) {
+      token_ = scheduler_->Schedule(monotonic_now, [this]() { HandleEvent(); });
+    } else {
+      token_ = scheduler_->Schedule(base, [this]() { HandleEvent(); });
+    }
+  }
+
+  void HandleEvent() {
+    const ::aos::monotonic_clock::time_point monotonic_now =
+        scheduler_->monotonic_now();
+    if (repeat_offset_ != ::aos::monotonic_clock::zero()) {
+      // Reschedule.
+      while (base_ <= monotonic_now) base_ += repeat_offset_;
+      token_ = scheduler_->Schedule(base_, [this]() { HandleEvent(); });
+    } else {
+      token_ = EventScheduler::Token();
+    }
+    fn_();
+  }
+
+  void Disable() override {
+    if (token_ != EventScheduler::Token()) {
+      scheduler_->Deschedule(token_);
+      token_ = EventScheduler::Token();
+    }
+  }
+
+ private:
+  EventScheduler *scheduler_;
+  EventScheduler::Token token_;
+  // Function to be run on the thread
+  ::std::function<void()> fn_;
+  monotonic_clock::time_point base_;
+  monotonic_clock::duration repeat_offset_;
+};
+
+
+class SimulatedEventLoop : public EventLoop {
+ public:
+  explicit SimulatedEventLoop(
+      EventScheduler *scheduler,
+      ::std::map<::std::pair<::std::string, QueueTypeInfo>, SimulatedQueue>
+          *queues)
+      : scheduler_(scheduler), queues_(queues) {}
+  ~SimulatedEventLoop() override {};
+
+  ::aos::monotonic_clock::time_point monotonic_now() override {
+    return scheduler_->monotonic_now();
+  }
+
+  ::std::unique_ptr<RawSender> MakeRawSender(
+      const ::std::string &path, const QueueTypeInfo &type) override;
+
+  ::std::unique_ptr<RawFetcher> MakeRawFetcher(
+      const ::std::string &path, const QueueTypeInfo &type) override;
+
+  void MakeRawWatcher(
+      const ::std::string &path, const QueueTypeInfo &type,
+      ::std::function<void(const ::aos::Message *message)> watcher) override;
+
+  TimerHandler *AddTimer(::std::function<void()> callback) override {
+    timers_.emplace_back(new SimulatedTimerHandler(scheduler_, callback));
+    return timers_.back().get();
+  }
+
+  void OnRun(::std::function<void()> on_run) override {
+    scheduler_->Schedule(scheduler_->monotonic_now(), on_run);
+  }
+  void Run() override {
+    set_is_running(true);
+    scheduler_->Run();
+  }
+  void Exit() override {
+    set_is_running(false);
+    scheduler_->Exit();
+  }
+
+  SimulatedQueue *GetSimulatedQueue(
+      const ::std::pair<::std::string, QueueTypeInfo> &);
+
+  void Take(const ::std::string &path);
+
+ private:
+  EventScheduler *scheduler_;
+  ::std::map<::std::pair<::std::string, QueueTypeInfo>, SimulatedQueue>
+      *queues_;
+  ::std::vector<std::string> taken_;
+  ::std::vector<std::unique_ptr<TimerHandler>> timers_;
+};
+
 EventScheduler::Token EventScheduler::Schedule(
     ::aos::monotonic_clock::time_point time, ::std::function<void()> callback) {
   return events_list_.emplace(time, callback);
@@ -91,7 +203,7 @@
     const std::string &path, const QueueTypeInfo &type) {
   Take(path);
   ::std::pair<::std::string, QueueTypeInfo> key(path, type);
-  return GetSimulatedQueue(key)->MakeRawSender();
+  return GetSimulatedQueue(key)->MakeRawSender(this);
 }
 
 std::unique_ptr<RawFetcher> SimulatedEventLoop::MakeRawFetcher(
@@ -117,8 +229,9 @@
   watchers_.push_back(watcher);
 }
 
-std::unique_ptr<RawSender> SimulatedQueue::MakeRawSender() {
-  return std::unique_ptr<RawSender>(new SimulatedSender(this));
+std::unique_ptr<RawSender> SimulatedQueue::MakeRawSender(
+    EventLoop *event_loop) {
+  return std::unique_ptr<RawSender>(new SimulatedSender(this, event_loop));
 }
 
 std::unique_ptr<RawFetcher> SimulatedQueue::MakeRawFetcher() {
@@ -136,4 +249,10 @@
     taken_.emplace_back(path);
   }
 }
+
+::std::unique_ptr<EventLoop> SimulatedEventLoopFactory::MakeEventLoop() {
+  return ::std::unique_ptr<EventLoop>(
+      new SimulatedEventLoop(&scheduler_, &queues_));
+}
+
 }  // namespace aos