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/event_loop_unittest.cc b/policy_manager/event_loop_unittest.cc
new file mode 100644
index 0000000..e568594
--- /dev/null
+++ b/policy_manager/event_loop_unittest.cc
@@ -0,0 +1,62 @@
+// Copyright (c) 2014 The Chromium OS Authors. All rights reserved.
+// 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/event_loop.h"
+
+#include <base/bind.h>
+#include <gtest/gtest.h>
+
+#include "update_engine/test_utils.h"
+
+using base::Bind;
+using base::TimeDelta;
+using chromeos_update_engine::RunGMainLoopMaxIterations;
+using chromeos_update_engine::RunGMainLoopUntil;
+
+namespace {
+
+// Sets the value of the passed pointer to true.
+void SetTrue(bool* value) {
+  *value = true;
+}
+
+bool GetBoolean(bool* value) {
+  return *value;
+}
+
+}  // namespace
+
+namespace chromeos_policy_manager {
+
+class EventLoopTest : public ::testing::Test {};
+
+TEST(EventLoopTest, RunFromMainLoopTest) {
+  bool called = false;
+  EventId ev = RunFromMainLoop(Bind(SetTrue, &called));
+  EXPECT_NE(0, ev);
+  RunGMainLoopMaxIterations(100);
+  EXPECT_TRUE(called);
+}
+
+// Tests that we can cancel events right after we schedule them.
+TEST(EventLoopTest, RunFromMainLoopCancelTest) {
+  bool called = false;
+  EventId ev = RunFromMainLoop(Bind(SetTrue, &called));
+  EXPECT_NE(0, ev);
+  EXPECT_TRUE(CancelMainLoopEvent(ev));
+  RunGMainLoopMaxIterations(100);
+  EXPECT_FALSE(called);
+}
+
+TEST(EventLoopTest, RunFromMainLoopAfterTimeoutTest) {
+  bool called = false;
+  EventId ev = RunFromMainLoopAfterTimeout(Bind(SetTrue, &called),
+                                           TimeDelta::FromSeconds(1));
+  EXPECT_NE(0, ev);
+  RunGMainLoopUntil(10000, Bind(GetBoolean, &called));
+  // Check that the main loop finished before the 10 seconds timeout.
+  EXPECT_TRUE(called);
+}
+
+}  // namespace chromeos_policy_manager