Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 1 | // 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 Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 5 | #include <string> |
| 6 | |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 7 | #include <base/bind.h> |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 8 | #include <gtest/gtest.h> |
| 9 | |
Alex Deymo | 41a75a7 | 2014-04-15 15:36:22 -0700 | [diff] [blame] | 10 | #include "update_engine/fake_clock.h" |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 11 | #include "update_engine/test_utils.h" |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 12 | #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 Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 17 | |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 18 | using base::Bind; |
Gilad Arnold | fb794f4 | 2014-07-01 15:36:31 -0700 | [diff] [blame] | 19 | using base::Closure; |
Alex Deymo | 41a75a7 | 2014-04-15 15:36:22 -0700 | [diff] [blame] | 20 | using base::Time; |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 21 | using base::TimeDelta; |
Alex Deymo | 41a75a7 | 2014-04-15 15:36:22 -0700 | [diff] [blame] | 22 | using chromeos_update_engine::FakeClock; |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 23 | using chromeos_update_engine::RunGMainLoopMaxIterations; |
| 24 | using chromeos_update_engine::RunGMainLoopUntil; |
Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 25 | using std::string; |
Alex Deymo | 41a75a7 | 2014-04-15 15:36:22 -0700 | [diff] [blame] | 26 | using testing::Return; |
| 27 | using testing::StrictMock; |
| 28 | using testing::_; |
Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 29 | |
Gilad Arnold | fb794f4 | 2014-07-01 15:36:31 -0700 | [diff] [blame] | 30 | namespace chromeos_update_manager { |
| 31 | |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 32 | namespace { |
| 33 | |
| 34 | void DoNothing() {} |
| 35 | |
| 36 | // Sets the value of the passed pointer to true. |
| 37 | void SetTrue(bool* value) { |
| 38 | *value = true; |
| 39 | } |
| 40 | |
| 41 | bool GetBoolean(bool* value) { |
| 42 | return *value; |
| 43 | } |
| 44 | |
Gilad Arnold | fb794f4 | 2014-07-01 15:36:31 -0700 | [diff] [blame] | 45 | template<typename T> |
| 46 | void ReadVar(scoped_refptr<EvaluationContext> ec, Variable<T>* var) { |
| 47 | ec->GetValue(var); |
| 48 | } |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 49 | |
Gilad Arnold | fb794f4 | 2014-07-01 15:36:31 -0700 | [diff] [blame] | 50 | // 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|. |
| 52 | void 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 Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 68 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 69 | class UmEvaluationContextTest : public ::testing::Test { |
Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 70 | protected: |
| 71 | virtual void SetUp() { |
Alex Deymo | 41a75a7 | 2014-04-15 15:36:22 -0700 | [diff] [blame] | 72 | // Set the clock to a fixed values. |
| 73 | fake_clock_.SetMonotonicTime(Time::FromInternalValue(12345678L)); |
David Zeuthen | c149028 | 2014-04-29 16:25:03 -0700 | [diff] [blame] | 74 | // Mar 2, 2006 1:23:45 UTC is 1141262625 since the Unix Epoch. |
| 75 | fake_clock_.SetWallclockTime(Time::FromTimeT(1141262625)); |
Gilad Arnold | f9f85d6 | 2014-06-19 18:07:01 -0700 | [diff] [blame] | 76 | eval_ctx_ = new EvaluationContext(&fake_clock_, default_timeout_, |
| 77 | default_timeout_); |
Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 78 | } |
| 79 | |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 80 | virtual void TearDown() { |
Gilad Arnold | fb794f4 | 2014-07-01 15:36:31 -0700 | [diff] [blame] | 81 | // Ensure that the evaluation context did not leak and is actually being |
| 82 | // destroyed. |
| 83 | if (eval_ctx_) { |
| 84 | base::WeakPtr<EvaluationContext> eval_ctx_weak_alias = |
| 85 | eval_ctx_->weak_ptr_factory_.GetWeakPtr(); |
| 86 | UMTEST_ASSERT_NOT_NULL(eval_ctx_weak_alias.get()); |
| 87 | eval_ctx_ = nullptr; |
| 88 | UMTEST_EXPECT_NULL(eval_ctx_weak_alias.get()) |
| 89 | << "The evaluation context was not destroyed! This is likely a bug " |
| 90 | "in how the test was written, look for leaking handles to the EC, " |
| 91 | "possibly through closure objects."; |
| 92 | } |
| 93 | |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 94 | // Check that the evaluation context removed all the observers. |
| 95 | EXPECT_TRUE(fake_int_var_.observer_list_.empty()); |
| 96 | EXPECT_TRUE(fake_async_var_.observer_list_.empty()); |
| 97 | EXPECT_TRUE(fake_const_var_.observer_list_.empty()); |
| 98 | EXPECT_TRUE(fake_poll_var_.observer_list_.empty()); |
| 99 | } |
| 100 | |
Alex Deymo | 41a75a7 | 2014-04-15 15:36:22 -0700 | [diff] [blame] | 101 | base::TimeDelta default_timeout_ = base::TimeDelta::FromSeconds(5); |
| 102 | |
| 103 | FakeClock fake_clock_; |
Alex Deymo | 7b948f0 | 2014-03-10 17:01:10 -0700 | [diff] [blame] | 104 | scoped_refptr<EvaluationContext> eval_ctx_; |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 105 | |
| 106 | // FakeVariables used for testing the EvaluationContext. These are required |
| 107 | // here to prevent them from going away *before* the EvaluationContext under |
| 108 | // test does, which keeps a reference to them. |
David Zeuthen | c149028 | 2014-04-29 16:25:03 -0700 | [diff] [blame] | 109 | FakeVariable<bool> fail_var_ = {"fail_var", kVariableModePoll}; |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 110 | FakeVariable<int> fake_int_var_ = {"fake_int", kVariableModePoll}; |
| 111 | FakeVariable<string> fake_async_var_ = {"fake_async", kVariableModeAsync}; |
| 112 | FakeVariable<string> fake_const_var_ = {"fake_const", kVariableModeConst}; |
| 113 | FakeVariable<string> fake_poll_var_ = {"fake_poll", |
| 114 | TimeDelta::FromSeconds(1)}; |
Gilad Arnold | fb794f4 | 2014-07-01 15:36:31 -0700 | [diff] [blame] | 115 | StrictMock<MockVariable<string>> mock_var_async_ { |
| 116 | "mock_var_async", kVariableModeAsync}; |
| 117 | StrictMock<MockVariable<string>> mock_var_poll_ { |
| 118 | "mock_var_poll", kVariableModePoll}; |
Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 119 | }; |
| 120 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 121 | TEST_F(UmEvaluationContextTest, GetValueFails) { |
Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 122 | // FakeVariable is initialized as returning NULL. |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 123 | UMTEST_EXPECT_NULL(eval_ctx_->GetValue(&fake_int_var_)); |
Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 124 | } |
| 125 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 126 | TEST_F(UmEvaluationContextTest, GetValueFailsWithInvalidVar) { |
| 127 | UMTEST_EXPECT_NULL(eval_ctx_->GetValue( |
Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 128 | reinterpret_cast<Variable<int>*>(NULL))); |
| 129 | } |
| 130 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 131 | TEST_F(UmEvaluationContextTest, GetValueReturns) { |
Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 132 | const int* p_fake_int; |
| 133 | |
| 134 | fake_int_var_.reset(new int(42)); |
| 135 | p_fake_int = eval_ctx_->GetValue(&fake_int_var_); |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 136 | UMTEST_ASSERT_NOT_NULL(p_fake_int); |
Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 137 | EXPECT_EQ(42, *p_fake_int); |
| 138 | } |
| 139 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 140 | TEST_F(UmEvaluationContextTest, GetValueCached) { |
Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 141 | const int* p_fake_int; |
| 142 | |
| 143 | fake_int_var_.reset(new int(42)); |
| 144 | p_fake_int = eval_ctx_->GetValue(&fake_int_var_); |
| 145 | |
| 146 | // Check that if the variable changes, the EvaluationContext keeps returning |
| 147 | // the cached value. |
| 148 | fake_int_var_.reset(new int(5)); |
| 149 | |
| 150 | p_fake_int = eval_ctx_->GetValue(&fake_int_var_); |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 151 | UMTEST_ASSERT_NOT_NULL(p_fake_int); |
Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 152 | EXPECT_EQ(42, *p_fake_int); |
| 153 | } |
| 154 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 155 | TEST_F(UmEvaluationContextTest, GetValueCachesNull) { |
Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 156 | const int* p_fake_int = eval_ctx_->GetValue(&fake_int_var_); |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 157 | UMTEST_EXPECT_NULL(p_fake_int); |
Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 158 | |
| 159 | fake_int_var_.reset(new int(42)); |
Alex Deymo | cc0e5cf | 2014-04-23 20:20:11 -0700 | [diff] [blame] | 160 | // A second attempt to read the variable should not work because this |
| 161 | // EvaluationContext already got a NULL value. |
Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 162 | p_fake_int = eval_ctx_->GetValue(&fake_int_var_); |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 163 | UMTEST_EXPECT_NULL(p_fake_int); |
Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 164 | } |
| 165 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 166 | TEST_F(UmEvaluationContextTest, GetValueMixedTypes) { |
Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 167 | const int* p_fake_int; |
| 168 | const string* p_fake_string; |
| 169 | |
| 170 | fake_int_var_.reset(new int(42)); |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 171 | fake_poll_var_.reset(new string("Hello world!")); |
Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 172 | // Check that the EvaluationContext can handle multiple Variable types. This |
| 173 | // is mostly a compile-time check due to the template nature of this method. |
| 174 | p_fake_int = eval_ctx_->GetValue(&fake_int_var_); |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 175 | p_fake_string = eval_ctx_->GetValue(&fake_poll_var_); |
Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 176 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 177 | UMTEST_ASSERT_NOT_NULL(p_fake_int); |
Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 178 | EXPECT_EQ(42, *p_fake_int); |
| 179 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 180 | UMTEST_ASSERT_NOT_NULL(p_fake_string); |
Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 181 | EXPECT_EQ("Hello world!", *p_fake_string); |
| 182 | } |
| 183 | |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 184 | // 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] | 185 | TEST_F(UmEvaluationContextTest, RunOnValueChangeOrTimeoutWithoutVariables) { |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 186 | fake_const_var_.reset(new string("Hello world!")); |
| 187 | EXPECT_EQ(*eval_ctx_->GetValue(&fake_const_var_), "Hello world!"); |
| 188 | |
| 189 | EXPECT_FALSE(eval_ctx_->RunOnValueChangeOrTimeout(Bind(&DoNothing))); |
| 190 | } |
| 191 | |
Gilad Arnold | fb794f4 | 2014-07-01 15:36:31 -0700 | [diff] [blame] | 192 | // Test that reevaluation occurs when an async variable it depends on changes. |
Gilad Arnold | f9f85d6 | 2014-06-19 18:07:01 -0700 | [diff] [blame] | 193 | TEST_F(UmEvaluationContextTest, RunOnValueChangeOrTimeoutWithVariables) { |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 194 | fake_async_var_.reset(new string("Async value")); |
| 195 | eval_ctx_->GetValue(&fake_async_var_); |
| 196 | |
| 197 | bool value = false; |
| 198 | EXPECT_TRUE(eval_ctx_->RunOnValueChangeOrTimeout(Bind(&SetTrue, &value))); |
| 199 | // Check that the scheduled callback isn't run until we signal a ValueChaged. |
| 200 | RunGMainLoopMaxIterations(100); |
| 201 | EXPECT_FALSE(value); |
| 202 | |
| 203 | fake_async_var_.NotifyValueChanged(); |
| 204 | EXPECT_FALSE(value); |
| 205 | // Ensure that the scheduled callback isn't run until we are back on the main |
| 206 | // loop. |
| 207 | RunGMainLoopMaxIterations(100); |
| 208 | EXPECT_TRUE(value); |
| 209 | } |
| 210 | |
| 211 | // 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] | 212 | TEST_F(UmEvaluationContextTest, RunOnValueChangeOrTimeoutCalledTwice) { |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 213 | fake_async_var_.reset(new string("Async value")); |
| 214 | eval_ctx_->GetValue(&fake_async_var_); |
| 215 | |
| 216 | bool value = false; |
| 217 | EXPECT_TRUE(eval_ctx_->RunOnValueChangeOrTimeout(Bind(&SetTrue, &value))); |
| 218 | EXPECT_FALSE(eval_ctx_->RunOnValueChangeOrTimeout(Bind(&SetTrue, &value))); |
| 219 | |
| 220 | // The scheduled event should still work. |
| 221 | fake_async_var_.NotifyValueChanged(); |
| 222 | RunGMainLoopMaxIterations(100); |
| 223 | EXPECT_TRUE(value); |
| 224 | } |
| 225 | |
Gilad Arnold | fb794f4 | 2014-07-01 15:36:31 -0700 | [diff] [blame] | 226 | // Test that reevaluation occurs when a polling timeout fires. |
Gilad Arnold | f9f85d6 | 2014-06-19 18:07:01 -0700 | [diff] [blame] | 227 | TEST_F(UmEvaluationContextTest, RunOnValueChangeOrTimeoutRunsFromTimeout) { |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 228 | fake_poll_var_.reset(new string("Polled value")); |
| 229 | eval_ctx_->GetValue(&fake_poll_var_); |
| 230 | |
| 231 | bool value = false; |
| 232 | EXPECT_TRUE(eval_ctx_->RunOnValueChangeOrTimeout(Bind(&SetTrue, &value))); |
| 233 | // Check that the scheduled callback isn't run until the timeout occurs. |
| 234 | RunGMainLoopMaxIterations(10); |
| 235 | EXPECT_FALSE(value); |
| 236 | RunGMainLoopUntil(10000, Bind(&GetBoolean, &value)); |
| 237 | EXPECT_TRUE(value); |
| 238 | } |
| 239 | |
Gilad Arnold | f9f85d6 | 2014-06-19 18:07:01 -0700 | [diff] [blame] | 240 | // Test that callback is called when evaluation context expires, and that it |
| 241 | // cannot be used again. |
| 242 | TEST_F(UmEvaluationContextTest, RunOnValueChangeOrTimeoutExpires) { |
| 243 | fake_async_var_.reset(new string("Async value")); |
| 244 | eval_ctx_->GetValue(&fake_async_var_); |
| 245 | |
| 246 | bool value = false; |
| 247 | EXPECT_TRUE(eval_ctx_->RunOnValueChangeOrTimeout(Bind(&SetTrue, &value))); |
| 248 | // Check that the scheduled callback isn't run until the timeout occurs. |
| 249 | RunGMainLoopMaxIterations(10); |
| 250 | EXPECT_FALSE(value); |
| 251 | RunGMainLoopUntil(10000, Bind(&GetBoolean, &value)); |
| 252 | EXPECT_TRUE(value); |
| 253 | |
| 254 | // Ensure that we cannot reschedule an evaluation. |
| 255 | EXPECT_FALSE(eval_ctx_->RunOnValueChangeOrTimeout(Bind(&DoNothing))); |
| 256 | } |
| 257 | |
| 258 | // Test that we clear the events when destroying the EvaluationContext. |
| 259 | TEST_F(UmEvaluationContextTest, RemoveObserversAndTimeoutTest) { |
| 260 | fake_async_var_.reset(new string("Async value")); |
| 261 | eval_ctx_->GetValue(&fake_async_var_); |
| 262 | |
| 263 | bool value = false; |
| 264 | EXPECT_TRUE(eval_ctx_->RunOnValueChangeOrTimeout(Bind(&SetTrue, &value))); |
| 265 | eval_ctx_ = nullptr; |
| 266 | |
| 267 | // This should not trigger the callback since the EvaluationContext waiting |
| 268 | // for it is gone, and it should have remove all its observers. |
| 269 | fake_async_var_.NotifyValueChanged(); |
| 270 | RunGMainLoopMaxIterations(100); |
| 271 | EXPECT_FALSE(value); |
| 272 | } |
| 273 | |
Gilad Arnold | fb794f4 | 2014-07-01 15:36:31 -0700 | [diff] [blame] | 274 | // Scheduling two reevaluations from the callback should succeed. |
| 275 | TEST_F(UmEvaluationContextTest, |
| 276 | RunOnValueChangeOrTimeoutReevaluatesRepeatedly) { |
| 277 | fake_poll_var_.reset(new string("Polled value")); |
| 278 | Closure evaluation = Bind(ReadVar<string>, eval_ctx_, &fake_poll_var_); |
| 279 | int num_reevaluations = 2; |
| 280 | bool done = false; |
| 281 | |
| 282 | // Run the evaluation once. |
| 283 | evaluation.Run(); |
| 284 | |
| 285 | // Schedule repeated reevaluations. |
| 286 | Closure closure = Bind(EvaluateRepeatedly, evaluation, eval_ctx_, |
| 287 | &num_reevaluations, &done); |
| 288 | ASSERT_TRUE(eval_ctx_->RunOnValueChangeOrTimeout(closure)); |
| 289 | RunGMainLoopUntil(10000, Bind(&GetBoolean, &done)); |
| 290 | EXPECT_EQ(0, num_reevaluations); |
| 291 | } |
| 292 | |
Alex Deymo | db79953 | 2014-03-21 13:00:00 -0700 | [diff] [blame] | 293 | // Test that we can delete the EvaluationContext while having pending events. |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 294 | TEST_F(UmEvaluationContextTest, ObjectDeletedWithPendingEventsTest) { |
Alex Deymo | db79953 | 2014-03-21 13:00:00 -0700 | [diff] [blame] | 295 | fake_async_var_.reset(new string("Async value")); |
| 296 | fake_poll_var_.reset(new string("Polled value")); |
| 297 | eval_ctx_->GetValue(&fake_async_var_); |
| 298 | eval_ctx_->GetValue(&fake_poll_var_); |
| 299 | EXPECT_TRUE(eval_ctx_->RunOnValueChangeOrTimeout(Bind(&DoNothing))); |
| 300 | // TearDown() checks for leaked observers on this async_variable, which means |
| 301 | // that our object is still alive after removing its reference. |
| 302 | } |
| 303 | |
| 304 | // Test that timed events fired after removal of the EvaluationContext don't |
| 305 | // crash. |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 306 | TEST_F(UmEvaluationContextTest, TimeoutEventAfterDeleteTest) { |
Alex Deymo | db79953 | 2014-03-21 13:00:00 -0700 | [diff] [blame] | 307 | FakeVariable<string> fake_short_poll_var = {"fake_short_poll", TimeDelta()}; |
| 308 | fake_short_poll_var.reset(new string("Polled value")); |
| 309 | eval_ctx_->GetValue(&fake_short_poll_var); |
| 310 | bool value = false; |
| 311 | EXPECT_TRUE(eval_ctx_->RunOnValueChangeOrTimeout(Bind(&SetTrue, &value))); |
| 312 | // Remove the last reference to the EvaluationContext and run the loop for |
| 313 | // 1 second to give time to the main loop to trigger the timeout Event (of 0 |
| 314 | // seconds). Our callback should not be called because the EvaluationContext |
| 315 | // was removed before the timeout event is attended. |
Gilad Arnold | f9f85d6 | 2014-06-19 18:07:01 -0700 | [diff] [blame] | 316 | eval_ctx_ = nullptr; |
Alex Deymo | db79953 | 2014-03-21 13:00:00 -0700 | [diff] [blame] | 317 | RunGMainLoopUntil(1000, Bind(&GetBoolean, &value)); |
| 318 | EXPECT_FALSE(value); |
| 319 | } |
| 320 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 321 | TEST_F(UmEvaluationContextTest, DefaultTimeout) { |
Gilad Arnold | f9f85d6 | 2014-06-19 18:07:01 -0700 | [diff] [blame] | 322 | // Test that the evaluation timeout calculation uses the default timeout on |
| 323 | // setup. |
Alex Deymo | 41a75a7 | 2014-04-15 15:36:22 -0700 | [diff] [blame] | 324 | EXPECT_CALL(mock_var_async_, GetValue(default_timeout_, _)) |
| 325 | .WillOnce(Return(nullptr)); |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 326 | UMTEST_EXPECT_NULL(eval_ctx_->GetValue(&mock_var_async_)); |
Alex Deymo | 41a75a7 | 2014-04-15 15:36:22 -0700 | [diff] [blame] | 327 | } |
| 328 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 329 | TEST_F(UmEvaluationContextTest, TimeoutUpdatesWithMonotonicTime) { |
Alex Deymo | 41a75a7 | 2014-04-15 15:36:22 -0700 | [diff] [blame] | 330 | fake_clock_.SetMonotonicTime( |
| 331 | fake_clock_.GetMonotonicTime() + TimeDelta::FromSeconds(1)); |
| 332 | |
| 333 | TimeDelta timeout = default_timeout_ - TimeDelta::FromSeconds(1); |
| 334 | |
| 335 | EXPECT_CALL(mock_var_async_, GetValue(timeout, _)) |
| 336 | .WillOnce(Return(nullptr)); |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 337 | UMTEST_EXPECT_NULL(eval_ctx_->GetValue(&mock_var_async_)); |
Alex Deymo | 41a75a7 | 2014-04-15 15:36:22 -0700 | [diff] [blame] | 338 | } |
| 339 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 340 | TEST_F(UmEvaluationContextTest, ResetEvaluationResetsTimes) { |
Alex Deymo | 41a75a7 | 2014-04-15 15:36:22 -0700 | [diff] [blame] | 341 | base::Time cur_time = fake_clock_.GetWallclockTime(); |
| 342 | // Advance the time on the clock but don't call ResetEvaluation yet. |
| 343 | fake_clock_.SetWallclockTime(cur_time + TimeDelta::FromSeconds(4)); |
| 344 | |
| 345 | EXPECT_TRUE(eval_ctx_->IsTimeGreaterThan(cur_time - |
| 346 | TimeDelta::FromSeconds(1))); |
| 347 | EXPECT_FALSE(eval_ctx_->IsTimeGreaterThan(cur_time)); |
| 348 | EXPECT_FALSE(eval_ctx_->IsTimeGreaterThan(cur_time + |
| 349 | TimeDelta::FromSeconds(1))); |
| 350 | // Call ResetEvaluation now, which should use the new evaluation time. |
| 351 | eval_ctx_->ResetEvaluation(); |
| 352 | |
| 353 | cur_time = fake_clock_.GetWallclockTime(); |
| 354 | EXPECT_TRUE(eval_ctx_->IsTimeGreaterThan(cur_time - |
| 355 | TimeDelta::FromSeconds(1))); |
| 356 | EXPECT_FALSE(eval_ctx_->IsTimeGreaterThan(cur_time)); |
| 357 | EXPECT_FALSE(eval_ctx_->IsTimeGreaterThan(cur_time + |
| 358 | TimeDelta::FromSeconds(1))); |
| 359 | } |
| 360 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 361 | TEST_F(UmEvaluationContextTest, IsTimeGreaterThanSignalsTriggerReevaluation) { |
Alex Deymo | 41a75a7 | 2014-04-15 15:36:22 -0700 | [diff] [blame] | 362 | EXPECT_FALSE(eval_ctx_->IsTimeGreaterThan( |
| 363 | fake_clock_.GetWallclockTime() + TimeDelta::FromSeconds(1))); |
| 364 | |
| 365 | // The "false" from IsTimeGreaterThan means that's not that timestamp yet, so |
| 366 | // this should schedule a callback for when that happens. |
| 367 | EXPECT_TRUE(eval_ctx_->RunOnValueChangeOrTimeout(Bind(&DoNothing))); |
| 368 | } |
| 369 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 370 | TEST_F(UmEvaluationContextTest, IsTimeGreaterThanDoesntRecordPastTimestamps) { |
Alex Deymo | 41a75a7 | 2014-04-15 15:36:22 -0700 | [diff] [blame] | 371 | // IsTimeGreaterThan() should ignore timestamps on the past for reevaluation. |
| 372 | EXPECT_TRUE(eval_ctx_->IsTimeGreaterThan( |
| 373 | fake_clock_.GetWallclockTime() - TimeDelta::FromSeconds(20))); |
| 374 | EXPECT_TRUE(eval_ctx_->IsTimeGreaterThan( |
| 375 | fake_clock_.GetWallclockTime() - TimeDelta::FromSeconds(1))); |
| 376 | |
| 377 | // Callback should not be scheduled. |
| 378 | EXPECT_FALSE(eval_ctx_->RunOnValueChangeOrTimeout(Bind(&DoNothing))); |
| 379 | } |
| 380 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 381 | TEST_F(UmEvaluationContextTest, DumpContext) { |
David Zeuthen | c149028 | 2014-04-29 16:25:03 -0700 | [diff] [blame] | 382 | // |fail_var_| yield "(no value)" since it is unset. |
| 383 | eval_ctx_->GetValue(&fail_var_); |
| 384 | |
| 385 | // Check that this is included. |
| 386 | fake_int_var_.reset(new int(42)); |
| 387 | eval_ctx_->GetValue(&fake_int_var_); |
| 388 | |
| 389 | // Check that double-quotes are escaped properly. |
| 390 | fake_poll_var_.reset(new string("Hello \"world\"!")); |
| 391 | eval_ctx_->GetValue(&fake_poll_var_); |
| 392 | |
| 393 | // Note that the variables are printed in alphabetical order. Also |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 394 | // see UmEvaluationContextText::SetUp() where the value used for |
David Zeuthen | c149028 | 2014-04-29 16:25:03 -0700 | [diff] [blame] | 395 | // |evaluation_start| is set. |
| 396 | EXPECT_EQ("{\n" |
| 397 | " \"evaluation_start\": \"3/2/2006 1:23:45 GMT\",\n" |
| 398 | " \"variables\": {\n" |
| 399 | " \"fail_var\": \"(no value)\",\n" |
| 400 | " \"fake_int\": \"42\",\n" |
| 401 | " \"fake_poll\": \"Hello \\\"world\\\"!\"\n" |
| 402 | " }\n" |
Gilad Arnold | 6e5ab5c | 2014-06-23 15:13:56 -0700 | [diff] [blame^] | 403 | "}", |
David Zeuthen | c149028 | 2014-04-29 16:25:03 -0700 | [diff] [blame] | 404 | eval_ctx_->DumpContext()); |
| 405 | } |
| 406 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 407 | } // namespace chromeos_update_manager |