blob: 6a8475b0ca9478338f837e175d58d9c9744ead8a [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;
Alex Deymo23949d42014-02-05 15:20:59 -080042using std::string;
Ben Chan02f7c1d2014-10-18 15:18:02 -070043using std::unique_ptr;
Alex Deymo41a75a72014-04-15 15:36:22 -070044using testing::Return;
45using testing::StrictMock;
46using testing::_;
Alex Deymo23949d42014-02-05 15:20:59 -080047
Gilad Arnoldfb794f42014-07-01 15:36:31 -070048namespace chromeos_update_manager {
49
Alex Deymo53556ec2014-03-17 10:05:57 -070050namespace {
51
Alex Deymo53556ec2014-03-17 10:05:57 -070052// Sets the value of the passed pointer to true.
53void SetTrue(bool* value) {
54 *value = true;
55}
56
57bool GetBoolean(bool* value) {
58 return *value;
59}
60
Gilad Arnoldfb794f42014-07-01 15:36:31 -070061template<typename T>
62void ReadVar(scoped_refptr<EvaluationContext> ec, Variable<T>* var) {
63 ec->GetValue(var);
64}
Alex Deymo53556ec2014-03-17 10:05:57 -070065
Gilad Arnoldfb794f42014-07-01 15:36:31 -070066// Runs |evaluation|; if the value pointed by |count_p| is greater than zero,
67// decrement it and schedule a reevaluation; otherwise, writes true to |done_p|.
68void EvaluateRepeatedly(Closure evaluation, scoped_refptr<EvaluationContext> ec,
69 int* count_p, bool* done_p) {
70 evaluation.Run();
71
72 // Schedule reevaluation if needed.
73 if (*count_p > 0) {
74 Closure closure = Bind(EvaluateRepeatedly, evaluation, ec, count_p, done_p);
75 ASSERT_TRUE(ec->RunOnValueChangeOrTimeout(closure))
76 << "Failed to schedule reevaluation, count_p=" << *count_p;
77 (*count_p)--;
78 } else {
79 *done_p = true;
80 }
81}
82
83} // namespace
Alex Deymo23949d42014-02-05 15:20:59 -080084
Alex Deymo63784a52014-05-28 10:46:14 -070085class UmEvaluationContextTest : public ::testing::Test {
Alex Deymo23949d42014-02-05 15:20:59 -080086 protected:
Alex Deymo610277e2014-11-11 21:18:11 -080087 void SetUp() override {
Alex Deymo509dd532015-06-10 14:11:05 -070088 loop_.SetAsCurrent();
Gilad Arnolda65fced2014-07-23 09:01:31 -070089 // Apr 22, 2009 19:25:00 UTC (this is a random reference point).
90 fake_clock_.SetMonotonicTime(Time::FromTimeT(1240428300));
91 // Mar 2, 2006 1:23:45 UTC.
David Zeuthenc1490282014-04-29 16:25:03 -070092 fake_clock_.SetWallclockTime(Time::FromTimeT(1141262625));
Gilad Arnold83ffdda2014-08-08 13:30:31 -070093 eval_ctx_ = new EvaluationContext(
94 &fake_clock_, default_timeout_, default_timeout_,
Ben Chan02f7c1d2014-10-18 15:18:02 -070095 unique_ptr<base::Callback<void(EvaluationContext*)>>(nullptr));
Alex Deymo23949d42014-02-05 15:20:59 -080096 }
97
Alex Deymo610277e2014-11-11 21:18:11 -080098 void TearDown() override {
Gilad Arnoldfb794f42014-07-01 15:36:31 -070099 // Ensure that the evaluation context did not leak and is actually being
100 // destroyed.
101 if (eval_ctx_) {
102 base::WeakPtr<EvaluationContext> eval_ctx_weak_alias =
103 eval_ctx_->weak_ptr_factory_.GetWeakPtr();
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700104 ASSERT_NE(nullptr, eval_ctx_weak_alias.get());
Gilad Arnoldfb794f42014-07-01 15:36:31 -0700105 eval_ctx_ = nullptr;
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700106 EXPECT_EQ(nullptr, eval_ctx_weak_alias.get())
Gilad Arnoldfb794f42014-07-01 15:36:31 -0700107 << "The evaluation context was not destroyed! This is likely a bug "
108 "in how the test was written, look for leaking handles to the EC, "
109 "possibly through closure objects.";
110 }
111
Alex Deymo53556ec2014-03-17 10:05:57 -0700112 // Check that the evaluation context removed all the observers.
113 EXPECT_TRUE(fake_int_var_.observer_list_.empty());
114 EXPECT_TRUE(fake_async_var_.observer_list_.empty());
115 EXPECT_TRUE(fake_const_var_.observer_list_.empty());
116 EXPECT_TRUE(fake_poll_var_.observer_list_.empty());
Alex Deymo509dd532015-06-10 14:11:05 -0700117
118 EXPECT_FALSE(loop_.PendingTasks());
Alex Deymo53556ec2014-03-17 10:05:57 -0700119 }
120
Alex Deymof329b932014-10-30 01:37:48 -0700121 TimeDelta default_timeout_ = TimeDelta::FromSeconds(5);
Alex Deymo41a75a72014-04-15 15:36:22 -0700122
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700123 brillo::FakeMessageLoop loop_{nullptr};
Alex Deymo41a75a72014-04-15 15:36:22 -0700124 FakeClock fake_clock_;
Alex Deymo7b948f02014-03-10 17:01:10 -0700125 scoped_refptr<EvaluationContext> eval_ctx_;
Alex Deymo53556ec2014-03-17 10:05:57 -0700126
127 // FakeVariables used for testing the EvaluationContext. These are required
128 // here to prevent them from going away *before* the EvaluationContext under
129 // test does, which keeps a reference to them.
David Zeuthenc1490282014-04-29 16:25:03 -0700130 FakeVariable<bool> fail_var_ = {"fail_var", kVariableModePoll};
Alex Deymo53556ec2014-03-17 10:05:57 -0700131 FakeVariable<int> fake_int_var_ = {"fake_int", kVariableModePoll};
132 FakeVariable<string> fake_async_var_ = {"fake_async", kVariableModeAsync};
133 FakeVariable<string> fake_const_var_ = {"fake_const", kVariableModeConst};
134 FakeVariable<string> fake_poll_var_ = {"fake_poll",
135 TimeDelta::FromSeconds(1)};
Gilad Arnoldfb794f42014-07-01 15:36:31 -0700136 StrictMock<MockVariable<string>> mock_var_async_ {
137 "mock_var_async", kVariableModeAsync};
138 StrictMock<MockVariable<string>> mock_var_poll_ {
139 "mock_var_poll", kVariableModePoll};
Alex Deymo23949d42014-02-05 15:20:59 -0800140};
141
Alex Deymo63784a52014-05-28 10:46:14 -0700142TEST_F(UmEvaluationContextTest, GetValueFails) {
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700143 // FakeVariable is initialized as returning null.
144 EXPECT_EQ(nullptr, eval_ctx_->GetValue(&fake_int_var_));
Alex Deymo23949d42014-02-05 15:20:59 -0800145}
146
Alex Deymo63784a52014-05-28 10:46:14 -0700147TEST_F(UmEvaluationContextTest, GetValueFailsWithInvalidVar) {
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700148 EXPECT_EQ(nullptr, eval_ctx_->GetValue(static_cast<Variable<int>*>(nullptr)));
Alex Deymo23949d42014-02-05 15:20:59 -0800149}
150
Alex Deymo63784a52014-05-28 10:46:14 -0700151TEST_F(UmEvaluationContextTest, GetValueReturns) {
Alex Deymo23949d42014-02-05 15:20:59 -0800152 const int* p_fake_int;
153
154 fake_int_var_.reset(new int(42));
155 p_fake_int = eval_ctx_->GetValue(&fake_int_var_);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700156 ASSERT_NE(nullptr, p_fake_int);
Alex Deymo23949d42014-02-05 15:20:59 -0800157 EXPECT_EQ(42, *p_fake_int);
158}
159
Alex Deymo63784a52014-05-28 10:46:14 -0700160TEST_F(UmEvaluationContextTest, GetValueCached) {
Alex Deymo23949d42014-02-05 15:20:59 -0800161 const int* p_fake_int;
162
163 fake_int_var_.reset(new int(42));
164 p_fake_int = eval_ctx_->GetValue(&fake_int_var_);
165
166 // Check that if the variable changes, the EvaluationContext keeps returning
167 // the cached value.
168 fake_int_var_.reset(new int(5));
169
170 p_fake_int = eval_ctx_->GetValue(&fake_int_var_);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700171 ASSERT_NE(nullptr, p_fake_int);
Alex Deymo23949d42014-02-05 15:20:59 -0800172 EXPECT_EQ(42, *p_fake_int);
173}
174
Alex Deymo63784a52014-05-28 10:46:14 -0700175TEST_F(UmEvaluationContextTest, GetValueCachesNull) {
Alex Deymo23949d42014-02-05 15:20:59 -0800176 const int* p_fake_int = eval_ctx_->GetValue(&fake_int_var_);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700177 EXPECT_EQ(nullptr, p_fake_int);
Alex Deymo23949d42014-02-05 15:20:59 -0800178
179 fake_int_var_.reset(new int(42));
Alex Deymocc0e5cf2014-04-23 20:20:11 -0700180 // A second attempt to read the variable should not work because this
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700181 // EvaluationContext already got a null value.
Alex Deymo23949d42014-02-05 15:20:59 -0800182 p_fake_int = eval_ctx_->GetValue(&fake_int_var_);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700183 EXPECT_EQ(nullptr, p_fake_int);
Alex Deymo23949d42014-02-05 15:20:59 -0800184}
185
Alex Deymo63784a52014-05-28 10:46:14 -0700186TEST_F(UmEvaluationContextTest, GetValueMixedTypes) {
Alex Deymo23949d42014-02-05 15:20:59 -0800187 const int* p_fake_int;
188 const string* p_fake_string;
189
190 fake_int_var_.reset(new int(42));
Alex Deymo53556ec2014-03-17 10:05:57 -0700191 fake_poll_var_.reset(new string("Hello world!"));
Alex Deymo23949d42014-02-05 15:20:59 -0800192 // Check that the EvaluationContext can handle multiple Variable types. This
193 // is mostly a compile-time check due to the template nature of this method.
194 p_fake_int = eval_ctx_->GetValue(&fake_int_var_);
Alex Deymo53556ec2014-03-17 10:05:57 -0700195 p_fake_string = eval_ctx_->GetValue(&fake_poll_var_);
Alex Deymo23949d42014-02-05 15:20:59 -0800196
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700197 ASSERT_NE(nullptr, p_fake_int);
Alex Deymo23949d42014-02-05 15:20:59 -0800198 EXPECT_EQ(42, *p_fake_int);
199
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700200 ASSERT_NE(nullptr, p_fake_string);
Alex Deymo23949d42014-02-05 15:20:59 -0800201 EXPECT_EQ("Hello world!", *p_fake_string);
202}
203
Alex Deymo53556ec2014-03-17 10:05:57 -0700204// Test that we don't schedule an event if there's no variable to wait for.
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700205TEST_F(UmEvaluationContextTest, RunOnValueChangeOrTimeoutWithoutVariables) {
Alex Deymo53556ec2014-03-17 10:05:57 -0700206 fake_const_var_.reset(new string("Hello world!"));
207 EXPECT_EQ(*eval_ctx_->GetValue(&fake_const_var_), "Hello world!");
208
Jakub Pawlowski7e1dcf72018-07-26 00:29:42 -0700209 EXPECT_FALSE(eval_ctx_->RunOnValueChangeOrTimeout(base::DoNothing()));
Alex Deymo53556ec2014-03-17 10:05:57 -0700210}
211
Gilad Arnoldfb794f42014-07-01 15:36:31 -0700212// Test that reevaluation occurs when an async variable it depends on changes.
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700213TEST_F(UmEvaluationContextTest, RunOnValueChangeOrTimeoutWithVariables) {
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 // Check that the scheduled callback isn't run until we signal a ValueChaged.
Alex Deymo509dd532015-06-10 14:11:05 -0700220 MessageLoopRunMaxIterations(MessageLoop::current(), 100);
Alex Deymo53556ec2014-03-17 10:05:57 -0700221 EXPECT_FALSE(value);
222
223 fake_async_var_.NotifyValueChanged();
224 EXPECT_FALSE(value);
225 // Ensure that the scheduled callback isn't run until we are back on the main
226 // loop.
Alex Deymo509dd532015-06-10 14:11:05 -0700227 MessageLoopRunMaxIterations(MessageLoop::current(), 100);
Alex Deymo53556ec2014-03-17 10:05:57 -0700228 EXPECT_TRUE(value);
229}
230
231// Test that we don't re-schedule the events if we are attending one.
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700232TEST_F(UmEvaluationContextTest, RunOnValueChangeOrTimeoutCalledTwice) {
Alex Deymo53556ec2014-03-17 10:05:57 -0700233 fake_async_var_.reset(new string("Async value"));
234 eval_ctx_->GetValue(&fake_async_var_);
235
236 bool value = false;
237 EXPECT_TRUE(eval_ctx_->RunOnValueChangeOrTimeout(Bind(&SetTrue, &value)));
238 EXPECT_FALSE(eval_ctx_->RunOnValueChangeOrTimeout(Bind(&SetTrue, &value)));
239
240 // The scheduled event should still work.
241 fake_async_var_.NotifyValueChanged();
Alex Deymo509dd532015-06-10 14:11:05 -0700242 MessageLoopRunMaxIterations(MessageLoop::current(), 100);
Alex Deymo53556ec2014-03-17 10:05:57 -0700243 EXPECT_TRUE(value);
244}
245
Gilad Arnoldfb794f42014-07-01 15:36:31 -0700246// Test that reevaluation occurs when a polling timeout fires.
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700247TEST_F(UmEvaluationContextTest, RunOnValueChangeOrTimeoutRunsFromTimeout) {
Alex Deymo53556ec2014-03-17 10:05:57 -0700248 fake_poll_var_.reset(new string("Polled value"));
249 eval_ctx_->GetValue(&fake_poll_var_);
250
251 bool value = false;
252 EXPECT_TRUE(eval_ctx_->RunOnValueChangeOrTimeout(Bind(&SetTrue, &value)));
253 // Check that the scheduled callback isn't run until the timeout occurs.
Alex Deymo509dd532015-06-10 14:11:05 -0700254 MessageLoopRunMaxIterations(MessageLoop::current(), 10);
Alex Deymo53556ec2014-03-17 10:05:57 -0700255 EXPECT_FALSE(value);
Alex Deymo509dd532015-06-10 14:11:05 -0700256 MessageLoopRunUntil(MessageLoop::current(),
257 TimeDelta::FromSeconds(10),
258 Bind(&GetBoolean, &value));
Alex Deymo53556ec2014-03-17 10:05:57 -0700259 EXPECT_TRUE(value);
260}
261
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700262// Test that callback is called when evaluation context expires, and that it
Gilad Arnoldfd45a732014-08-07 15:53:46 -0700263// cannot be used again unless the expiration deadline is reset.
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700264TEST_F(UmEvaluationContextTest, RunOnValueChangeOrTimeoutExpires) {
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 // Check that the scheduled callback isn't run until the timeout occurs.
Alex Deymo509dd532015-06-10 14:11:05 -0700271 MessageLoopRunMaxIterations(MessageLoop::current(), 10);
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700272 EXPECT_FALSE(value);
Alex Deymo509dd532015-06-10 14:11:05 -0700273 MessageLoopRunUntil(MessageLoop::current(),
274 TimeDelta::FromSeconds(10),
275 Bind(&GetBoolean, &value));
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700276 EXPECT_TRUE(value);
277
278 // Ensure that we cannot reschedule an evaluation.
Jakub Pawlowski7e1dcf72018-07-26 00:29:42 -0700279 EXPECT_FALSE(eval_ctx_->RunOnValueChangeOrTimeout(base::DoNothing()));
Gilad Arnoldfd45a732014-08-07 15:53:46 -0700280
281 // Ensure that we can reschedule an evaluation after resetting expiration.
282 eval_ctx_->ResetExpiration();
Jakub Pawlowski7e1dcf72018-07-26 00:29:42 -0700283 EXPECT_TRUE(eval_ctx_->RunOnValueChangeOrTimeout(base::DoNothing()));
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700284}
285
286// Test that we clear the events when destroying the EvaluationContext.
287TEST_F(UmEvaluationContextTest, RemoveObserversAndTimeoutTest) {
288 fake_async_var_.reset(new string("Async value"));
289 eval_ctx_->GetValue(&fake_async_var_);
290
291 bool value = false;
292 EXPECT_TRUE(eval_ctx_->RunOnValueChangeOrTimeout(Bind(&SetTrue, &value)));
293 eval_ctx_ = nullptr;
294
295 // This should not trigger the callback since the EvaluationContext waiting
296 // for it is gone, and it should have remove all its observers.
297 fake_async_var_.NotifyValueChanged();
Alex Deymo509dd532015-06-10 14:11:05 -0700298 MessageLoopRunMaxIterations(MessageLoop::current(), 100);
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700299 EXPECT_FALSE(value);
300}
301
Gilad Arnoldfb794f42014-07-01 15:36:31 -0700302// Scheduling two reevaluations from the callback should succeed.
303TEST_F(UmEvaluationContextTest,
304 RunOnValueChangeOrTimeoutReevaluatesRepeatedly) {
305 fake_poll_var_.reset(new string("Polled value"));
306 Closure evaluation = Bind(ReadVar<string>, eval_ctx_, &fake_poll_var_);
307 int num_reevaluations = 2;
308 bool done = false;
309
310 // Run the evaluation once.
311 evaluation.Run();
312
313 // Schedule repeated reevaluations.
314 Closure closure = Bind(EvaluateRepeatedly, evaluation, eval_ctx_,
315 &num_reevaluations, &done);
316 ASSERT_TRUE(eval_ctx_->RunOnValueChangeOrTimeout(closure));
Alex Deymo509dd532015-06-10 14:11:05 -0700317 MessageLoopRunUntil(MessageLoop::current(),
318 TimeDelta::FromSeconds(10),
319 Bind(&GetBoolean, &done));
Gilad Arnoldfb794f42014-07-01 15:36:31 -0700320 EXPECT_EQ(0, num_reevaluations);
321}
322
Alex Deymodb799532014-03-21 13:00:00 -0700323// Test that we can delete the EvaluationContext while having pending events.
Alex Deymo63784a52014-05-28 10:46:14 -0700324TEST_F(UmEvaluationContextTest, ObjectDeletedWithPendingEventsTest) {
Alex Deymodb799532014-03-21 13:00:00 -0700325 fake_async_var_.reset(new string("Async value"));
326 fake_poll_var_.reset(new string("Polled value"));
327 eval_ctx_->GetValue(&fake_async_var_);
328 eval_ctx_->GetValue(&fake_poll_var_);
Jakub Pawlowski7e1dcf72018-07-26 00:29:42 -0700329 EXPECT_TRUE(eval_ctx_->RunOnValueChangeOrTimeout(base::DoNothing()));
Alex Deymodb799532014-03-21 13:00:00 -0700330 // TearDown() checks for leaked observers on this async_variable, which means
331 // that our object is still alive after removing its reference.
332}
333
334// Test that timed events fired after removal of the EvaluationContext don't
335// crash.
Alex Deymo63784a52014-05-28 10:46:14 -0700336TEST_F(UmEvaluationContextTest, TimeoutEventAfterDeleteTest) {
Alex Deymo0bb23412015-06-19 00:04:46 -0700337 FakeVariable<string> fake_short_poll_var = {"fake_short_poll",
338 TimeDelta::FromSeconds(1)};
Alex Deymodb799532014-03-21 13:00:00 -0700339 fake_short_poll_var.reset(new string("Polled value"));
340 eval_ctx_->GetValue(&fake_short_poll_var);
341 bool value = false;
342 EXPECT_TRUE(eval_ctx_->RunOnValueChangeOrTimeout(Bind(&SetTrue, &value)));
343 // Remove the last reference to the EvaluationContext and run the loop for
Alex Deymo0bb23412015-06-19 00:04:46 -0700344 // 10 seconds to give time to the main loop to trigger the timeout Event (of 1
345 // second). Our callback should not be called because the EvaluationContext
Alex Deymodb799532014-03-21 13:00:00 -0700346 // was removed before the timeout event is attended.
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700347 eval_ctx_ = nullptr;
Alex Deymo509dd532015-06-10 14:11:05 -0700348 MessageLoopRunUntil(MessageLoop::current(),
349 TimeDelta::FromSeconds(10),
350 Bind(&GetBoolean, &value));
Alex Deymodb799532014-03-21 13:00:00 -0700351 EXPECT_FALSE(value);
352}
353
Alex Deymo63784a52014-05-28 10:46:14 -0700354TEST_F(UmEvaluationContextTest, DefaultTimeout) {
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700355 // Test that the evaluation timeout calculation uses the default timeout on
356 // setup.
Alex Deymo41a75a72014-04-15 15:36:22 -0700357 EXPECT_CALL(mock_var_async_, GetValue(default_timeout_, _))
358 .WillOnce(Return(nullptr));
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700359 EXPECT_EQ(nullptr, eval_ctx_->GetValue(&mock_var_async_));
Alex Deymo41a75a72014-04-15 15:36:22 -0700360}
361
Alex Deymo63784a52014-05-28 10:46:14 -0700362TEST_F(UmEvaluationContextTest, TimeoutUpdatesWithMonotonicTime) {
Alex Deymo41a75a72014-04-15 15:36:22 -0700363 fake_clock_.SetMonotonicTime(
364 fake_clock_.GetMonotonicTime() + TimeDelta::FromSeconds(1));
365
366 TimeDelta timeout = default_timeout_ - TimeDelta::FromSeconds(1);
367
368 EXPECT_CALL(mock_var_async_, GetValue(timeout, _))
369 .WillOnce(Return(nullptr));
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700370 EXPECT_EQ(nullptr, eval_ctx_->GetValue(&mock_var_async_));
Alex Deymo41a75a72014-04-15 15:36:22 -0700371}
372
Gilad Arnolda65fced2014-07-23 09:01:31 -0700373TEST_F(UmEvaluationContextTest, ResetEvaluationResetsTimesWallclock) {
Alex Deymof329b932014-10-30 01:37:48 -0700374 Time cur_time = fake_clock_.GetWallclockTime();
Alex Deymo41a75a72014-04-15 15:36:22 -0700375 // Advance the time on the clock but don't call ResetEvaluation yet.
376 fake_clock_.SetWallclockTime(cur_time + TimeDelta::FromSeconds(4));
377
Gilad Arnolda65fced2014-07-23 09:01:31 -0700378 EXPECT_TRUE(eval_ctx_->IsWallclockTimeGreaterThan(
379 cur_time - TimeDelta::FromSeconds(1)));
380 EXPECT_FALSE(eval_ctx_->IsWallclockTimeGreaterThan(cur_time));
381 EXPECT_FALSE(eval_ctx_->IsWallclockTimeGreaterThan(
382 cur_time + TimeDelta::FromSeconds(1)));
Alex Deymo41a75a72014-04-15 15:36:22 -0700383 // Call ResetEvaluation now, which should use the new evaluation time.
384 eval_ctx_->ResetEvaluation();
385
386 cur_time = fake_clock_.GetWallclockTime();
Gilad Arnolda65fced2014-07-23 09:01:31 -0700387 EXPECT_TRUE(eval_ctx_->IsWallclockTimeGreaterThan(
388 cur_time - TimeDelta::FromSeconds(1)));
389 EXPECT_FALSE(eval_ctx_->IsWallclockTimeGreaterThan(cur_time));
390 EXPECT_FALSE(eval_ctx_->IsWallclockTimeGreaterThan(
391 cur_time + TimeDelta::FromSeconds(1)));
Alex Deymo41a75a72014-04-15 15:36:22 -0700392}
393
Gilad Arnolda65fced2014-07-23 09:01:31 -0700394TEST_F(UmEvaluationContextTest, ResetEvaluationResetsTimesMonotonic) {
Alex Deymof329b932014-10-30 01:37:48 -0700395 Time cur_time = fake_clock_.GetMonotonicTime();
Gilad Arnolda65fced2014-07-23 09:01:31 -0700396 // Advance the time on the clock but don't call ResetEvaluation yet.
397 fake_clock_.SetMonotonicTime(cur_time + TimeDelta::FromSeconds(4));
398
399 EXPECT_TRUE(eval_ctx_->IsMonotonicTimeGreaterThan(
400 cur_time - TimeDelta::FromSeconds(1)));
401 EXPECT_FALSE(eval_ctx_->IsMonotonicTimeGreaterThan(cur_time));
402 EXPECT_FALSE(eval_ctx_->IsMonotonicTimeGreaterThan(
403 cur_time + TimeDelta::FromSeconds(1)));
404 // Call ResetEvaluation now, which should use the new evaluation time.
405 eval_ctx_->ResetEvaluation();
406
407 cur_time = fake_clock_.GetMonotonicTime();
408 EXPECT_TRUE(eval_ctx_->IsMonotonicTimeGreaterThan(
409 cur_time - TimeDelta::FromSeconds(1)));
410 EXPECT_FALSE(eval_ctx_->IsMonotonicTimeGreaterThan(cur_time));
411 EXPECT_FALSE(eval_ctx_->IsMonotonicTimeGreaterThan(
412 cur_time + TimeDelta::FromSeconds(1)));
413}
414
415TEST_F(UmEvaluationContextTest,
416 IsWallclockTimeGreaterThanSignalsTriggerReevaluation) {
417 EXPECT_FALSE(eval_ctx_->IsWallclockTimeGreaterThan(
Alex Deymo41a75a72014-04-15 15:36:22 -0700418 fake_clock_.GetWallclockTime() + TimeDelta::FromSeconds(1)));
419
Gilad Arnolda65fced2014-07-23 09:01:31 -0700420 // The "false" from IsWallclockTimeGreaterThan means that's not that timestamp
421 // yet, so this should schedule a callback for when that happens.
Jakub Pawlowski7e1dcf72018-07-26 00:29:42 -0700422 EXPECT_TRUE(eval_ctx_->RunOnValueChangeOrTimeout(base::DoNothing()));
Alex Deymo41a75a72014-04-15 15:36:22 -0700423}
424
Gilad Arnolda65fced2014-07-23 09:01:31 -0700425TEST_F(UmEvaluationContextTest,
426 IsMonotonicTimeGreaterThanSignalsTriggerReevaluation) {
427 EXPECT_FALSE(eval_ctx_->IsMonotonicTimeGreaterThan(
428 fake_clock_.GetMonotonicTime() + TimeDelta::FromSeconds(1)));
429
430 // The "false" from IsMonotonicTimeGreaterThan means that's not that timestamp
431 // yet, so this should schedule a callback for when that happens.
Jakub Pawlowski7e1dcf72018-07-26 00:29:42 -0700432 EXPECT_TRUE(eval_ctx_->RunOnValueChangeOrTimeout(base::DoNothing()));
Gilad Arnolda65fced2014-07-23 09:01:31 -0700433}
434
435TEST_F(UmEvaluationContextTest,
436 IsWallclockTimeGreaterThanDoesntRecordPastTimestamps) {
437 // IsWallclockTimeGreaterThan() should ignore timestamps on the past for
438 // reevaluation.
439 EXPECT_TRUE(eval_ctx_->IsWallclockTimeGreaterThan(
Alex Deymo41a75a72014-04-15 15:36:22 -0700440 fake_clock_.GetWallclockTime() - TimeDelta::FromSeconds(20)));
Gilad Arnolda65fced2014-07-23 09:01:31 -0700441 EXPECT_TRUE(eval_ctx_->IsWallclockTimeGreaterThan(
Alex Deymo41a75a72014-04-15 15:36:22 -0700442 fake_clock_.GetWallclockTime() - TimeDelta::FromSeconds(1)));
443
444 // Callback should not be scheduled.
Jakub Pawlowski7e1dcf72018-07-26 00:29:42 -0700445 EXPECT_FALSE(eval_ctx_->RunOnValueChangeOrTimeout(base::DoNothing()));
Alex Deymo41a75a72014-04-15 15:36:22 -0700446}
447
Gilad Arnolda65fced2014-07-23 09:01:31 -0700448TEST_F(UmEvaluationContextTest,
449 IsMonotonicTimeGreaterThanDoesntRecordPastTimestamps) {
450 // IsMonotonicTimeGreaterThan() should ignore timestamps on the past for
451 // reevaluation.
452 EXPECT_TRUE(eval_ctx_->IsMonotonicTimeGreaterThan(
453 fake_clock_.GetMonotonicTime() - TimeDelta::FromSeconds(20)));
454 EXPECT_TRUE(eval_ctx_->IsMonotonicTimeGreaterThan(
455 fake_clock_.GetMonotonicTime() - TimeDelta::FromSeconds(1)));
456
457 // Callback should not be scheduled.
Jakub Pawlowski7e1dcf72018-07-26 00:29:42 -0700458 EXPECT_FALSE(eval_ctx_->RunOnValueChangeOrTimeout(base::DoNothing()));
Gilad Arnolda65fced2014-07-23 09:01:31 -0700459}
460
Alex Deymo63784a52014-05-28 10:46:14 -0700461TEST_F(UmEvaluationContextTest, DumpContext) {
David Zeuthenc1490282014-04-29 16:25:03 -0700462 // |fail_var_| yield "(no value)" since it is unset.
463 eval_ctx_->GetValue(&fail_var_);
464
465 // Check that this is included.
466 fake_int_var_.reset(new int(42));
467 eval_ctx_->GetValue(&fake_int_var_);
468
469 // Check that double-quotes are escaped properly.
470 fake_poll_var_.reset(new string("Hello \"world\"!"));
471 eval_ctx_->GetValue(&fake_poll_var_);
472
473 // Note that the variables are printed in alphabetical order. Also
Gilad Arnolda65fced2014-07-23 09:01:31 -0700474 // see UmEvaluationContextText::SetUp() where the values used for
475 // |evaluation_start_{monotonic,wallclock| are set.
David Zeuthenc1490282014-04-29 16:25:03 -0700476 EXPECT_EQ("{\n"
Gilad Arnolda65fced2014-07-23 09:01:31 -0700477 " \"evaluation_start_monotonic\": \"4/22/2009 19:25:00 GMT\",\n"
478 " \"evaluation_start_wallclock\": \"3/2/2006 1:23:45 GMT\",\n"
David Zeuthenc1490282014-04-29 16:25:03 -0700479 " \"variables\": {\n"
480 " \"fail_var\": \"(no value)\",\n"
481 " \"fake_int\": \"42\",\n"
482 " \"fake_poll\": \"Hello \\\"world\\\"!\"\n"
483 " }\n"
Gilad Arnold6e5ab5c2014-06-23 15:13:56 -0700484 "}",
David Zeuthenc1490282014-04-29 16:25:03 -0700485 eval_ctx_->DumpContext());
486}
487
Alex Deymo63784a52014-05-28 10:46:14 -0700488} // namespace chromeos_update_manager