Fix a bug with multiple interleaved events in SimulatedEventLoop

The new test used to segfault, and now it works correctly.

Change-Id: I2dd35ee637f42d8ff926c508fa3872227fe6cdab
diff --git a/aos/events/event_loop_event.h b/aos/events/event_loop_event.h
index 6438da1..808a4fe 100644
--- a/aos/events/event_loop_event.h
+++ b/aos/events/event_loop_event.h
@@ -12,21 +12,31 @@
   virtual ~EventLoopEvent() {}
 
   bool valid() const { return event_time_ != monotonic_clock::max_time; }
-  void Invalidate() { event_time_ = monotonic_clock::max_time; }
+  void Invalidate() {
+    event_time_ = monotonic_clock::max_time;
+    generation_ = 0;
+  }
 
   monotonic_clock::time_point event_time() const {
     DCHECK(valid());
     return event_time_;
   }
-
-  virtual void HandleEvent() = 0;
-
   void set_event_time(monotonic_clock::time_point event_time) {
     event_time_ = event_time;
   }
 
+  // Internal book-keeping for EventLoop.
+  size_t generation() const {
+    DCHECK(valid());
+    return generation_;
+  }
+  void set_generation(size_t generation) { generation_ = generation; }
+
+  virtual void HandleEvent() = 0;
+
  private:
   monotonic_clock::time_point event_time_ = monotonic_clock::max_time;
+  size_t generation_ = 0;
 };
 
 // Adapter class to implement EventLoopEvent by calling HandleEvent on T.
@@ -34,7 +44,7 @@
 class EventHandler final : public EventLoopEvent {
  public:
   EventHandler(T *t) : t_(t) {}
-  ~EventHandler() = default;
+  ~EventHandler() override = default;
   void HandleEvent() override { t_->HandleEvent(); }
 
  private: