PolicyManager: Schedule re-evaluations based on variable usage.

This patch makes the EvaluationContext re-schedule a policy request
based on the variables used by that method, waiting for the Async
variables and polling the Poll variables on the suggested interval.

In order to use the main loop functions from the EvaluationContext
they were moved to its own file called event_loop.h.

BUG=chromium:340871
TEST=Unit tests added.

Change-Id: Ibfc52e4dfd12c5e1ef87b5ad9cc318f9821dcfdd
Reviewed-on: https://chromium-review.googlesource.com/190424
Reviewed-by: Gilad Arnold <garnold@chromium.org>
Commit-Queue: Alex Deymo <deymo@chromium.org>
Tested-by: Alex Deymo <deymo@chromium.org>
diff --git a/policy_manager/variable_unittest.cc b/policy_manager/variable_unittest.cc
index 2a6824a..81e1e59 100644
--- a/policy_manager/variable_unittest.cc
+++ b/policy_manager/variable_unittest.cc
@@ -2,15 +2,18 @@
 // Use of this source code is governed by a BSD-style license that can be
 // found in the LICENSE file.
 
+#include "update_engine/policy_manager/variable.h"
+
 #include <vector>
 
 #include <gtest/gtest.h>
 
-#include "update_engine/policy_manager/variable.h"
+#include "update_engine/test_utils.h"
 
 using base::TimeDelta;
 using std::string;
 using std::vector;
+using chromeos_update_engine::RunGMainLoopMaxIterations;
 
 namespace chromeos_policy_manager {
 
@@ -25,7 +28,7 @@
   virtual ~DefaultVariable() {}
 
  protected:
-  virtual const T* GetValue(base::TimeDelta /* timeout */,
+  virtual const T* GetValue(TimeDelta /* timeout */,
                             string* /* errmsg */) {
     return new T();
   }
@@ -59,7 +62,7 @@
   EXPECT_EQ(var.GetPollInterval(), TimeDelta::FromMinutes(3));
 }
 
-class BaseVariableObserver : public BaseVariable::Observer {
+class BaseVariableObserver : public BaseVariable::ObserverInterface {
  public:
   void ValueChanged(BaseVariable* variable) {
     calls_.push_back(variable);
@@ -88,18 +91,21 @@
   var.AddObserver(&observer1);
   // Simulate a value change on the variable's implementation.
   var.NotifyValueChanged();
+  ASSERT_EQ(0, observer1.calls_.size());
+  RunGMainLoopMaxIterations(100);
 
-  ASSERT_EQ(observer1.calls_.size(), 1);
+  ASSERT_EQ(1, observer1.calls_.size());
   // Check that the observer is called with the right argument.
-  EXPECT_EQ(observer1.calls_[0], &var);
+  EXPECT_EQ(&var, observer1.calls_[0]);
 
   BaseVariableObserver observer2;
   var.AddObserver(&observer2);
   var.NotifyValueChanged();
+  RunGMainLoopMaxIterations(100);
 
   // Check that all the observers are called.
-  EXPECT_EQ(observer1.calls_.size(), 2);
-  EXPECT_EQ(observer2.calls_.size(), 1);
+  EXPECT_EQ(2, observer1.calls_.size());
+  EXPECT_EQ(1, observer2.calls_.size());
 }
 
 }  // namespace chromeos_policy_manager