Alex Deymo | aea4c1c | 2015-08-19 20:24:43 -0700 | [diff] [blame] | 1 | // |
| 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 Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 16 | |
Alex Deymo | aab50e3 | 2014-11-10 19:55:35 -0800 | [diff] [blame] | 17 | #include "update_engine/update_manager/evaluation_context.h" |
| 18 | |
Ben Chan | 02f7c1d | 2014-10-18 15:18:02 -0700 | [diff] [blame] | 19 | #include <memory> |
Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 20 | #include <string> |
| 21 | |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 22 | #include <base/bind.h> |
Jakub Pawlowski | 7e1dcf7 | 2018-07-26 00:29:42 -0700 | [diff] [blame^] | 23 | #include <base/bind_helpers.h> |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 24 | #include <brillo/message_loops/fake_message_loop.h> |
| 25 | #include <brillo/message_loops/message_loop_utils.h> |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 26 | #include <gtest/gtest.h> |
| 27 | |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 28 | #include "update_engine/common/fake_clock.h" |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 29 | #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 Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 33 | |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 34 | using base::Bind; |
Gilad Arnold | fb794f4 | 2014-07-01 15:36:31 -0700 | [diff] [blame] | 35 | using base::Closure; |
Alex Deymo | 41a75a7 | 2014-04-15 15:36:22 -0700 | [diff] [blame] | 36 | using base::Time; |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 37 | using base::TimeDelta; |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 38 | using brillo::MessageLoop; |
| 39 | using brillo::MessageLoopRunMaxIterations; |
| 40 | using brillo::MessageLoopRunUntil; |
Alex Deymo | 41a75a7 | 2014-04-15 15:36:22 -0700 | [diff] [blame] | 41 | using chromeos_update_engine::FakeClock; |
Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 42 | using std::string; |
Ben Chan | 02f7c1d | 2014-10-18 15:18:02 -0700 | [diff] [blame] | 43 | using std::unique_ptr; |
Alex Deymo | 41a75a7 | 2014-04-15 15:36:22 -0700 | [diff] [blame] | 44 | using testing::Return; |
| 45 | using testing::StrictMock; |
| 46 | using testing::_; |
Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 47 | |
Gilad Arnold | fb794f4 | 2014-07-01 15:36:31 -0700 | [diff] [blame] | 48 | namespace chromeos_update_manager { |
| 49 | |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 50 | namespace { |
| 51 | |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 52 | // Sets the value of the passed pointer to true. |
| 53 | void SetTrue(bool* value) { |
| 54 | *value = true; |
| 55 | } |
| 56 | |
| 57 | bool GetBoolean(bool* value) { |
| 58 | return *value; |
| 59 | } |
| 60 | |
Gilad Arnold | fb794f4 | 2014-07-01 15:36:31 -0700 | [diff] [blame] | 61 | template<typename T> |
| 62 | void ReadVar(scoped_refptr<EvaluationContext> ec, Variable<T>* var) { |
| 63 | ec->GetValue(var); |
| 64 | } |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 65 | |
Gilad Arnold | fb794f4 | 2014-07-01 15:36:31 -0700 | [diff] [blame] | 66 | // 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|. |
| 68 | void 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 Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 84 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 85 | class UmEvaluationContextTest : public ::testing::Test { |
Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 86 | protected: |
Alex Deymo | 610277e | 2014-11-11 21:18:11 -0800 | [diff] [blame] | 87 | void SetUp() override { |
Alex Deymo | 509dd53 | 2015-06-10 14:11:05 -0700 | [diff] [blame] | 88 | loop_.SetAsCurrent(); |
Gilad Arnold | a65fced | 2014-07-23 09:01:31 -0700 | [diff] [blame] | 89 | // 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 Zeuthen | c149028 | 2014-04-29 16:25:03 -0700 | [diff] [blame] | 92 | fake_clock_.SetWallclockTime(Time::FromTimeT(1141262625)); |
Gilad Arnold | 83ffdda | 2014-08-08 13:30:31 -0700 | [diff] [blame] | 93 | eval_ctx_ = new EvaluationContext( |
| 94 | &fake_clock_, default_timeout_, default_timeout_, |
Ben Chan | 02f7c1d | 2014-10-18 15:18:02 -0700 | [diff] [blame] | 95 | unique_ptr<base::Callback<void(EvaluationContext*)>>(nullptr)); |
Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 96 | } |
| 97 | |
Alex Deymo | 610277e | 2014-11-11 21:18:11 -0800 | [diff] [blame] | 98 | void TearDown() override { |
Gilad Arnold | fb794f4 | 2014-07-01 15:36:31 -0700 | [diff] [blame] | 99 | // 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 Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 104 | ASSERT_NE(nullptr, eval_ctx_weak_alias.get()); |
Gilad Arnold | fb794f4 | 2014-07-01 15:36:31 -0700 | [diff] [blame] | 105 | eval_ctx_ = nullptr; |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 106 | EXPECT_EQ(nullptr, eval_ctx_weak_alias.get()) |
Gilad Arnold | fb794f4 | 2014-07-01 15:36:31 -0700 | [diff] [blame] | 107 | << "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 Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 112 | // 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 Deymo | 509dd53 | 2015-06-10 14:11:05 -0700 | [diff] [blame] | 117 | |
| 118 | EXPECT_FALSE(loop_.PendingTasks()); |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 119 | } |
| 120 | |
Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 121 | TimeDelta default_timeout_ = TimeDelta::FromSeconds(5); |
Alex Deymo | 41a75a7 | 2014-04-15 15:36:22 -0700 | [diff] [blame] | 122 | |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 123 | brillo::FakeMessageLoop loop_{nullptr}; |
Alex Deymo | 41a75a7 | 2014-04-15 15:36:22 -0700 | [diff] [blame] | 124 | FakeClock fake_clock_; |
Alex Deymo | 7b948f0 | 2014-03-10 17:01:10 -0700 | [diff] [blame] | 125 | scoped_refptr<EvaluationContext> eval_ctx_; |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 126 | |
| 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 Zeuthen | c149028 | 2014-04-29 16:25:03 -0700 | [diff] [blame] | 130 | FakeVariable<bool> fail_var_ = {"fail_var", kVariableModePoll}; |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 131 | 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 Arnold | fb794f4 | 2014-07-01 15:36:31 -0700 | [diff] [blame] | 136 | StrictMock<MockVariable<string>> mock_var_async_ { |
| 137 | "mock_var_async", kVariableModeAsync}; |
| 138 | StrictMock<MockVariable<string>> mock_var_poll_ { |
| 139 | "mock_var_poll", kVariableModePoll}; |
Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 140 | }; |
| 141 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 142 | TEST_F(UmEvaluationContextTest, GetValueFails) { |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 143 | // FakeVariable is initialized as returning null. |
| 144 | EXPECT_EQ(nullptr, eval_ctx_->GetValue(&fake_int_var_)); |
Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 145 | } |
| 146 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 147 | TEST_F(UmEvaluationContextTest, GetValueFailsWithInvalidVar) { |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 148 | EXPECT_EQ(nullptr, eval_ctx_->GetValue(static_cast<Variable<int>*>(nullptr))); |
Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 149 | } |
| 150 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 151 | TEST_F(UmEvaluationContextTest, GetValueReturns) { |
Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 152 | 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 Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 156 | ASSERT_NE(nullptr, p_fake_int); |
Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 157 | EXPECT_EQ(42, *p_fake_int); |
| 158 | } |
| 159 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 160 | TEST_F(UmEvaluationContextTest, GetValueCached) { |
Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 161 | 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 Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 171 | ASSERT_NE(nullptr, p_fake_int); |
Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 172 | EXPECT_EQ(42, *p_fake_int); |
| 173 | } |
| 174 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 175 | TEST_F(UmEvaluationContextTest, GetValueCachesNull) { |
Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 176 | const int* p_fake_int = eval_ctx_->GetValue(&fake_int_var_); |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 177 | EXPECT_EQ(nullptr, p_fake_int); |
Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 178 | |
| 179 | fake_int_var_.reset(new int(42)); |
Alex Deymo | cc0e5cf | 2014-04-23 20:20:11 -0700 | [diff] [blame] | 180 | // A second attempt to read the variable should not work because this |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 181 | // EvaluationContext already got a null value. |
Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 182 | p_fake_int = eval_ctx_->GetValue(&fake_int_var_); |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 183 | EXPECT_EQ(nullptr, p_fake_int); |
Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 184 | } |
| 185 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 186 | TEST_F(UmEvaluationContextTest, GetValueMixedTypes) { |
Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 187 | const int* p_fake_int; |
| 188 | const string* p_fake_string; |
| 189 | |
| 190 | fake_int_var_.reset(new int(42)); |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 191 | fake_poll_var_.reset(new string("Hello world!")); |
Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 192 | // 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 Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 195 | p_fake_string = eval_ctx_->GetValue(&fake_poll_var_); |
Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 196 | |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 197 | ASSERT_NE(nullptr, p_fake_int); |
Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 198 | EXPECT_EQ(42, *p_fake_int); |
| 199 | |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 200 | ASSERT_NE(nullptr, p_fake_string); |
Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 201 | EXPECT_EQ("Hello world!", *p_fake_string); |
| 202 | } |
| 203 | |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 204 | // Test that we don't schedule an event if there's no variable to wait for. |
Gilad Arnold | f9f85d6 | 2014-06-19 18:07:01 -0700 | [diff] [blame] | 205 | TEST_F(UmEvaluationContextTest, RunOnValueChangeOrTimeoutWithoutVariables) { |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 206 | fake_const_var_.reset(new string("Hello world!")); |
| 207 | EXPECT_EQ(*eval_ctx_->GetValue(&fake_const_var_), "Hello world!"); |
| 208 | |
Jakub Pawlowski | 7e1dcf7 | 2018-07-26 00:29:42 -0700 | [diff] [blame^] | 209 | EXPECT_FALSE(eval_ctx_->RunOnValueChangeOrTimeout(base::DoNothing())); |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 210 | } |
| 211 | |
Gilad Arnold | fb794f4 | 2014-07-01 15:36:31 -0700 | [diff] [blame] | 212 | // Test that reevaluation occurs when an async variable it depends on changes. |
Gilad Arnold | f9f85d6 | 2014-06-19 18:07:01 -0700 | [diff] [blame] | 213 | TEST_F(UmEvaluationContextTest, RunOnValueChangeOrTimeoutWithVariables) { |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 214 | 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 Deymo | 509dd53 | 2015-06-10 14:11:05 -0700 | [diff] [blame] | 220 | MessageLoopRunMaxIterations(MessageLoop::current(), 100); |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 221 | 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 Deymo | 509dd53 | 2015-06-10 14:11:05 -0700 | [diff] [blame] | 227 | MessageLoopRunMaxIterations(MessageLoop::current(), 100); |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 228 | EXPECT_TRUE(value); |
| 229 | } |
| 230 | |
| 231 | // Test that we don't re-schedule the events if we are attending one. |
Gilad Arnold | f9f85d6 | 2014-06-19 18:07:01 -0700 | [diff] [blame] | 232 | TEST_F(UmEvaluationContextTest, RunOnValueChangeOrTimeoutCalledTwice) { |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 233 | 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 Deymo | 509dd53 | 2015-06-10 14:11:05 -0700 | [diff] [blame] | 242 | MessageLoopRunMaxIterations(MessageLoop::current(), 100); |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 243 | EXPECT_TRUE(value); |
| 244 | } |
| 245 | |
Gilad Arnold | fb794f4 | 2014-07-01 15:36:31 -0700 | [diff] [blame] | 246 | // Test that reevaluation occurs when a polling timeout fires. |
Gilad Arnold | f9f85d6 | 2014-06-19 18:07:01 -0700 | [diff] [blame] | 247 | TEST_F(UmEvaluationContextTest, RunOnValueChangeOrTimeoutRunsFromTimeout) { |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 248 | 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 Deymo | 509dd53 | 2015-06-10 14:11:05 -0700 | [diff] [blame] | 254 | MessageLoopRunMaxIterations(MessageLoop::current(), 10); |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 255 | EXPECT_FALSE(value); |
Alex Deymo | 509dd53 | 2015-06-10 14:11:05 -0700 | [diff] [blame] | 256 | MessageLoopRunUntil(MessageLoop::current(), |
| 257 | TimeDelta::FromSeconds(10), |
| 258 | Bind(&GetBoolean, &value)); |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 259 | EXPECT_TRUE(value); |
| 260 | } |
| 261 | |
Gilad Arnold | f9f85d6 | 2014-06-19 18:07:01 -0700 | [diff] [blame] | 262 | // Test that callback is called when evaluation context expires, and that it |
Gilad Arnold | fd45a73 | 2014-08-07 15:53:46 -0700 | [diff] [blame] | 263 | // cannot be used again unless the expiration deadline is reset. |
Gilad Arnold | f9f85d6 | 2014-06-19 18:07:01 -0700 | [diff] [blame] | 264 | TEST_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 Deymo | 509dd53 | 2015-06-10 14:11:05 -0700 | [diff] [blame] | 271 | MessageLoopRunMaxIterations(MessageLoop::current(), 10); |
Gilad Arnold | f9f85d6 | 2014-06-19 18:07:01 -0700 | [diff] [blame] | 272 | EXPECT_FALSE(value); |
Alex Deymo | 509dd53 | 2015-06-10 14:11:05 -0700 | [diff] [blame] | 273 | MessageLoopRunUntil(MessageLoop::current(), |
| 274 | TimeDelta::FromSeconds(10), |
| 275 | Bind(&GetBoolean, &value)); |
Gilad Arnold | f9f85d6 | 2014-06-19 18:07:01 -0700 | [diff] [blame] | 276 | EXPECT_TRUE(value); |
| 277 | |
| 278 | // Ensure that we cannot reschedule an evaluation. |
Jakub Pawlowski | 7e1dcf7 | 2018-07-26 00:29:42 -0700 | [diff] [blame^] | 279 | EXPECT_FALSE(eval_ctx_->RunOnValueChangeOrTimeout(base::DoNothing())); |
Gilad Arnold | fd45a73 | 2014-08-07 15:53:46 -0700 | [diff] [blame] | 280 | |
| 281 | // Ensure that we can reschedule an evaluation after resetting expiration. |
| 282 | eval_ctx_->ResetExpiration(); |
Jakub Pawlowski | 7e1dcf7 | 2018-07-26 00:29:42 -0700 | [diff] [blame^] | 283 | EXPECT_TRUE(eval_ctx_->RunOnValueChangeOrTimeout(base::DoNothing())); |
Gilad Arnold | f9f85d6 | 2014-06-19 18:07:01 -0700 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | // Test that we clear the events when destroying the EvaluationContext. |
| 287 | TEST_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 Deymo | 509dd53 | 2015-06-10 14:11:05 -0700 | [diff] [blame] | 298 | MessageLoopRunMaxIterations(MessageLoop::current(), 100); |
Gilad Arnold | f9f85d6 | 2014-06-19 18:07:01 -0700 | [diff] [blame] | 299 | EXPECT_FALSE(value); |
| 300 | } |
| 301 | |
Gilad Arnold | fb794f4 | 2014-07-01 15:36:31 -0700 | [diff] [blame] | 302 | // Scheduling two reevaluations from the callback should succeed. |
| 303 | TEST_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 Deymo | 509dd53 | 2015-06-10 14:11:05 -0700 | [diff] [blame] | 317 | MessageLoopRunUntil(MessageLoop::current(), |
| 318 | TimeDelta::FromSeconds(10), |
| 319 | Bind(&GetBoolean, &done)); |
Gilad Arnold | fb794f4 | 2014-07-01 15:36:31 -0700 | [diff] [blame] | 320 | EXPECT_EQ(0, num_reevaluations); |
| 321 | } |
| 322 | |
Alex Deymo | db79953 | 2014-03-21 13:00:00 -0700 | [diff] [blame] | 323 | // Test that we can delete the EvaluationContext while having pending events. |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 324 | TEST_F(UmEvaluationContextTest, ObjectDeletedWithPendingEventsTest) { |
Alex Deymo | db79953 | 2014-03-21 13:00:00 -0700 | [diff] [blame] | 325 | 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 Pawlowski | 7e1dcf7 | 2018-07-26 00:29:42 -0700 | [diff] [blame^] | 329 | EXPECT_TRUE(eval_ctx_->RunOnValueChangeOrTimeout(base::DoNothing())); |
Alex Deymo | db79953 | 2014-03-21 13:00:00 -0700 | [diff] [blame] | 330 | // 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 Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 336 | TEST_F(UmEvaluationContextTest, TimeoutEventAfterDeleteTest) { |
Alex Deymo | 0bb2341 | 2015-06-19 00:04:46 -0700 | [diff] [blame] | 337 | FakeVariable<string> fake_short_poll_var = {"fake_short_poll", |
| 338 | TimeDelta::FromSeconds(1)}; |
Alex Deymo | db79953 | 2014-03-21 13:00:00 -0700 | [diff] [blame] | 339 | 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 Deymo | 0bb2341 | 2015-06-19 00:04:46 -0700 | [diff] [blame] | 344 | // 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 Deymo | db79953 | 2014-03-21 13:00:00 -0700 | [diff] [blame] | 346 | // was removed before the timeout event is attended. |
Gilad Arnold | f9f85d6 | 2014-06-19 18:07:01 -0700 | [diff] [blame] | 347 | eval_ctx_ = nullptr; |
Alex Deymo | 509dd53 | 2015-06-10 14:11:05 -0700 | [diff] [blame] | 348 | MessageLoopRunUntil(MessageLoop::current(), |
| 349 | TimeDelta::FromSeconds(10), |
| 350 | Bind(&GetBoolean, &value)); |
Alex Deymo | db79953 | 2014-03-21 13:00:00 -0700 | [diff] [blame] | 351 | EXPECT_FALSE(value); |
| 352 | } |
| 353 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 354 | TEST_F(UmEvaluationContextTest, DefaultTimeout) { |
Gilad Arnold | f9f85d6 | 2014-06-19 18:07:01 -0700 | [diff] [blame] | 355 | // Test that the evaluation timeout calculation uses the default timeout on |
| 356 | // setup. |
Alex Deymo | 41a75a7 | 2014-04-15 15:36:22 -0700 | [diff] [blame] | 357 | EXPECT_CALL(mock_var_async_, GetValue(default_timeout_, _)) |
| 358 | .WillOnce(Return(nullptr)); |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 359 | EXPECT_EQ(nullptr, eval_ctx_->GetValue(&mock_var_async_)); |
Alex Deymo | 41a75a7 | 2014-04-15 15:36:22 -0700 | [diff] [blame] | 360 | } |
| 361 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 362 | TEST_F(UmEvaluationContextTest, TimeoutUpdatesWithMonotonicTime) { |
Alex Deymo | 41a75a7 | 2014-04-15 15:36:22 -0700 | [diff] [blame] | 363 | 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 Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 370 | EXPECT_EQ(nullptr, eval_ctx_->GetValue(&mock_var_async_)); |
Alex Deymo | 41a75a7 | 2014-04-15 15:36:22 -0700 | [diff] [blame] | 371 | } |
| 372 | |
Gilad Arnold | a65fced | 2014-07-23 09:01:31 -0700 | [diff] [blame] | 373 | TEST_F(UmEvaluationContextTest, ResetEvaluationResetsTimesWallclock) { |
Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 374 | Time cur_time = fake_clock_.GetWallclockTime(); |
Alex Deymo | 41a75a7 | 2014-04-15 15:36:22 -0700 | [diff] [blame] | 375 | // Advance the time on the clock but don't call ResetEvaluation yet. |
| 376 | fake_clock_.SetWallclockTime(cur_time + TimeDelta::FromSeconds(4)); |
| 377 | |
Gilad Arnold | a65fced | 2014-07-23 09:01:31 -0700 | [diff] [blame] | 378 | 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 Deymo | 41a75a7 | 2014-04-15 15:36:22 -0700 | [diff] [blame] | 383 | // Call ResetEvaluation now, which should use the new evaluation time. |
| 384 | eval_ctx_->ResetEvaluation(); |
| 385 | |
| 386 | cur_time = fake_clock_.GetWallclockTime(); |
Gilad Arnold | a65fced | 2014-07-23 09:01:31 -0700 | [diff] [blame] | 387 | 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 Deymo | 41a75a7 | 2014-04-15 15:36:22 -0700 | [diff] [blame] | 392 | } |
| 393 | |
Gilad Arnold | a65fced | 2014-07-23 09:01:31 -0700 | [diff] [blame] | 394 | TEST_F(UmEvaluationContextTest, ResetEvaluationResetsTimesMonotonic) { |
Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 395 | Time cur_time = fake_clock_.GetMonotonicTime(); |
Gilad Arnold | a65fced | 2014-07-23 09:01:31 -0700 | [diff] [blame] | 396 | // 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 | |
| 415 | TEST_F(UmEvaluationContextTest, |
| 416 | IsWallclockTimeGreaterThanSignalsTriggerReevaluation) { |
| 417 | EXPECT_FALSE(eval_ctx_->IsWallclockTimeGreaterThan( |
Alex Deymo | 41a75a7 | 2014-04-15 15:36:22 -0700 | [diff] [blame] | 418 | fake_clock_.GetWallclockTime() + TimeDelta::FromSeconds(1))); |
| 419 | |
Gilad Arnold | a65fced | 2014-07-23 09:01:31 -0700 | [diff] [blame] | 420 | // The "false" from IsWallclockTimeGreaterThan means that's not that timestamp |
| 421 | // yet, so this should schedule a callback for when that happens. |
Jakub Pawlowski | 7e1dcf7 | 2018-07-26 00:29:42 -0700 | [diff] [blame^] | 422 | EXPECT_TRUE(eval_ctx_->RunOnValueChangeOrTimeout(base::DoNothing())); |
Alex Deymo | 41a75a7 | 2014-04-15 15:36:22 -0700 | [diff] [blame] | 423 | } |
| 424 | |
Gilad Arnold | a65fced | 2014-07-23 09:01:31 -0700 | [diff] [blame] | 425 | TEST_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 Pawlowski | 7e1dcf7 | 2018-07-26 00:29:42 -0700 | [diff] [blame^] | 432 | EXPECT_TRUE(eval_ctx_->RunOnValueChangeOrTimeout(base::DoNothing())); |
Gilad Arnold | a65fced | 2014-07-23 09:01:31 -0700 | [diff] [blame] | 433 | } |
| 434 | |
| 435 | TEST_F(UmEvaluationContextTest, |
| 436 | IsWallclockTimeGreaterThanDoesntRecordPastTimestamps) { |
| 437 | // IsWallclockTimeGreaterThan() should ignore timestamps on the past for |
| 438 | // reevaluation. |
| 439 | EXPECT_TRUE(eval_ctx_->IsWallclockTimeGreaterThan( |
Alex Deymo | 41a75a7 | 2014-04-15 15:36:22 -0700 | [diff] [blame] | 440 | fake_clock_.GetWallclockTime() - TimeDelta::FromSeconds(20))); |
Gilad Arnold | a65fced | 2014-07-23 09:01:31 -0700 | [diff] [blame] | 441 | EXPECT_TRUE(eval_ctx_->IsWallclockTimeGreaterThan( |
Alex Deymo | 41a75a7 | 2014-04-15 15:36:22 -0700 | [diff] [blame] | 442 | fake_clock_.GetWallclockTime() - TimeDelta::FromSeconds(1))); |
| 443 | |
| 444 | // Callback should not be scheduled. |
Jakub Pawlowski | 7e1dcf7 | 2018-07-26 00:29:42 -0700 | [diff] [blame^] | 445 | EXPECT_FALSE(eval_ctx_->RunOnValueChangeOrTimeout(base::DoNothing())); |
Alex Deymo | 41a75a7 | 2014-04-15 15:36:22 -0700 | [diff] [blame] | 446 | } |
| 447 | |
Gilad Arnold | a65fced | 2014-07-23 09:01:31 -0700 | [diff] [blame] | 448 | TEST_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 Pawlowski | 7e1dcf7 | 2018-07-26 00:29:42 -0700 | [diff] [blame^] | 458 | EXPECT_FALSE(eval_ctx_->RunOnValueChangeOrTimeout(base::DoNothing())); |
Gilad Arnold | a65fced | 2014-07-23 09:01:31 -0700 | [diff] [blame] | 459 | } |
| 460 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 461 | TEST_F(UmEvaluationContextTest, DumpContext) { |
David Zeuthen | c149028 | 2014-04-29 16:25:03 -0700 | [diff] [blame] | 462 | // |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 Arnold | a65fced | 2014-07-23 09:01:31 -0700 | [diff] [blame] | 474 | // see UmEvaluationContextText::SetUp() where the values used for |
| 475 | // |evaluation_start_{monotonic,wallclock| are set. |
David Zeuthen | c149028 | 2014-04-29 16:25:03 -0700 | [diff] [blame] | 476 | EXPECT_EQ("{\n" |
Gilad Arnold | a65fced | 2014-07-23 09:01:31 -0700 | [diff] [blame] | 477 | " \"evaluation_start_monotonic\": \"4/22/2009 19:25:00 GMT\",\n" |
| 478 | " \"evaluation_start_wallclock\": \"3/2/2006 1:23:45 GMT\",\n" |
David Zeuthen | c149028 | 2014-04-29 16:25:03 -0700 | [diff] [blame] | 479 | " \"variables\": {\n" |
| 480 | " \"fail_var\": \"(no value)\",\n" |
| 481 | " \"fake_int\": \"42\",\n" |
| 482 | " \"fake_poll\": \"Hello \\\"world\\\"!\"\n" |
| 483 | " }\n" |
Gilad Arnold | 6e5ab5c | 2014-06-23 15:13:56 -0700 | [diff] [blame] | 484 | "}", |
David Zeuthen | c149028 | 2014-04-29 16:25:03 -0700 | [diff] [blame] | 485 | eval_ctx_->DumpContext()); |
| 486 | } |
| 487 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 488 | } // namespace chromeos_update_manager |