blob: e1937182b167ceeb78b5c56127b7c333fe6b072d [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 Deymo53556ec2014-03-17 10:05:57 -070011#include "update_engine/test_utils.h"
Alex Deymo63784a52014-05-28 10:46:14 -070012#include "update_engine/update_manager/evaluation_context.h"
13#include "update_engine/update_manager/fake_variable.h"
14#include "update_engine/update_manager/generic_variables.h"
15#include "update_engine/update_manager/mock_variable.h"
16#include "update_engine/update_manager/umtest_utils.h"
Alex Deymo23949d42014-02-05 15:20:59 -080017
Alex Deymo53556ec2014-03-17 10:05:57 -070018using base::Bind;
Gilad Arnoldfb794f42014-07-01 15:36:31 -070019using base::Closure;
Alex Deymo41a75a72014-04-15 15:36:22 -070020using base::Time;
Alex Deymo53556ec2014-03-17 10:05:57 -070021using base::TimeDelta;
Alex Deymo41a75a72014-04-15 15:36:22 -070022using chromeos_update_engine::FakeClock;
Alex Deymo53556ec2014-03-17 10:05:57 -070023using chromeos_update_engine::RunGMainLoopMaxIterations;
24using chromeos_update_engine::RunGMainLoopUntil;
Alex Deymo23949d42014-02-05 15:20:59 -080025using std::string;
Alex Deymo41a75a72014-04-15 15:36:22 -070026using testing::Return;
27using testing::StrictMock;
28using testing::_;
Alex Deymo23949d42014-02-05 15:20:59 -080029
Gilad Arnoldfb794f42014-07-01 15:36:31 -070030namespace chromeos_update_manager {
31
Alex Deymo53556ec2014-03-17 10:05:57 -070032namespace {
33
34void DoNothing() {}
35
36// Sets the value of the passed pointer to true.
37void SetTrue(bool* value) {
38 *value = true;
39}
40
41bool GetBoolean(bool* value) {
42 return *value;
43}
44
Gilad Arnoldfb794f42014-07-01 15:36:31 -070045template<typename T>
46void ReadVar(scoped_refptr<EvaluationContext> ec, Variable<T>* var) {
47 ec->GetValue(var);
48}
Alex Deymo53556ec2014-03-17 10:05:57 -070049
Gilad Arnoldfb794f42014-07-01 15:36:31 -070050// Runs |evaluation|; if the value pointed by |count_p| is greater than zero,
51// decrement it and schedule a reevaluation; otherwise, writes true to |done_p|.
52void EvaluateRepeatedly(Closure evaluation, scoped_refptr<EvaluationContext> ec,
53 int* count_p, bool* done_p) {
54 evaluation.Run();
55
56 // Schedule reevaluation if needed.
57 if (*count_p > 0) {
58 Closure closure = Bind(EvaluateRepeatedly, evaluation, ec, count_p, done_p);
59 ASSERT_TRUE(ec->RunOnValueChangeOrTimeout(closure))
60 << "Failed to schedule reevaluation, count_p=" << *count_p;
61 (*count_p)--;
62 } else {
63 *done_p = true;
64 }
65}
66
67} // namespace
Alex Deymo23949d42014-02-05 15:20:59 -080068
Alex Deymo63784a52014-05-28 10:46:14 -070069class UmEvaluationContextTest : public ::testing::Test {
Alex Deymo23949d42014-02-05 15:20:59 -080070 protected:
71 virtual void SetUp() {
Gilad Arnolda65fced2014-07-23 09:01:31 -070072 // Apr 22, 2009 19:25:00 UTC (this is a random reference point).
73 fake_clock_.SetMonotonicTime(Time::FromTimeT(1240428300));
74 // Mar 2, 2006 1:23:45 UTC.
David Zeuthenc1490282014-04-29 16:25:03 -070075 fake_clock_.SetWallclockTime(Time::FromTimeT(1141262625));
Gilad Arnold83ffdda2014-08-08 13:30:31 -070076 eval_ctx_ = new EvaluationContext(
77 &fake_clock_, default_timeout_, default_timeout_,
78 scoped_ptr<base::Callback<void(EvaluationContext*)>>(nullptr));
Alex Deymo23949d42014-02-05 15:20:59 -080079 }
80
Alex Deymo53556ec2014-03-17 10:05:57 -070081 virtual void TearDown() {
Gilad Arnoldfb794f42014-07-01 15:36:31 -070082 // Ensure that the evaluation context did not leak and is actually being
83 // destroyed.
84 if (eval_ctx_) {
85 base::WeakPtr<EvaluationContext> eval_ctx_weak_alias =
86 eval_ctx_->weak_ptr_factory_.GetWeakPtr();
87 UMTEST_ASSERT_NOT_NULL(eval_ctx_weak_alias.get());
88 eval_ctx_ = nullptr;
89 UMTEST_EXPECT_NULL(eval_ctx_weak_alias.get())
90 << "The evaluation context was not destroyed! This is likely a bug "
91 "in how the test was written, look for leaking handles to the EC, "
92 "possibly through closure objects.";
93 }
94
Alex Deymo53556ec2014-03-17 10:05:57 -070095 // Check that the evaluation context removed all the observers.
96 EXPECT_TRUE(fake_int_var_.observer_list_.empty());
97 EXPECT_TRUE(fake_async_var_.observer_list_.empty());
98 EXPECT_TRUE(fake_const_var_.observer_list_.empty());
99 EXPECT_TRUE(fake_poll_var_.observer_list_.empty());
100 }
101
Alex Deymo41a75a72014-04-15 15:36:22 -0700102 base::TimeDelta default_timeout_ = base::TimeDelta::FromSeconds(5);
103
104 FakeClock fake_clock_;
Alex Deymo7b948f02014-03-10 17:01:10 -0700105 scoped_refptr<EvaluationContext> eval_ctx_;
Alex Deymo53556ec2014-03-17 10:05:57 -0700106
107 // FakeVariables used for testing the EvaluationContext. These are required
108 // here to prevent them from going away *before* the EvaluationContext under
109 // test does, which keeps a reference to them.
David Zeuthenc1490282014-04-29 16:25:03 -0700110 FakeVariable<bool> fail_var_ = {"fail_var", kVariableModePoll};
Alex Deymo53556ec2014-03-17 10:05:57 -0700111 FakeVariable<int> fake_int_var_ = {"fake_int", kVariableModePoll};
112 FakeVariable<string> fake_async_var_ = {"fake_async", kVariableModeAsync};
113 FakeVariable<string> fake_const_var_ = {"fake_const", kVariableModeConst};
114 FakeVariable<string> fake_poll_var_ = {"fake_poll",
115 TimeDelta::FromSeconds(1)};
Gilad Arnoldfb794f42014-07-01 15:36:31 -0700116 StrictMock<MockVariable<string>> mock_var_async_ {
117 "mock_var_async", kVariableModeAsync};
118 StrictMock<MockVariable<string>> mock_var_poll_ {
119 "mock_var_poll", kVariableModePoll};
Alex Deymo23949d42014-02-05 15:20:59 -0800120};
121
Alex Deymo63784a52014-05-28 10:46:14 -0700122TEST_F(UmEvaluationContextTest, GetValueFails) {
Alex Deymo23949d42014-02-05 15:20:59 -0800123 // FakeVariable is initialized as returning NULL.
Alex Deymo63784a52014-05-28 10:46:14 -0700124 UMTEST_EXPECT_NULL(eval_ctx_->GetValue(&fake_int_var_));
Alex Deymo23949d42014-02-05 15:20:59 -0800125}
126
Alex Deymo63784a52014-05-28 10:46:14 -0700127TEST_F(UmEvaluationContextTest, GetValueFailsWithInvalidVar) {
128 UMTEST_EXPECT_NULL(eval_ctx_->GetValue(
Alex Deymo23949d42014-02-05 15:20:59 -0800129 reinterpret_cast<Variable<int>*>(NULL)));
130}
131
Alex Deymo63784a52014-05-28 10:46:14 -0700132TEST_F(UmEvaluationContextTest, GetValueReturns) {
Alex Deymo23949d42014-02-05 15:20:59 -0800133 const int* p_fake_int;
134
135 fake_int_var_.reset(new int(42));
136 p_fake_int = eval_ctx_->GetValue(&fake_int_var_);
Alex Deymo63784a52014-05-28 10:46:14 -0700137 UMTEST_ASSERT_NOT_NULL(p_fake_int);
Alex Deymo23949d42014-02-05 15:20:59 -0800138 EXPECT_EQ(42, *p_fake_int);
139}
140
Alex Deymo63784a52014-05-28 10:46:14 -0700141TEST_F(UmEvaluationContextTest, GetValueCached) {
Alex Deymo23949d42014-02-05 15:20:59 -0800142 const int* p_fake_int;
143
144 fake_int_var_.reset(new int(42));
145 p_fake_int = eval_ctx_->GetValue(&fake_int_var_);
146
147 // Check that if the variable changes, the EvaluationContext keeps returning
148 // the cached value.
149 fake_int_var_.reset(new int(5));
150
151 p_fake_int = eval_ctx_->GetValue(&fake_int_var_);
Alex Deymo63784a52014-05-28 10:46:14 -0700152 UMTEST_ASSERT_NOT_NULL(p_fake_int);
Alex Deymo23949d42014-02-05 15:20:59 -0800153 EXPECT_EQ(42, *p_fake_int);
154}
155
Alex Deymo63784a52014-05-28 10:46:14 -0700156TEST_F(UmEvaluationContextTest, GetValueCachesNull) {
Alex Deymo23949d42014-02-05 15:20:59 -0800157 const int* p_fake_int = eval_ctx_->GetValue(&fake_int_var_);
Alex Deymo63784a52014-05-28 10:46:14 -0700158 UMTEST_EXPECT_NULL(p_fake_int);
Alex Deymo23949d42014-02-05 15:20:59 -0800159
160 fake_int_var_.reset(new int(42));
Alex Deymocc0e5cf2014-04-23 20:20:11 -0700161 // A second attempt to read the variable should not work because this
162 // EvaluationContext already got a NULL value.
Alex Deymo23949d42014-02-05 15:20:59 -0800163 p_fake_int = eval_ctx_->GetValue(&fake_int_var_);
Alex Deymo63784a52014-05-28 10:46:14 -0700164 UMTEST_EXPECT_NULL(p_fake_int);
Alex Deymo23949d42014-02-05 15:20:59 -0800165}
166
Alex Deymo63784a52014-05-28 10:46:14 -0700167TEST_F(UmEvaluationContextTest, GetValueMixedTypes) {
Alex Deymo23949d42014-02-05 15:20:59 -0800168 const int* p_fake_int;
169 const string* p_fake_string;
170
171 fake_int_var_.reset(new int(42));
Alex Deymo53556ec2014-03-17 10:05:57 -0700172 fake_poll_var_.reset(new string("Hello world!"));
Alex Deymo23949d42014-02-05 15:20:59 -0800173 // Check that the EvaluationContext can handle multiple Variable types. This
174 // is mostly a compile-time check due to the template nature of this method.
175 p_fake_int = eval_ctx_->GetValue(&fake_int_var_);
Alex Deymo53556ec2014-03-17 10:05:57 -0700176 p_fake_string = eval_ctx_->GetValue(&fake_poll_var_);
Alex Deymo23949d42014-02-05 15:20:59 -0800177
Alex Deymo63784a52014-05-28 10:46:14 -0700178 UMTEST_ASSERT_NOT_NULL(p_fake_int);
Alex Deymo23949d42014-02-05 15:20:59 -0800179 EXPECT_EQ(42, *p_fake_int);
180
Alex Deymo63784a52014-05-28 10:46:14 -0700181 UMTEST_ASSERT_NOT_NULL(p_fake_string);
Alex Deymo23949d42014-02-05 15:20:59 -0800182 EXPECT_EQ("Hello world!", *p_fake_string);
183}
184
Alex Deymo53556ec2014-03-17 10:05:57 -0700185// Test that we don't schedule an event if there's no variable to wait for.
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700186TEST_F(UmEvaluationContextTest, RunOnValueChangeOrTimeoutWithoutVariables) {
Alex Deymo53556ec2014-03-17 10:05:57 -0700187 fake_const_var_.reset(new string("Hello world!"));
188 EXPECT_EQ(*eval_ctx_->GetValue(&fake_const_var_), "Hello world!");
189
190 EXPECT_FALSE(eval_ctx_->RunOnValueChangeOrTimeout(Bind(&DoNothing)));
191}
192
Gilad Arnoldfb794f42014-07-01 15:36:31 -0700193// Test that reevaluation occurs when an async variable it depends on changes.
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700194TEST_F(UmEvaluationContextTest, RunOnValueChangeOrTimeoutWithVariables) {
Alex Deymo53556ec2014-03-17 10:05:57 -0700195 fake_async_var_.reset(new string("Async value"));
196 eval_ctx_->GetValue(&fake_async_var_);
197
198 bool value = false;
199 EXPECT_TRUE(eval_ctx_->RunOnValueChangeOrTimeout(Bind(&SetTrue, &value)));
200 // Check that the scheduled callback isn't run until we signal a ValueChaged.
201 RunGMainLoopMaxIterations(100);
202 EXPECT_FALSE(value);
203
204 fake_async_var_.NotifyValueChanged();
205 EXPECT_FALSE(value);
206 // Ensure that the scheduled callback isn't run until we are back on the main
207 // loop.
208 RunGMainLoopMaxIterations(100);
209 EXPECT_TRUE(value);
210}
211
212// Test that we don't re-schedule the events if we are attending one.
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700213TEST_F(UmEvaluationContextTest, RunOnValueChangeOrTimeoutCalledTwice) {
Alex Deymo53556ec2014-03-17 10:05:57 -0700214 fake_async_var_.reset(new string("Async value"));
215 eval_ctx_->GetValue(&fake_async_var_);
216
217 bool value = false;
218 EXPECT_TRUE(eval_ctx_->RunOnValueChangeOrTimeout(Bind(&SetTrue, &value)));
219 EXPECT_FALSE(eval_ctx_->RunOnValueChangeOrTimeout(Bind(&SetTrue, &value)));
220
221 // The scheduled event should still work.
222 fake_async_var_.NotifyValueChanged();
223 RunGMainLoopMaxIterations(100);
224 EXPECT_TRUE(value);
225}
226
Gilad Arnoldfb794f42014-07-01 15:36:31 -0700227// Test that reevaluation occurs when a polling timeout fires.
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700228TEST_F(UmEvaluationContextTest, RunOnValueChangeOrTimeoutRunsFromTimeout) {
Alex Deymo53556ec2014-03-17 10:05:57 -0700229 fake_poll_var_.reset(new string("Polled value"));
230 eval_ctx_->GetValue(&fake_poll_var_);
231
232 bool value = false;
233 EXPECT_TRUE(eval_ctx_->RunOnValueChangeOrTimeout(Bind(&SetTrue, &value)));
234 // Check that the scheduled callback isn't run until the timeout occurs.
235 RunGMainLoopMaxIterations(10);
236 EXPECT_FALSE(value);
237 RunGMainLoopUntil(10000, Bind(&GetBoolean, &value));
238 EXPECT_TRUE(value);
239}
240
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700241// Test that callback is called when evaluation context expires, and that it
Gilad Arnoldfd45a732014-08-07 15:53:46 -0700242// cannot be used again unless the expiration deadline is reset.
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700243TEST_F(UmEvaluationContextTest, RunOnValueChangeOrTimeoutExpires) {
244 fake_async_var_.reset(new string("Async value"));
245 eval_ctx_->GetValue(&fake_async_var_);
246
247 bool value = false;
248 EXPECT_TRUE(eval_ctx_->RunOnValueChangeOrTimeout(Bind(&SetTrue, &value)));
249 // Check that the scheduled callback isn't run until the timeout occurs.
250 RunGMainLoopMaxIterations(10);
251 EXPECT_FALSE(value);
252 RunGMainLoopUntil(10000, Bind(&GetBoolean, &value));
253 EXPECT_TRUE(value);
254
255 // Ensure that we cannot reschedule an evaluation.
256 EXPECT_FALSE(eval_ctx_->RunOnValueChangeOrTimeout(Bind(&DoNothing)));
Gilad Arnoldfd45a732014-08-07 15:53:46 -0700257
258 // Ensure that we can reschedule an evaluation after resetting expiration.
259 eval_ctx_->ResetExpiration();
260 EXPECT_TRUE(eval_ctx_->RunOnValueChangeOrTimeout(Bind(&DoNothing)));
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700261}
262
263// Test that we clear the events when destroying the EvaluationContext.
264TEST_F(UmEvaluationContextTest, RemoveObserversAndTimeoutTest) {
265 fake_async_var_.reset(new string("Async value"));
266 eval_ctx_->GetValue(&fake_async_var_);
267
268 bool value = false;
269 EXPECT_TRUE(eval_ctx_->RunOnValueChangeOrTimeout(Bind(&SetTrue, &value)));
270 eval_ctx_ = nullptr;
271
272 // This should not trigger the callback since the EvaluationContext waiting
273 // for it is gone, and it should have remove all its observers.
274 fake_async_var_.NotifyValueChanged();
275 RunGMainLoopMaxIterations(100);
276 EXPECT_FALSE(value);
277}
278
Gilad Arnoldfb794f42014-07-01 15:36:31 -0700279// Scheduling two reevaluations from the callback should succeed.
280TEST_F(UmEvaluationContextTest,
281 RunOnValueChangeOrTimeoutReevaluatesRepeatedly) {
282 fake_poll_var_.reset(new string("Polled value"));
283 Closure evaluation = Bind(ReadVar<string>, eval_ctx_, &fake_poll_var_);
284 int num_reevaluations = 2;
285 bool done = false;
286
287 // Run the evaluation once.
288 evaluation.Run();
289
290 // Schedule repeated reevaluations.
291 Closure closure = Bind(EvaluateRepeatedly, evaluation, eval_ctx_,
292 &num_reevaluations, &done);
293 ASSERT_TRUE(eval_ctx_->RunOnValueChangeOrTimeout(closure));
294 RunGMainLoopUntil(10000, Bind(&GetBoolean, &done));
295 EXPECT_EQ(0, num_reevaluations);
296}
297
Alex Deymodb799532014-03-21 13:00:00 -0700298// Test that we can delete the EvaluationContext while having pending events.
Alex Deymo63784a52014-05-28 10:46:14 -0700299TEST_F(UmEvaluationContextTest, ObjectDeletedWithPendingEventsTest) {
Alex Deymodb799532014-03-21 13:00:00 -0700300 fake_async_var_.reset(new string("Async value"));
301 fake_poll_var_.reset(new string("Polled value"));
302 eval_ctx_->GetValue(&fake_async_var_);
303 eval_ctx_->GetValue(&fake_poll_var_);
304 EXPECT_TRUE(eval_ctx_->RunOnValueChangeOrTimeout(Bind(&DoNothing)));
305 // TearDown() checks for leaked observers on this async_variable, which means
306 // that our object is still alive after removing its reference.
307}
308
309// Test that timed events fired after removal of the EvaluationContext don't
310// crash.
Alex Deymo63784a52014-05-28 10:46:14 -0700311TEST_F(UmEvaluationContextTest, TimeoutEventAfterDeleteTest) {
Alex Deymodb799532014-03-21 13:00:00 -0700312 FakeVariable<string> fake_short_poll_var = {"fake_short_poll", TimeDelta()};
313 fake_short_poll_var.reset(new string("Polled value"));
314 eval_ctx_->GetValue(&fake_short_poll_var);
315 bool value = false;
316 EXPECT_TRUE(eval_ctx_->RunOnValueChangeOrTimeout(Bind(&SetTrue, &value)));
317 // Remove the last reference to the EvaluationContext and run the loop for
318 // 1 second to give time to the main loop to trigger the timeout Event (of 0
319 // seconds). Our callback should not be called because the EvaluationContext
320 // was removed before the timeout event is attended.
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700321 eval_ctx_ = nullptr;
Alex Deymodb799532014-03-21 13:00:00 -0700322 RunGMainLoopUntil(1000, Bind(&GetBoolean, &value));
323 EXPECT_FALSE(value);
324}
325
Alex Deymo63784a52014-05-28 10:46:14 -0700326TEST_F(UmEvaluationContextTest, DefaultTimeout) {
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700327 // Test that the evaluation timeout calculation uses the default timeout on
328 // setup.
Alex Deymo41a75a72014-04-15 15:36:22 -0700329 EXPECT_CALL(mock_var_async_, GetValue(default_timeout_, _))
330 .WillOnce(Return(nullptr));
Alex Deymo63784a52014-05-28 10:46:14 -0700331 UMTEST_EXPECT_NULL(eval_ctx_->GetValue(&mock_var_async_));
Alex Deymo41a75a72014-04-15 15:36:22 -0700332}
333
Alex Deymo63784a52014-05-28 10:46:14 -0700334TEST_F(UmEvaluationContextTest, TimeoutUpdatesWithMonotonicTime) {
Alex Deymo41a75a72014-04-15 15:36:22 -0700335 fake_clock_.SetMonotonicTime(
336 fake_clock_.GetMonotonicTime() + TimeDelta::FromSeconds(1));
337
338 TimeDelta timeout = default_timeout_ - TimeDelta::FromSeconds(1);
339
340 EXPECT_CALL(mock_var_async_, GetValue(timeout, _))
341 .WillOnce(Return(nullptr));
Alex Deymo63784a52014-05-28 10:46:14 -0700342 UMTEST_EXPECT_NULL(eval_ctx_->GetValue(&mock_var_async_));
Alex Deymo41a75a72014-04-15 15:36:22 -0700343}
344
Gilad Arnolda65fced2014-07-23 09:01:31 -0700345TEST_F(UmEvaluationContextTest, ResetEvaluationResetsTimesWallclock) {
Alex Deymo41a75a72014-04-15 15:36:22 -0700346 base::Time cur_time = fake_clock_.GetWallclockTime();
347 // Advance the time on the clock but don't call ResetEvaluation yet.
348 fake_clock_.SetWallclockTime(cur_time + TimeDelta::FromSeconds(4));
349
Gilad Arnolda65fced2014-07-23 09:01:31 -0700350 EXPECT_TRUE(eval_ctx_->IsWallclockTimeGreaterThan(
351 cur_time - TimeDelta::FromSeconds(1)));
352 EXPECT_FALSE(eval_ctx_->IsWallclockTimeGreaterThan(cur_time));
353 EXPECT_FALSE(eval_ctx_->IsWallclockTimeGreaterThan(
354 cur_time + TimeDelta::FromSeconds(1)));
Alex Deymo41a75a72014-04-15 15:36:22 -0700355 // Call ResetEvaluation now, which should use the new evaluation time.
356 eval_ctx_->ResetEvaluation();
357
358 cur_time = fake_clock_.GetWallclockTime();
Gilad Arnolda65fced2014-07-23 09:01:31 -0700359 EXPECT_TRUE(eval_ctx_->IsWallclockTimeGreaterThan(
360 cur_time - TimeDelta::FromSeconds(1)));
361 EXPECT_FALSE(eval_ctx_->IsWallclockTimeGreaterThan(cur_time));
362 EXPECT_FALSE(eval_ctx_->IsWallclockTimeGreaterThan(
363 cur_time + TimeDelta::FromSeconds(1)));
Alex Deymo41a75a72014-04-15 15:36:22 -0700364}
365
Gilad Arnolda65fced2014-07-23 09:01:31 -0700366TEST_F(UmEvaluationContextTest, ResetEvaluationResetsTimesMonotonic) {
367 base::Time cur_time = fake_clock_.GetMonotonicTime();
368 // Advance the time on the clock but don't call ResetEvaluation yet.
369 fake_clock_.SetMonotonicTime(cur_time + TimeDelta::FromSeconds(4));
370
371 EXPECT_TRUE(eval_ctx_->IsMonotonicTimeGreaterThan(
372 cur_time - TimeDelta::FromSeconds(1)));
373 EXPECT_FALSE(eval_ctx_->IsMonotonicTimeGreaterThan(cur_time));
374 EXPECT_FALSE(eval_ctx_->IsMonotonicTimeGreaterThan(
375 cur_time + TimeDelta::FromSeconds(1)));
376 // Call ResetEvaluation now, which should use the new evaluation time.
377 eval_ctx_->ResetEvaluation();
378
379 cur_time = fake_clock_.GetMonotonicTime();
380 EXPECT_TRUE(eval_ctx_->IsMonotonicTimeGreaterThan(
381 cur_time - TimeDelta::FromSeconds(1)));
382 EXPECT_FALSE(eval_ctx_->IsMonotonicTimeGreaterThan(cur_time));
383 EXPECT_FALSE(eval_ctx_->IsMonotonicTimeGreaterThan(
384 cur_time + TimeDelta::FromSeconds(1)));
385}
386
387TEST_F(UmEvaluationContextTest,
388 IsWallclockTimeGreaterThanSignalsTriggerReevaluation) {
389 EXPECT_FALSE(eval_ctx_->IsWallclockTimeGreaterThan(
Alex Deymo41a75a72014-04-15 15:36:22 -0700390 fake_clock_.GetWallclockTime() + TimeDelta::FromSeconds(1)));
391
Gilad Arnolda65fced2014-07-23 09:01:31 -0700392 // The "false" from IsWallclockTimeGreaterThan means that's not that timestamp
393 // yet, so this should schedule a callback for when that happens.
Alex Deymo41a75a72014-04-15 15:36:22 -0700394 EXPECT_TRUE(eval_ctx_->RunOnValueChangeOrTimeout(Bind(&DoNothing)));
395}
396
Gilad Arnolda65fced2014-07-23 09:01:31 -0700397TEST_F(UmEvaluationContextTest,
398 IsMonotonicTimeGreaterThanSignalsTriggerReevaluation) {
399 EXPECT_FALSE(eval_ctx_->IsMonotonicTimeGreaterThan(
400 fake_clock_.GetMonotonicTime() + TimeDelta::FromSeconds(1)));
401
402 // The "false" from IsMonotonicTimeGreaterThan means that's not that timestamp
403 // yet, so this should schedule a callback for when that happens.
404 EXPECT_TRUE(eval_ctx_->RunOnValueChangeOrTimeout(Bind(&DoNothing)));
405}
406
407TEST_F(UmEvaluationContextTest,
408 IsWallclockTimeGreaterThanDoesntRecordPastTimestamps) {
409 // IsWallclockTimeGreaterThan() should ignore timestamps on the past for
410 // reevaluation.
411 EXPECT_TRUE(eval_ctx_->IsWallclockTimeGreaterThan(
Alex Deymo41a75a72014-04-15 15:36:22 -0700412 fake_clock_.GetWallclockTime() - TimeDelta::FromSeconds(20)));
Gilad Arnolda65fced2014-07-23 09:01:31 -0700413 EXPECT_TRUE(eval_ctx_->IsWallclockTimeGreaterThan(
Alex Deymo41a75a72014-04-15 15:36:22 -0700414 fake_clock_.GetWallclockTime() - TimeDelta::FromSeconds(1)));
415
416 // Callback should not be scheduled.
417 EXPECT_FALSE(eval_ctx_->RunOnValueChangeOrTimeout(Bind(&DoNothing)));
418}
419
Gilad Arnolda65fced2014-07-23 09:01:31 -0700420TEST_F(UmEvaluationContextTest,
421 IsMonotonicTimeGreaterThanDoesntRecordPastTimestamps) {
422 // IsMonotonicTimeGreaterThan() should ignore timestamps on the past for
423 // reevaluation.
424 EXPECT_TRUE(eval_ctx_->IsMonotonicTimeGreaterThan(
425 fake_clock_.GetMonotonicTime() - TimeDelta::FromSeconds(20)));
426 EXPECT_TRUE(eval_ctx_->IsMonotonicTimeGreaterThan(
427 fake_clock_.GetMonotonicTime() - TimeDelta::FromSeconds(1)));
428
429 // Callback should not be scheduled.
430 EXPECT_FALSE(eval_ctx_->RunOnValueChangeOrTimeout(Bind(&DoNothing)));
431}
432
Alex Deymo63784a52014-05-28 10:46:14 -0700433TEST_F(UmEvaluationContextTest, DumpContext) {
David Zeuthenc1490282014-04-29 16:25:03 -0700434 // |fail_var_| yield "(no value)" since it is unset.
435 eval_ctx_->GetValue(&fail_var_);
436
437 // Check that this is included.
438 fake_int_var_.reset(new int(42));
439 eval_ctx_->GetValue(&fake_int_var_);
440
441 // Check that double-quotes are escaped properly.
442 fake_poll_var_.reset(new string("Hello \"world\"!"));
443 eval_ctx_->GetValue(&fake_poll_var_);
444
445 // Note that the variables are printed in alphabetical order. Also
Gilad Arnolda65fced2014-07-23 09:01:31 -0700446 // see UmEvaluationContextText::SetUp() where the values used for
447 // |evaluation_start_{monotonic,wallclock| are set.
David Zeuthenc1490282014-04-29 16:25:03 -0700448 EXPECT_EQ("{\n"
Gilad Arnolda65fced2014-07-23 09:01:31 -0700449 " \"evaluation_start_monotonic\": \"4/22/2009 19:25:00 GMT\",\n"
450 " \"evaluation_start_wallclock\": \"3/2/2006 1:23:45 GMT\",\n"
David Zeuthenc1490282014-04-29 16:25:03 -0700451 " \"variables\": {\n"
452 " \"fail_var\": \"(no value)\",\n"
453 " \"fake_int\": \"42\",\n"
454 " \"fake_poll\": \"Hello \\\"world\\\"!\"\n"
455 " }\n"
Gilad Arnold6e5ab5c2014-06-23 15:13:56 -0700456 "}",
David Zeuthenc1490282014-04-29 16:25:03 -0700457 eval_ctx_->DumpContext());
458}
459
Alex Deymo63784a52014-05-28 10:46:14 -0700460} // namespace chromeos_update_manager