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