blob: 0af025152ad7d54d7c4a036dfbdcaef39e23d4a3 [file] [log] [blame]
Alex Deymo23949d42014-02-05 15:20:59 -08001// Copyright (c) 2014 The Chromium OS 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
Alex Deymo23949d42014-02-05 15:20:59 -08005#include <string>
6
Alex Deymo53556ec2014-03-17 10:05:57 -07007#include <base/bind.h>
Alex Deymo53556ec2014-03-17 10:05:57 -07008#include <gtest/gtest.h>
9
Alex Deymo41a75a72014-04-15 15:36:22 -070010#include "update_engine/fake_clock.h"
Alex Deymo23949d42014-02-05 15:20:59 -080011#include "update_engine/policy_manager/evaluation_context.h"
Alex Deymo53556ec2014-03-17 10:05:57 -070012#include "update_engine/policy_manager/fake_variable.h"
Alex Deymo23949d42014-02-05 15:20:59 -080013#include "update_engine/policy_manager/generic_variables.h"
Alex Deymo41a75a72014-04-15 15:36:22 -070014#include "update_engine/policy_manager/mock_variable.h"
Alex Deymo23949d42014-02-05 15:20:59 -080015#include "update_engine/policy_manager/pmtest_utils.h"
Alex Deymo53556ec2014-03-17 10:05:57 -070016#include "update_engine/test_utils.h"
Alex Deymo23949d42014-02-05 15:20:59 -080017
Alex Deymo53556ec2014-03-17 10:05:57 -070018using base::Bind;
Alex Deymo41a75a72014-04-15 15:36:22 -070019using base::Time;
Alex Deymo53556ec2014-03-17 10:05:57 -070020using base::TimeDelta;
Alex Deymo41a75a72014-04-15 15:36:22 -070021using chromeos_update_engine::FakeClock;
Alex Deymo53556ec2014-03-17 10:05:57 -070022using chromeos_update_engine::RunGMainLoopMaxIterations;
23using chromeos_update_engine::RunGMainLoopUntil;
Alex Deymo23949d42014-02-05 15:20:59 -080024using std::string;
Alex Deymo41a75a72014-04-15 15:36:22 -070025using testing::Return;
26using testing::StrictMock;
27using testing::_;
Alex Deymo23949d42014-02-05 15:20:59 -080028
Alex Deymo53556ec2014-03-17 10:05:57 -070029namespace {
30
31void DoNothing() {}
32
33// Sets the value of the passed pointer to true.
34void SetTrue(bool* value) {
35 *value = true;
36}
37
38bool GetBoolean(bool* value) {
39 return *value;
40}
41
42} // namespace
43
Alex Deymo23949d42014-02-05 15:20:59 -080044namespace chromeos_policy_manager {
45
46class PmEvaluationContextTest : public ::testing::Test {
Alex Deymo23949d42014-02-05 15:20:59 -080047 protected:
48 virtual void SetUp() {
Alex Deymo41a75a72014-04-15 15:36:22 -070049 // Set the clock to a fixed values.
50 fake_clock_.SetMonotonicTime(Time::FromInternalValue(12345678L));
51 fake_clock_.SetWallclockTime(Time::FromInternalValue(12345678901234L));
52 eval_ctx_ = new EvaluationContext(&fake_clock_);
Alex Deymo23949d42014-02-05 15:20:59 -080053 }
54
Alex Deymo53556ec2014-03-17 10:05:57 -070055 virtual void TearDown() {
56 eval_ctx_ = NULL;
57 // Check that the evaluation context removed all the observers.
58 EXPECT_TRUE(fake_int_var_.observer_list_.empty());
59 EXPECT_TRUE(fake_async_var_.observer_list_.empty());
60 EXPECT_TRUE(fake_const_var_.observer_list_.empty());
61 EXPECT_TRUE(fake_poll_var_.observer_list_.empty());
62 }
63
Alex Deymo41a75a72014-04-15 15:36:22 -070064 // TODO(deymo): Update the default timeout to the one passed on construction.
65 // See crbug.com/363790
66 base::TimeDelta default_timeout_ = base::TimeDelta::FromSeconds(5);
67
68 FakeClock fake_clock_;
Alex Deymo7b948f02014-03-10 17:01:10 -070069 scoped_refptr<EvaluationContext> eval_ctx_;
Alex Deymo53556ec2014-03-17 10:05:57 -070070
71 // FakeVariables used for testing the EvaluationContext. These are required
72 // here to prevent them from going away *before* the EvaluationContext under
73 // test does, which keeps a reference to them.
74 FakeVariable<int> fake_int_var_ = {"fake_int", kVariableModePoll};
75 FakeVariable<string> fake_async_var_ = {"fake_async", kVariableModeAsync};
76 FakeVariable<string> fake_const_var_ = {"fake_const", kVariableModeConst};
77 FakeVariable<string> fake_poll_var_ = {"fake_poll",
78 TimeDelta::FromSeconds(1)};
Alex Deymo41a75a72014-04-15 15:36:22 -070079 StrictMock<MockVariable<string>> mock_var_async_{"mock_var_async",
80 kVariableModeAsync};
81 StrictMock<MockVariable<string>> mock_var_poll_{"mock_var_poll",
82 kVariableModePoll};
Alex Deymo23949d42014-02-05 15:20:59 -080083};
84
85TEST_F(PmEvaluationContextTest, GetValueFails) {
86 // FakeVariable is initialized as returning NULL.
87 PMTEST_EXPECT_NULL(eval_ctx_->GetValue(&fake_int_var_));
88}
89
90TEST_F(PmEvaluationContextTest, GetValueFailsWithInvalidVar) {
91 PMTEST_EXPECT_NULL(eval_ctx_->GetValue(
92 reinterpret_cast<Variable<int>*>(NULL)));
93}
94
95TEST_F(PmEvaluationContextTest, GetValueReturns) {
96 const int* p_fake_int;
97
98 fake_int_var_.reset(new int(42));
99 p_fake_int = eval_ctx_->GetValue(&fake_int_var_);
100 PMTEST_ASSERT_NOT_NULL(p_fake_int);
101 EXPECT_EQ(42, *p_fake_int);
102}
103
104TEST_F(PmEvaluationContextTest, GetValueCached) {
105 const int* p_fake_int;
106
107 fake_int_var_.reset(new int(42));
108 p_fake_int = eval_ctx_->GetValue(&fake_int_var_);
109
110 // Check that if the variable changes, the EvaluationContext keeps returning
111 // the cached value.
112 fake_int_var_.reset(new int(5));
113
114 p_fake_int = eval_ctx_->GetValue(&fake_int_var_);
115 PMTEST_ASSERT_NOT_NULL(p_fake_int);
116 EXPECT_EQ(42, *p_fake_int);
117}
118
Alex Deymocc0e5cf2014-04-23 20:20:11 -0700119TEST_F(PmEvaluationContextTest, GetValueCachesNull) {
Alex Deymo23949d42014-02-05 15:20:59 -0800120 const int* p_fake_int = eval_ctx_->GetValue(&fake_int_var_);
121 PMTEST_EXPECT_NULL(p_fake_int);
122
123 fake_int_var_.reset(new int(42));
Alex Deymocc0e5cf2014-04-23 20:20:11 -0700124 // A second attempt to read the variable should not work because this
125 // EvaluationContext already got a NULL value.
Alex Deymo23949d42014-02-05 15:20:59 -0800126 p_fake_int = eval_ctx_->GetValue(&fake_int_var_);
Alex Deymocc0e5cf2014-04-23 20:20:11 -0700127 PMTEST_EXPECT_NULL(p_fake_int);
Alex Deymo23949d42014-02-05 15:20:59 -0800128}
129
130TEST_F(PmEvaluationContextTest, GetValueMixedTypes) {
Alex Deymo23949d42014-02-05 15:20:59 -0800131 const int* p_fake_int;
132 const string* p_fake_string;
133
134 fake_int_var_.reset(new int(42));
Alex Deymo53556ec2014-03-17 10:05:57 -0700135 fake_poll_var_.reset(new string("Hello world!"));
Alex Deymo23949d42014-02-05 15:20:59 -0800136 // Check that the EvaluationContext can handle multiple Variable types. This
137 // is mostly a compile-time check due to the template nature of this method.
138 p_fake_int = eval_ctx_->GetValue(&fake_int_var_);
Alex Deymo53556ec2014-03-17 10:05:57 -0700139 p_fake_string = eval_ctx_->GetValue(&fake_poll_var_);
Alex Deymo23949d42014-02-05 15:20:59 -0800140
141 PMTEST_ASSERT_NOT_NULL(p_fake_int);
142 EXPECT_EQ(42, *p_fake_int);
143
144 PMTEST_ASSERT_NOT_NULL(p_fake_string);
145 EXPECT_EQ("Hello world!", *p_fake_string);
146}
147
Alex Deymo53556ec2014-03-17 10:05:57 -0700148// Test that we don't schedule an event if there's no variable to wait for.
149TEST_F(PmEvaluationContextTest, RunOnValueChangeOrTimeoutWithoutVariablesTest) {
150 fake_const_var_.reset(new string("Hello world!"));
151 EXPECT_EQ(*eval_ctx_->GetValue(&fake_const_var_), "Hello world!");
152
153 EXPECT_FALSE(eval_ctx_->RunOnValueChangeOrTimeout(Bind(&DoNothing)));
154}
155
156// Test that we don't schedule an event if there's no variable to wait for.
157TEST_F(PmEvaluationContextTest, RunOnValueChangeOrTimeoutWithVariablesTest) {
158 fake_async_var_.reset(new string("Async value"));
159 eval_ctx_->GetValue(&fake_async_var_);
160
161 bool value = false;
162 EXPECT_TRUE(eval_ctx_->RunOnValueChangeOrTimeout(Bind(&SetTrue, &value)));
163 // Check that the scheduled callback isn't run until we signal a ValueChaged.
164 RunGMainLoopMaxIterations(100);
165 EXPECT_FALSE(value);
166
167 fake_async_var_.NotifyValueChanged();
168 EXPECT_FALSE(value);
169 // Ensure that the scheduled callback isn't run until we are back on the main
170 // loop.
171 RunGMainLoopMaxIterations(100);
172 EXPECT_TRUE(value);
173}
174
175// Test that we don't re-schedule the events if we are attending one.
176TEST_F(PmEvaluationContextTest, RunOnValueChangeOrTimeoutCalledTwiceTest) {
177 fake_async_var_.reset(new string("Async value"));
178 eval_ctx_->GetValue(&fake_async_var_);
179
180 bool value = false;
181 EXPECT_TRUE(eval_ctx_->RunOnValueChangeOrTimeout(Bind(&SetTrue, &value)));
182 EXPECT_FALSE(eval_ctx_->RunOnValueChangeOrTimeout(Bind(&SetTrue, &value)));
183
184 // The scheduled event should still work.
185 fake_async_var_.NotifyValueChanged();
186 RunGMainLoopMaxIterations(100);
187 EXPECT_TRUE(value);
188}
189
190// Test that we clear the events when destroying the EvaluationContext.
191TEST_F(PmEvaluationContextTest, RemoveObserversAndTimeoutTest) {
192 fake_async_var_.reset(new string("Async value"));
193 eval_ctx_->GetValue(&fake_async_var_);
194
195 bool value = false;
196 EXPECT_TRUE(eval_ctx_->RunOnValueChangeOrTimeout(Bind(&SetTrue, &value)));
197 eval_ctx_ = NULL;
198
199 // This should not trigger the callback since the EvaluationContext waiting
200 // for it is gone, and it should have remove all its observers.
201 fake_async_var_.NotifyValueChanged();
202 RunGMainLoopMaxIterations(100);
203 EXPECT_FALSE(value);
204}
205
206// Test that we don't schedule an event if there's no variable to wait for.
207TEST_F(PmEvaluationContextTest, RunOnValueChangeOrTimeoutRunsFromTimeoutTest) {
208 fake_poll_var_.reset(new string("Polled value"));
209 eval_ctx_->GetValue(&fake_poll_var_);
210
211 bool value = false;
212 EXPECT_TRUE(eval_ctx_->RunOnValueChangeOrTimeout(Bind(&SetTrue, &value)));
213 // Check that the scheduled callback isn't run until the timeout occurs.
214 RunGMainLoopMaxIterations(10);
215 EXPECT_FALSE(value);
216 RunGMainLoopUntil(10000, Bind(&GetBoolean, &value));
217 EXPECT_TRUE(value);
218}
219
Alex Deymodb799532014-03-21 13:00:00 -0700220// Test that we can delete the EvaluationContext while having pending events.
221TEST_F(PmEvaluationContextTest, ObjectDeletedWithPendingEventsTest) {
222 fake_async_var_.reset(new string("Async value"));
223 fake_poll_var_.reset(new string("Polled value"));
224 eval_ctx_->GetValue(&fake_async_var_);
225 eval_ctx_->GetValue(&fake_poll_var_);
226 EXPECT_TRUE(eval_ctx_->RunOnValueChangeOrTimeout(Bind(&DoNothing)));
227 // TearDown() checks for leaked observers on this async_variable, which means
228 // that our object is still alive after removing its reference.
229}
230
231// Test that timed events fired after removal of the EvaluationContext don't
232// crash.
233TEST_F(PmEvaluationContextTest, TimeoutEventAfterDeleteTest) {
234 FakeVariable<string> fake_short_poll_var = {"fake_short_poll", TimeDelta()};
235 fake_short_poll_var.reset(new string("Polled value"));
236 eval_ctx_->GetValue(&fake_short_poll_var);
237 bool value = false;
238 EXPECT_TRUE(eval_ctx_->RunOnValueChangeOrTimeout(Bind(&SetTrue, &value)));
239 // Remove the last reference to the EvaluationContext and run the loop for
240 // 1 second to give time to the main loop to trigger the timeout Event (of 0
241 // seconds). Our callback should not be called because the EvaluationContext
242 // was removed before the timeout event is attended.
243 eval_ctx_ = NULL;
244 RunGMainLoopUntil(1000, Bind(&GetBoolean, &value));
245 EXPECT_FALSE(value);
246}
247
Alex Deymo41a75a72014-04-15 15:36:22 -0700248TEST_F(PmEvaluationContextTest, DefaultTimeout) {
249 // Test that the RemainingTime() uses the default timeout on setup.
250 EXPECT_CALL(mock_var_async_, GetValue(default_timeout_, _))
251 .WillOnce(Return(nullptr));
252 PMTEST_EXPECT_NULL(eval_ctx_->GetValue(&mock_var_async_));
253}
254
255TEST_F(PmEvaluationContextTest, TimeoutUpdatesWithMonotonicTime) {
256 fake_clock_.SetMonotonicTime(
257 fake_clock_.GetMonotonicTime() + TimeDelta::FromSeconds(1));
258
259 TimeDelta timeout = default_timeout_ - TimeDelta::FromSeconds(1);
260
261 EXPECT_CALL(mock_var_async_, GetValue(timeout, _))
262 .WillOnce(Return(nullptr));
263 PMTEST_EXPECT_NULL(eval_ctx_->GetValue(&mock_var_async_));
264}
265
266TEST_F(PmEvaluationContextTest, ResetEvaluationResetsTimes) {
267 base::Time cur_time = fake_clock_.GetWallclockTime();
268 // Advance the time on the clock but don't call ResetEvaluation yet.
269 fake_clock_.SetWallclockTime(cur_time + TimeDelta::FromSeconds(4));
270
271 EXPECT_TRUE(eval_ctx_->IsTimeGreaterThan(cur_time -
272 TimeDelta::FromSeconds(1)));
273 EXPECT_FALSE(eval_ctx_->IsTimeGreaterThan(cur_time));
274 EXPECT_FALSE(eval_ctx_->IsTimeGreaterThan(cur_time +
275 TimeDelta::FromSeconds(1)));
276 // Call ResetEvaluation now, which should use the new evaluation time.
277 eval_ctx_->ResetEvaluation();
278
279 cur_time = fake_clock_.GetWallclockTime();
280 EXPECT_TRUE(eval_ctx_->IsTimeGreaterThan(cur_time -
281 TimeDelta::FromSeconds(1)));
282 EXPECT_FALSE(eval_ctx_->IsTimeGreaterThan(cur_time));
283 EXPECT_FALSE(eval_ctx_->IsTimeGreaterThan(cur_time +
284 TimeDelta::FromSeconds(1)));
285}
286
287TEST_F(PmEvaluationContextTest, IsTimeGreaterThanSignalsTriggerReevaluation) {
288 EXPECT_FALSE(eval_ctx_->IsTimeGreaterThan(
289 fake_clock_.GetWallclockTime() + TimeDelta::FromSeconds(1)));
290
291 // The "false" from IsTimeGreaterThan means that's not that timestamp yet, so
292 // this should schedule a callback for when that happens.
293 EXPECT_TRUE(eval_ctx_->RunOnValueChangeOrTimeout(Bind(&DoNothing)));
294}
295
296TEST_F(PmEvaluationContextTest, IsTimeGreaterThanDoesntRecordPastTimestamps) {
297 // IsTimeGreaterThan() should ignore timestamps on the past for reevaluation.
298 EXPECT_TRUE(eval_ctx_->IsTimeGreaterThan(
299 fake_clock_.GetWallclockTime() - TimeDelta::FromSeconds(20)));
300 EXPECT_TRUE(eval_ctx_->IsTimeGreaterThan(
301 fake_clock_.GetWallclockTime() - TimeDelta::FromSeconds(1)));
302
303 // Callback should not be scheduled.
304 EXPECT_FALSE(eval_ctx_->RunOnValueChangeOrTimeout(Bind(&DoNothing)));
305}
306
Alex Deymo23949d42014-02-05 15:20:59 -0800307} // namespace chromeos_policy_manager