blob: cd0b2e6c8569a17f20d06b57271cbb5e25a6d50e [file] [log] [blame]
Alex Deymoaea4c1c2015-08-19 20:24:43 -07001//
2// Copyright (C) 2014 The Android Open Source Project
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15//
Alex Deymo23949d42014-02-05 15:20:59 -080016
Alex Deymoaab50e32014-11-10 19:55:35 -080017#include "update_engine/update_manager/evaluation_context.h"
18
Ben Chan02f7c1d2014-10-18 15:18:02 -070019#include <memory>
Alex Deymo23949d42014-02-05 15:20:59 -080020#include <string>
21
Alex Deymo53556ec2014-03-17 10:05:57 -070022#include <base/bind.h>
Jakub Pawlowski7e1dcf72018-07-26 00:29:42 -070023#include <base/bind_helpers.h>
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070024#include <brillo/message_loops/fake_message_loop.h>
25#include <brillo/message_loops/message_loop_utils.h>
Alex Deymo53556ec2014-03-17 10:05:57 -070026#include <gtest/gtest.h>
27
Alex Deymo39910dc2015-11-09 17:04:30 -080028#include "update_engine/common/fake_clock.h"
Alex Deymo63784a52014-05-28 10:46:14 -070029#include "update_engine/update_manager/fake_variable.h"
30#include "update_engine/update_manager/generic_variables.h"
31#include "update_engine/update_manager/mock_variable.h"
32#include "update_engine/update_manager/umtest_utils.h"
Alex Deymo23949d42014-02-05 15:20:59 -080033
Alex Deymo53556ec2014-03-17 10:05:57 -070034using base::Bind;
Gilad Arnoldfb794f42014-07-01 15:36:31 -070035using base::Closure;
Alex Deymo41a75a72014-04-15 15:36:22 -070036using base::Time;
Alex Deymo53556ec2014-03-17 10:05:57 -070037using base::TimeDelta;
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070038using brillo::MessageLoop;
39using brillo::MessageLoopRunMaxIterations;
40using brillo::MessageLoopRunUntil;
Alex Deymo41a75a72014-04-15 15:36:22 -070041using chromeos_update_engine::FakeClock;
Amin Hassania2c8b922019-08-14 19:41:03 -070042using std::shared_ptr;
Alex Deymo23949d42014-02-05 15:20:59 -080043using std::string;
Ben Chan02f7c1d2014-10-18 15:18:02 -070044using std::unique_ptr;
Amin Hassani4b717432019-01-14 16:24:20 -080045using testing::_;
Alex Deymo41a75a72014-04-15 15:36:22 -070046using testing::Return;
47using testing::StrictMock;
Alex Deymo23949d42014-02-05 15:20:59 -080048
Gilad Arnoldfb794f42014-07-01 15:36:31 -070049namespace chromeos_update_manager {
50
Alex Deymo53556ec2014-03-17 10:05:57 -070051namespace {
52
Alex Deymo53556ec2014-03-17 10:05:57 -070053// Sets the value of the passed pointer to true.
54void SetTrue(bool* value) {
55 *value = true;
56}
57
58bool GetBoolean(bool* value) {
59 return *value;
60}
61
Amin Hassani4b717432019-01-14 16:24:20 -080062template <typename T>
Amin Hassania2c8b922019-08-14 19:41:03 -070063void ReadVar(shared_ptr<EvaluationContext> ec, Variable<T>* var) {
Gilad Arnoldfb794f42014-07-01 15:36:31 -070064 ec->GetValue(var);
65}
Alex Deymo53556ec2014-03-17 10:05:57 -070066
Gilad Arnoldfb794f42014-07-01 15:36:31 -070067// Runs |evaluation|; if the value pointed by |count_p| is greater than zero,
68// decrement it and schedule a reevaluation; otherwise, writes true to |done_p|.
Amin Hassani4b717432019-01-14 16:24:20 -080069void EvaluateRepeatedly(Closure evaluation,
Amin Hassania2c8b922019-08-14 19:41:03 -070070 shared_ptr<EvaluationContext> ec,
Amin Hassani4b717432019-01-14 16:24:20 -080071 int* count_p,
72 bool* done_p) {
Gilad Arnoldfb794f42014-07-01 15:36:31 -070073 evaluation.Run();
74
75 // Schedule reevaluation if needed.
76 if (*count_p > 0) {
77 Closure closure = Bind(EvaluateRepeatedly, evaluation, ec, count_p, done_p);
78 ASSERT_TRUE(ec->RunOnValueChangeOrTimeout(closure))
79 << "Failed to schedule reevaluation, count_p=" << *count_p;
80 (*count_p)--;
81 } else {
82 *done_p = true;
83 }
84}
85
86} // namespace
Alex Deymo23949d42014-02-05 15:20:59 -080087
Alex Deymo63784a52014-05-28 10:46:14 -070088class UmEvaluationContextTest : public ::testing::Test {
Alex Deymo23949d42014-02-05 15:20:59 -080089 protected:
Alex Deymo610277e2014-11-11 21:18:11 -080090 void SetUp() override {
Alex Deymo509dd532015-06-10 14:11:05 -070091 loop_.SetAsCurrent();
Gilad Arnolda65fced2014-07-23 09:01:31 -070092 // Apr 22, 2009 19:25:00 UTC (this is a random reference point).
93 fake_clock_.SetMonotonicTime(Time::FromTimeT(1240428300));
94 // Mar 2, 2006 1:23:45 UTC.
David Zeuthenc1490282014-04-29 16:25:03 -070095 fake_clock_.SetWallclockTime(Time::FromTimeT(1141262625));
Amin Hassania2c8b922019-08-14 19:41:03 -070096 eval_ctx_.reset(new EvaluationContext(
Amin Hassani4b717432019-01-14 16:24:20 -080097 &fake_clock_,
98 default_timeout_,
99 default_timeout_,
Amin Hassania2c8b922019-08-14 19:41:03 -0700100 unique_ptr<base::Callback<void(EvaluationContext*)>>(nullptr)));
Alex Deymo23949d42014-02-05 15:20:59 -0800101 }
102
Alex Deymo610277e2014-11-11 21:18:11 -0800103 void TearDown() override {
Gilad Arnoldfb794f42014-07-01 15:36:31 -0700104 // Ensure that the evaluation context did not leak and is actually being
105 // destroyed.
106 if (eval_ctx_) {
107 base::WeakPtr<EvaluationContext> eval_ctx_weak_alias =
108 eval_ctx_->weak_ptr_factory_.GetWeakPtr();
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700109 ASSERT_NE(nullptr, eval_ctx_weak_alias.get());
Gilad Arnoldfb794f42014-07-01 15:36:31 -0700110 eval_ctx_ = nullptr;
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700111 EXPECT_EQ(nullptr, eval_ctx_weak_alias.get())
Gilad Arnoldfb794f42014-07-01 15:36:31 -0700112 << "The evaluation context was not destroyed! This is likely a bug "
113 "in how the test was written, look for leaking handles to the EC, "
114 "possibly through closure objects.";
115 }
116
Alex Deymo53556ec2014-03-17 10:05:57 -0700117 // Check that the evaluation context removed all the observers.
118 EXPECT_TRUE(fake_int_var_.observer_list_.empty());
119 EXPECT_TRUE(fake_async_var_.observer_list_.empty());
120 EXPECT_TRUE(fake_const_var_.observer_list_.empty());
121 EXPECT_TRUE(fake_poll_var_.observer_list_.empty());
Alex Deymo509dd532015-06-10 14:11:05 -0700122
123 EXPECT_FALSE(loop_.PendingTasks());
Alex Deymo53556ec2014-03-17 10:05:57 -0700124 }
125
Alex Deymof329b932014-10-30 01:37:48 -0700126 TimeDelta default_timeout_ = TimeDelta::FromSeconds(5);
Alex Deymo41a75a72014-04-15 15:36:22 -0700127
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700128 brillo::FakeMessageLoop loop_{nullptr};
Alex Deymo41a75a72014-04-15 15:36:22 -0700129 FakeClock fake_clock_;
Amin Hassania2c8b922019-08-14 19:41:03 -0700130 shared_ptr<EvaluationContext> eval_ctx_;
Alex Deymo53556ec2014-03-17 10:05:57 -0700131
132 // FakeVariables used for testing the EvaluationContext. These are required
133 // here to prevent them from going away *before* the EvaluationContext under
134 // test does, which keeps a reference to them.
David Zeuthenc1490282014-04-29 16:25:03 -0700135 FakeVariable<bool> fail_var_ = {"fail_var", kVariableModePoll};
Alex Deymo53556ec2014-03-17 10:05:57 -0700136 FakeVariable<int> fake_int_var_ = {"fake_int", kVariableModePoll};
137 FakeVariable<string> fake_async_var_ = {"fake_async", kVariableModeAsync};
138 FakeVariable<string> fake_const_var_ = {"fake_const", kVariableModeConst};
139 FakeVariable<string> fake_poll_var_ = {"fake_poll",
140 TimeDelta::FromSeconds(1)};
Amin Hassani4b717432019-01-14 16:24:20 -0800141 StrictMock<MockVariable<string>> mock_var_async_{"mock_var_async",
142 kVariableModeAsync};
143 StrictMock<MockVariable<string>> mock_var_poll_{"mock_var_poll",
144 kVariableModePoll};
Alex Deymo23949d42014-02-05 15:20:59 -0800145};
146
Alex Deymo63784a52014-05-28 10:46:14 -0700147TEST_F(UmEvaluationContextTest, GetValueFails) {
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700148 // FakeVariable is initialized as returning null.
149 EXPECT_EQ(nullptr, eval_ctx_->GetValue(&fake_int_var_));
Alex Deymo23949d42014-02-05 15:20:59 -0800150}
151
Alex Deymo63784a52014-05-28 10:46:14 -0700152TEST_F(UmEvaluationContextTest, GetValueFailsWithInvalidVar) {
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700153 EXPECT_EQ(nullptr, eval_ctx_->GetValue(static_cast<Variable<int>*>(nullptr)));
Alex Deymo23949d42014-02-05 15:20:59 -0800154}
155
Alex Deymo63784a52014-05-28 10:46:14 -0700156TEST_F(UmEvaluationContextTest, GetValueReturns) {
Alex Deymo23949d42014-02-05 15:20:59 -0800157 const int* p_fake_int;
158
159 fake_int_var_.reset(new int(42));
160 p_fake_int = eval_ctx_->GetValue(&fake_int_var_);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700161 ASSERT_NE(nullptr, p_fake_int);
Alex Deymo23949d42014-02-05 15:20:59 -0800162 EXPECT_EQ(42, *p_fake_int);
163}
164
Alex Deymo63784a52014-05-28 10:46:14 -0700165TEST_F(UmEvaluationContextTest, GetValueCached) {
Alex Deymo23949d42014-02-05 15:20:59 -0800166 const int* p_fake_int;
167
168 fake_int_var_.reset(new int(42));
169 p_fake_int = eval_ctx_->GetValue(&fake_int_var_);
170
171 // Check that if the variable changes, the EvaluationContext keeps returning
172 // the cached value.
173 fake_int_var_.reset(new int(5));
174
175 p_fake_int = eval_ctx_->GetValue(&fake_int_var_);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700176 ASSERT_NE(nullptr, p_fake_int);
Alex Deymo23949d42014-02-05 15:20:59 -0800177 EXPECT_EQ(42, *p_fake_int);
178}
179
Alex Deymo63784a52014-05-28 10:46:14 -0700180TEST_F(UmEvaluationContextTest, GetValueCachesNull) {
Alex Deymo23949d42014-02-05 15:20:59 -0800181 const int* p_fake_int = eval_ctx_->GetValue(&fake_int_var_);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700182 EXPECT_EQ(nullptr, p_fake_int);
Alex Deymo23949d42014-02-05 15:20:59 -0800183
184 fake_int_var_.reset(new int(42));
Alex Deymocc0e5cf2014-04-23 20:20:11 -0700185 // A second attempt to read the variable should not work because this
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700186 // EvaluationContext already got a null value.
Alex Deymo23949d42014-02-05 15:20:59 -0800187 p_fake_int = eval_ctx_->GetValue(&fake_int_var_);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700188 EXPECT_EQ(nullptr, p_fake_int);
Alex Deymo23949d42014-02-05 15:20:59 -0800189}
190
Alex Deymo63784a52014-05-28 10:46:14 -0700191TEST_F(UmEvaluationContextTest, GetValueMixedTypes) {
Alex Deymo23949d42014-02-05 15:20:59 -0800192 const int* p_fake_int;
193 const string* p_fake_string;
194
195 fake_int_var_.reset(new int(42));
Alex Deymo53556ec2014-03-17 10:05:57 -0700196 fake_poll_var_.reset(new string("Hello world!"));
Alex Deymo23949d42014-02-05 15:20:59 -0800197 // Check that the EvaluationContext can handle multiple Variable types. This
198 // is mostly a compile-time check due to the template nature of this method.
199 p_fake_int = eval_ctx_->GetValue(&fake_int_var_);
Alex Deymo53556ec2014-03-17 10:05:57 -0700200 p_fake_string = eval_ctx_->GetValue(&fake_poll_var_);
Alex Deymo23949d42014-02-05 15:20:59 -0800201
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700202 ASSERT_NE(nullptr, p_fake_int);
Alex Deymo23949d42014-02-05 15:20:59 -0800203 EXPECT_EQ(42, *p_fake_int);
204
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700205 ASSERT_NE(nullptr, p_fake_string);
Alex Deymo23949d42014-02-05 15:20:59 -0800206 EXPECT_EQ("Hello world!", *p_fake_string);
207}
208
Alex Deymo53556ec2014-03-17 10:05:57 -0700209// Test that we don't schedule an event if there's no variable to wait for.
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700210TEST_F(UmEvaluationContextTest, RunOnValueChangeOrTimeoutWithoutVariables) {
Alex Deymo53556ec2014-03-17 10:05:57 -0700211 fake_const_var_.reset(new string("Hello world!"));
212 EXPECT_EQ(*eval_ctx_->GetValue(&fake_const_var_), "Hello world!");
213
Jae Hoon Kim9d9492f2019-06-17 14:52:48 -0700214 EXPECT_FALSE(eval_ctx_->RunOnValueChangeOrTimeout(base::DoNothing()));
Alex Deymo53556ec2014-03-17 10:05:57 -0700215}
216
Gilad Arnoldfb794f42014-07-01 15:36:31 -0700217// Test that reevaluation occurs when an async variable it depends on changes.
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700218TEST_F(UmEvaluationContextTest, RunOnValueChangeOrTimeoutWithVariables) {
Alex Deymo53556ec2014-03-17 10:05:57 -0700219 fake_async_var_.reset(new string("Async value"));
220 eval_ctx_->GetValue(&fake_async_var_);
221
222 bool value = false;
223 EXPECT_TRUE(eval_ctx_->RunOnValueChangeOrTimeout(Bind(&SetTrue, &value)));
224 // Check that the scheduled callback isn't run until we signal a ValueChaged.
Alex Deymo509dd532015-06-10 14:11:05 -0700225 MessageLoopRunMaxIterations(MessageLoop::current(), 100);
Alex Deymo53556ec2014-03-17 10:05:57 -0700226 EXPECT_FALSE(value);
227
228 fake_async_var_.NotifyValueChanged();
229 EXPECT_FALSE(value);
230 // Ensure that the scheduled callback isn't run until we are back on the main
231 // loop.
Alex Deymo509dd532015-06-10 14:11:05 -0700232 MessageLoopRunMaxIterations(MessageLoop::current(), 100);
Alex Deymo53556ec2014-03-17 10:05:57 -0700233 EXPECT_TRUE(value);
234}
235
236// Test that we don't re-schedule the events if we are attending one.
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700237TEST_F(UmEvaluationContextTest, RunOnValueChangeOrTimeoutCalledTwice) {
Alex Deymo53556ec2014-03-17 10:05:57 -0700238 fake_async_var_.reset(new string("Async value"));
239 eval_ctx_->GetValue(&fake_async_var_);
240
241 bool value = false;
242 EXPECT_TRUE(eval_ctx_->RunOnValueChangeOrTimeout(Bind(&SetTrue, &value)));
243 EXPECT_FALSE(eval_ctx_->RunOnValueChangeOrTimeout(Bind(&SetTrue, &value)));
244
245 // The scheduled event should still work.
246 fake_async_var_.NotifyValueChanged();
Alex Deymo509dd532015-06-10 14:11:05 -0700247 MessageLoopRunMaxIterations(MessageLoop::current(), 100);
Alex Deymo53556ec2014-03-17 10:05:57 -0700248 EXPECT_TRUE(value);
249}
250
Gilad Arnoldfb794f42014-07-01 15:36:31 -0700251// Test that reevaluation occurs when a polling timeout fires.
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700252TEST_F(UmEvaluationContextTest, RunOnValueChangeOrTimeoutRunsFromTimeout) {
Alex Deymo53556ec2014-03-17 10:05:57 -0700253 fake_poll_var_.reset(new string("Polled value"));
254 eval_ctx_->GetValue(&fake_poll_var_);
255
256 bool value = false;
257 EXPECT_TRUE(eval_ctx_->RunOnValueChangeOrTimeout(Bind(&SetTrue, &value)));
258 // Check that the scheduled callback isn't run until the timeout occurs.
Alex Deymo509dd532015-06-10 14:11:05 -0700259 MessageLoopRunMaxIterations(MessageLoop::current(), 10);
Alex Deymo53556ec2014-03-17 10:05:57 -0700260 EXPECT_FALSE(value);
Alex Deymo509dd532015-06-10 14:11:05 -0700261 MessageLoopRunUntil(MessageLoop::current(),
262 TimeDelta::FromSeconds(10),
263 Bind(&GetBoolean, &value));
Alex Deymo53556ec2014-03-17 10:05:57 -0700264 EXPECT_TRUE(value);
265}
266
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700267// Test that callback is called when evaluation context expires, and that it
Gilad Arnoldfd45a732014-08-07 15:53:46 -0700268// cannot be used again unless the expiration deadline is reset.
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700269TEST_F(UmEvaluationContextTest, RunOnValueChangeOrTimeoutExpires) {
270 fake_async_var_.reset(new string("Async value"));
271 eval_ctx_->GetValue(&fake_async_var_);
272
273 bool value = false;
274 EXPECT_TRUE(eval_ctx_->RunOnValueChangeOrTimeout(Bind(&SetTrue, &value)));
275 // Check that the scheduled callback isn't run until the timeout occurs.
Alex Deymo509dd532015-06-10 14:11:05 -0700276 MessageLoopRunMaxIterations(MessageLoop::current(), 10);
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700277 EXPECT_FALSE(value);
Alex Deymo509dd532015-06-10 14:11:05 -0700278 MessageLoopRunUntil(MessageLoop::current(),
279 TimeDelta::FromSeconds(10),
280 Bind(&GetBoolean, &value));
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700281 EXPECT_TRUE(value);
282
283 // Ensure that we cannot reschedule an evaluation.
Jae Hoon Kim9d9492f2019-06-17 14:52:48 -0700284 EXPECT_FALSE(eval_ctx_->RunOnValueChangeOrTimeout(base::DoNothing()));
Gilad Arnoldfd45a732014-08-07 15:53:46 -0700285
286 // Ensure that we can reschedule an evaluation after resetting expiration.
287 eval_ctx_->ResetExpiration();
Jae Hoon Kim9d9492f2019-06-17 14:52:48 -0700288 EXPECT_TRUE(eval_ctx_->RunOnValueChangeOrTimeout(base::DoNothing()));
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700289}
290
291// Test that we clear the events when destroying the EvaluationContext.
292TEST_F(UmEvaluationContextTest, RemoveObserversAndTimeoutTest) {
293 fake_async_var_.reset(new string("Async value"));
294 eval_ctx_->GetValue(&fake_async_var_);
295
296 bool value = false;
297 EXPECT_TRUE(eval_ctx_->RunOnValueChangeOrTimeout(Bind(&SetTrue, &value)));
298 eval_ctx_ = nullptr;
299
300 // This should not trigger the callback since the EvaluationContext waiting
301 // for it is gone, and it should have remove all its observers.
302 fake_async_var_.NotifyValueChanged();
Alex Deymo509dd532015-06-10 14:11:05 -0700303 MessageLoopRunMaxIterations(MessageLoop::current(), 100);
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700304 EXPECT_FALSE(value);
305}
306
Gilad Arnoldfb794f42014-07-01 15:36:31 -0700307// Scheduling two reevaluations from the callback should succeed.
308TEST_F(UmEvaluationContextTest,
309 RunOnValueChangeOrTimeoutReevaluatesRepeatedly) {
310 fake_poll_var_.reset(new string("Polled value"));
311 Closure evaluation = Bind(ReadVar<string>, eval_ctx_, &fake_poll_var_);
312 int num_reevaluations = 2;
313 bool done = false;
314
315 // Run the evaluation once.
316 evaluation.Run();
317
318 // Schedule repeated reevaluations.
Amin Hassani4b717432019-01-14 16:24:20 -0800319 Closure closure = Bind(
320 EvaluateRepeatedly, evaluation, eval_ctx_, &num_reevaluations, &done);
Gilad Arnoldfb794f42014-07-01 15:36:31 -0700321 ASSERT_TRUE(eval_ctx_->RunOnValueChangeOrTimeout(closure));
Alex Deymo509dd532015-06-10 14:11:05 -0700322 MessageLoopRunUntil(MessageLoop::current(),
323 TimeDelta::FromSeconds(10),
324 Bind(&GetBoolean, &done));
Gilad Arnoldfb794f42014-07-01 15:36:31 -0700325 EXPECT_EQ(0, num_reevaluations);
326}
327
Alex Deymodb799532014-03-21 13:00:00 -0700328// Test that we can delete the EvaluationContext while having pending events.
Alex Deymo63784a52014-05-28 10:46:14 -0700329TEST_F(UmEvaluationContextTest, ObjectDeletedWithPendingEventsTest) {
Alex Deymodb799532014-03-21 13:00:00 -0700330 fake_async_var_.reset(new string("Async value"));
331 fake_poll_var_.reset(new string("Polled value"));
332 eval_ctx_->GetValue(&fake_async_var_);
333 eval_ctx_->GetValue(&fake_poll_var_);
Jae Hoon Kim9d9492f2019-06-17 14:52:48 -0700334 EXPECT_TRUE(eval_ctx_->RunOnValueChangeOrTimeout(base::DoNothing()));
Alex Deymodb799532014-03-21 13:00:00 -0700335 // TearDown() checks for leaked observers on this async_variable, which means
336 // that our object is still alive after removing its reference.
337}
338
339// Test that timed events fired after removal of the EvaluationContext don't
340// crash.
Alex Deymo63784a52014-05-28 10:46:14 -0700341TEST_F(UmEvaluationContextTest, TimeoutEventAfterDeleteTest) {
Alex Deymo0bb23412015-06-19 00:04:46 -0700342 FakeVariable<string> fake_short_poll_var = {"fake_short_poll",
343 TimeDelta::FromSeconds(1)};
Alex Deymodb799532014-03-21 13:00:00 -0700344 fake_short_poll_var.reset(new string("Polled value"));
345 eval_ctx_->GetValue(&fake_short_poll_var);
346 bool value = false;
347 EXPECT_TRUE(eval_ctx_->RunOnValueChangeOrTimeout(Bind(&SetTrue, &value)));
348 // Remove the last reference to the EvaluationContext and run the loop for
Alex Deymo0bb23412015-06-19 00:04:46 -0700349 // 10 seconds to give time to the main loop to trigger the timeout Event (of 1
350 // second). Our callback should not be called because the EvaluationContext
Alex Deymodb799532014-03-21 13:00:00 -0700351 // was removed before the timeout event is attended.
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700352 eval_ctx_ = nullptr;
Alex Deymo509dd532015-06-10 14:11:05 -0700353 MessageLoopRunUntil(MessageLoop::current(),
354 TimeDelta::FromSeconds(10),
355 Bind(&GetBoolean, &value));
Alex Deymodb799532014-03-21 13:00:00 -0700356 EXPECT_FALSE(value);
357}
358
Alex Deymo63784a52014-05-28 10:46:14 -0700359TEST_F(UmEvaluationContextTest, DefaultTimeout) {
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700360 // Test that the evaluation timeout calculation uses the default timeout on
361 // setup.
Alex Deymo41a75a72014-04-15 15:36:22 -0700362 EXPECT_CALL(mock_var_async_, GetValue(default_timeout_, _))
363 .WillOnce(Return(nullptr));
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700364 EXPECT_EQ(nullptr, eval_ctx_->GetValue(&mock_var_async_));
Alex Deymo41a75a72014-04-15 15:36:22 -0700365}
366
Alex Deymo63784a52014-05-28 10:46:14 -0700367TEST_F(UmEvaluationContextTest, TimeoutUpdatesWithMonotonicTime) {
Amin Hassani4b717432019-01-14 16:24:20 -0800368 fake_clock_.SetMonotonicTime(fake_clock_.GetMonotonicTime() +
369 TimeDelta::FromSeconds(1));
Alex Deymo41a75a72014-04-15 15:36:22 -0700370
371 TimeDelta timeout = default_timeout_ - TimeDelta::FromSeconds(1);
372
Amin Hassani4b717432019-01-14 16:24:20 -0800373 EXPECT_CALL(mock_var_async_, GetValue(timeout, _)).WillOnce(Return(nullptr));
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700374 EXPECT_EQ(nullptr, eval_ctx_->GetValue(&mock_var_async_));
Alex Deymo41a75a72014-04-15 15:36:22 -0700375}
376
Gilad Arnolda65fced2014-07-23 09:01:31 -0700377TEST_F(UmEvaluationContextTest, ResetEvaluationResetsTimesWallclock) {
Alex Deymof329b932014-10-30 01:37:48 -0700378 Time cur_time = fake_clock_.GetWallclockTime();
Alex Deymo41a75a72014-04-15 15:36:22 -0700379 // Advance the time on the clock but don't call ResetEvaluation yet.
380 fake_clock_.SetWallclockTime(cur_time + TimeDelta::FromSeconds(4));
381
Amin Hassani4b717432019-01-14 16:24:20 -0800382 EXPECT_TRUE(eval_ctx_->IsWallclockTimeGreaterThan(cur_time -
383 TimeDelta::FromSeconds(1)));
Gilad Arnolda65fced2014-07-23 09:01:31 -0700384 EXPECT_FALSE(eval_ctx_->IsWallclockTimeGreaterThan(cur_time));
385 EXPECT_FALSE(eval_ctx_->IsWallclockTimeGreaterThan(
Amin Hassani4b717432019-01-14 16:24:20 -0800386 cur_time + TimeDelta::FromSeconds(1)));
Alex Deymo41a75a72014-04-15 15:36:22 -0700387 // Call ResetEvaluation now, which should use the new evaluation time.
388 eval_ctx_->ResetEvaluation();
389
390 cur_time = fake_clock_.GetWallclockTime();
Amin Hassani4b717432019-01-14 16:24:20 -0800391 EXPECT_TRUE(eval_ctx_->IsWallclockTimeGreaterThan(cur_time -
392 TimeDelta::FromSeconds(1)));
Gilad Arnolda65fced2014-07-23 09:01:31 -0700393 EXPECT_FALSE(eval_ctx_->IsWallclockTimeGreaterThan(cur_time));
394 EXPECT_FALSE(eval_ctx_->IsWallclockTimeGreaterThan(
Amin Hassani4b717432019-01-14 16:24:20 -0800395 cur_time + TimeDelta::FromSeconds(1)));
Alex Deymo41a75a72014-04-15 15:36:22 -0700396}
397
Gilad Arnolda65fced2014-07-23 09:01:31 -0700398TEST_F(UmEvaluationContextTest, ResetEvaluationResetsTimesMonotonic) {
Alex Deymof329b932014-10-30 01:37:48 -0700399 Time cur_time = fake_clock_.GetMonotonicTime();
Gilad Arnolda65fced2014-07-23 09:01:31 -0700400 // Advance the time on the clock but don't call ResetEvaluation yet.
401 fake_clock_.SetMonotonicTime(cur_time + TimeDelta::FromSeconds(4));
402
Amin Hassani4b717432019-01-14 16:24:20 -0800403 EXPECT_TRUE(eval_ctx_->IsMonotonicTimeGreaterThan(cur_time -
404 TimeDelta::FromSeconds(1)));
Gilad Arnolda65fced2014-07-23 09:01:31 -0700405 EXPECT_FALSE(eval_ctx_->IsMonotonicTimeGreaterThan(cur_time));
406 EXPECT_FALSE(eval_ctx_->IsMonotonicTimeGreaterThan(
Amin Hassani4b717432019-01-14 16:24:20 -0800407 cur_time + TimeDelta::FromSeconds(1)));
Gilad Arnolda65fced2014-07-23 09:01:31 -0700408 // Call ResetEvaluation now, which should use the new evaluation time.
409 eval_ctx_->ResetEvaluation();
410
411 cur_time = fake_clock_.GetMonotonicTime();
Amin Hassani4b717432019-01-14 16:24:20 -0800412 EXPECT_TRUE(eval_ctx_->IsMonotonicTimeGreaterThan(cur_time -
413 TimeDelta::FromSeconds(1)));
Gilad Arnolda65fced2014-07-23 09:01:31 -0700414 EXPECT_FALSE(eval_ctx_->IsMonotonicTimeGreaterThan(cur_time));
415 EXPECT_FALSE(eval_ctx_->IsMonotonicTimeGreaterThan(
Amin Hassani4b717432019-01-14 16:24:20 -0800416 cur_time + TimeDelta::FromSeconds(1)));
Gilad Arnolda65fced2014-07-23 09:01:31 -0700417}
418
419TEST_F(UmEvaluationContextTest,
420 IsWallclockTimeGreaterThanSignalsTriggerReevaluation) {
421 EXPECT_FALSE(eval_ctx_->IsWallclockTimeGreaterThan(
Alex Deymo41a75a72014-04-15 15:36:22 -0700422 fake_clock_.GetWallclockTime() + TimeDelta::FromSeconds(1)));
423
Gilad Arnolda65fced2014-07-23 09:01:31 -0700424 // The "false" from IsWallclockTimeGreaterThan means that's not that timestamp
425 // yet, so this should schedule a callback for when that happens.
Jae Hoon Kim9d9492f2019-06-17 14:52:48 -0700426 EXPECT_TRUE(eval_ctx_->RunOnValueChangeOrTimeout(base::DoNothing()));
Alex Deymo41a75a72014-04-15 15:36:22 -0700427}
428
Gilad Arnolda65fced2014-07-23 09:01:31 -0700429TEST_F(UmEvaluationContextTest,
430 IsMonotonicTimeGreaterThanSignalsTriggerReevaluation) {
431 EXPECT_FALSE(eval_ctx_->IsMonotonicTimeGreaterThan(
432 fake_clock_.GetMonotonicTime() + TimeDelta::FromSeconds(1)));
433
434 // The "false" from IsMonotonicTimeGreaterThan means that's not that timestamp
435 // yet, so this should schedule a callback for when that happens.
Jae Hoon Kim9d9492f2019-06-17 14:52:48 -0700436 EXPECT_TRUE(eval_ctx_->RunOnValueChangeOrTimeout(base::DoNothing()));
Gilad Arnolda65fced2014-07-23 09:01:31 -0700437}
438
439TEST_F(UmEvaluationContextTest,
440 IsWallclockTimeGreaterThanDoesntRecordPastTimestamps) {
441 // IsWallclockTimeGreaterThan() should ignore timestamps on the past for
442 // reevaluation.
443 EXPECT_TRUE(eval_ctx_->IsWallclockTimeGreaterThan(
Alex Deymo41a75a72014-04-15 15:36:22 -0700444 fake_clock_.GetWallclockTime() - TimeDelta::FromSeconds(20)));
Gilad Arnolda65fced2014-07-23 09:01:31 -0700445 EXPECT_TRUE(eval_ctx_->IsWallclockTimeGreaterThan(
Alex Deymo41a75a72014-04-15 15:36:22 -0700446 fake_clock_.GetWallclockTime() - TimeDelta::FromSeconds(1)));
447
448 // Callback should not be scheduled.
Jae Hoon Kim9d9492f2019-06-17 14:52:48 -0700449 EXPECT_FALSE(eval_ctx_->RunOnValueChangeOrTimeout(base::DoNothing()));
Alex Deymo41a75a72014-04-15 15:36:22 -0700450}
451
Gilad Arnolda65fced2014-07-23 09:01:31 -0700452TEST_F(UmEvaluationContextTest,
453 IsMonotonicTimeGreaterThanDoesntRecordPastTimestamps) {
454 // IsMonotonicTimeGreaterThan() should ignore timestamps on the past for
455 // reevaluation.
456 EXPECT_TRUE(eval_ctx_->IsMonotonicTimeGreaterThan(
457 fake_clock_.GetMonotonicTime() - TimeDelta::FromSeconds(20)));
458 EXPECT_TRUE(eval_ctx_->IsMonotonicTimeGreaterThan(
459 fake_clock_.GetMonotonicTime() - TimeDelta::FromSeconds(1)));
460
461 // Callback should not be scheduled.
Jae Hoon Kim9d9492f2019-06-17 14:52:48 -0700462 EXPECT_FALSE(eval_ctx_->RunOnValueChangeOrTimeout(base::DoNothing()));
Gilad Arnolda65fced2014-07-23 09:01:31 -0700463}
464
Alex Deymo63784a52014-05-28 10:46:14 -0700465TEST_F(UmEvaluationContextTest, DumpContext) {
David Zeuthenc1490282014-04-29 16:25:03 -0700466 // |fail_var_| yield "(no value)" since it is unset.
467 eval_ctx_->GetValue(&fail_var_);
468
469 // Check that this is included.
470 fake_int_var_.reset(new int(42));
471 eval_ctx_->GetValue(&fake_int_var_);
472
473 // Check that double-quotes are escaped properly.
474 fake_poll_var_.reset(new string("Hello \"world\"!"));
475 eval_ctx_->GetValue(&fake_poll_var_);
476
477 // Note that the variables are printed in alphabetical order. Also
Gilad Arnolda65fced2014-07-23 09:01:31 -0700478 // see UmEvaluationContextText::SetUp() where the values used for
479 // |evaluation_start_{monotonic,wallclock| are set.
Amin Hassani4b717432019-01-14 16:24:20 -0800480 EXPECT_EQ(
481 "{\n"
482 " \"evaluation_start_monotonic\": \"4/22/2009 19:25:00 GMT\",\n"
483 " \"evaluation_start_wallclock\": \"3/2/2006 1:23:45 GMT\",\n"
484 " \"variables\": {\n"
485 " \"fail_var\": \"(no value)\",\n"
486 " \"fake_int\": \"42\",\n"
487 " \"fake_poll\": \"Hello \\\"world\\\"!\"\n"
488 " }\n"
489 "}",
490 eval_ctx_->DumpContext());
David Zeuthenc1490282014-04-29 16:25:03 -0700491}
492
Alex Deymo63784a52014-05-28 10:46:14 -0700493} // namespace chromeos_update_manager