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 | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 11 | #include "update_engine/policy_manager/evaluation_context.h" |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 12 | #include "update_engine/policy_manager/fake_variable.h" |
Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 13 | #include "update_engine/policy_manager/generic_variables.h" |
Alex Deymo | 41a75a7 | 2014-04-15 15:36:22 -0700 | [diff] [blame] | 14 | #include "update_engine/policy_manager/mock_variable.h" |
Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 15 | #include "update_engine/policy_manager/pmtest_utils.h" |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 16 | #include "update_engine/test_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; |
Alex Deymo | 41a75a7 | 2014-04-15 15:36:22 -0700 | [diff] [blame] | 19 | using base::Time; |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 20 | using base::TimeDelta; |
Alex Deymo | 41a75a7 | 2014-04-15 15:36:22 -0700 | [diff] [blame] | 21 | using chromeos_update_engine::FakeClock; |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 22 | using chromeos_update_engine::RunGMainLoopMaxIterations; |
| 23 | using chromeos_update_engine::RunGMainLoopUntil; |
Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 24 | using std::string; |
Alex Deymo | 41a75a7 | 2014-04-15 15:36:22 -0700 | [diff] [blame] | 25 | using testing::Return; |
| 26 | using testing::StrictMock; |
| 27 | using testing::_; |
Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 28 | |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 29 | namespace { |
| 30 | |
| 31 | void DoNothing() {} |
| 32 | |
| 33 | // Sets the value of the passed pointer to true. |
| 34 | void SetTrue(bool* value) { |
| 35 | *value = true; |
| 36 | } |
| 37 | |
| 38 | bool GetBoolean(bool* value) { |
| 39 | return *value; |
| 40 | } |
| 41 | |
| 42 | } // namespace |
| 43 | |
Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 44 | namespace chromeos_policy_manager { |
| 45 | |
| 46 | class PmEvaluationContextTest : public ::testing::Test { |
Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 47 | protected: |
| 48 | virtual void SetUp() { |
Alex Deymo | 41a75a7 | 2014-04-15 15:36:22 -0700 | [diff] [blame] | 49 | // Set the clock to a fixed values. |
| 50 | fake_clock_.SetMonotonicTime(Time::FromInternalValue(12345678L)); |
| 51 | fake_clock_.SetWallclockTime(Time::FromInternalValue(12345678901234L)); |
| 52 | eval_ctx_ = new EvaluationContext(&fake_clock_); |
Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 53 | } |
| 54 | |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 55 | virtual void TearDown() { |
| 56 | eval_ctx_ = NULL; |
| 57 | // Check that the evaluation context removed all the observers. |
| 58 | EXPECT_TRUE(fake_int_var_.observer_list_.empty()); |
| 59 | EXPECT_TRUE(fake_async_var_.observer_list_.empty()); |
| 60 | EXPECT_TRUE(fake_const_var_.observer_list_.empty()); |
| 61 | EXPECT_TRUE(fake_poll_var_.observer_list_.empty()); |
| 62 | } |
| 63 | |
Alex Deymo | 41a75a7 | 2014-04-15 15:36:22 -0700 | [diff] [blame] | 64 | // TODO(deymo): Update the default timeout to the one passed on construction. |
| 65 | // See crbug.com/363790 |
| 66 | base::TimeDelta default_timeout_ = base::TimeDelta::FromSeconds(5); |
| 67 | |
| 68 | FakeClock fake_clock_; |
Alex Deymo | 7b948f0 | 2014-03-10 17:01:10 -0700 | [diff] [blame] | 69 | scoped_refptr<EvaluationContext> eval_ctx_; |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 70 | |
| 71 | // FakeVariables used for testing the EvaluationContext. These are required |
| 72 | // here to prevent them from going away *before* the EvaluationContext under |
| 73 | // test does, which keeps a reference to them. |
| 74 | FakeVariable<int> fake_int_var_ = {"fake_int", kVariableModePoll}; |
| 75 | FakeVariable<string> fake_async_var_ = {"fake_async", kVariableModeAsync}; |
| 76 | FakeVariable<string> fake_const_var_ = {"fake_const", kVariableModeConst}; |
| 77 | FakeVariable<string> fake_poll_var_ = {"fake_poll", |
| 78 | TimeDelta::FromSeconds(1)}; |
Alex Deymo | 41a75a7 | 2014-04-15 15:36:22 -0700 | [diff] [blame] | 79 | StrictMock<MockVariable<string>> mock_var_async_{"mock_var_async", |
| 80 | kVariableModeAsync}; |
| 81 | StrictMock<MockVariable<string>> mock_var_poll_{"mock_var_poll", |
| 82 | kVariableModePoll}; |
Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 83 | }; |
| 84 | |
| 85 | TEST_F(PmEvaluationContextTest, GetValueFails) { |
| 86 | // FakeVariable is initialized as returning NULL. |
| 87 | PMTEST_EXPECT_NULL(eval_ctx_->GetValue(&fake_int_var_)); |
| 88 | } |
| 89 | |
| 90 | TEST_F(PmEvaluationContextTest, GetValueFailsWithInvalidVar) { |
| 91 | PMTEST_EXPECT_NULL(eval_ctx_->GetValue( |
| 92 | reinterpret_cast<Variable<int>*>(NULL))); |
| 93 | } |
| 94 | |
| 95 | TEST_F(PmEvaluationContextTest, GetValueReturns) { |
| 96 | const int* p_fake_int; |
| 97 | |
| 98 | fake_int_var_.reset(new int(42)); |
| 99 | p_fake_int = eval_ctx_->GetValue(&fake_int_var_); |
| 100 | PMTEST_ASSERT_NOT_NULL(p_fake_int); |
| 101 | EXPECT_EQ(42, *p_fake_int); |
| 102 | } |
| 103 | |
| 104 | TEST_F(PmEvaluationContextTest, GetValueCached) { |
| 105 | const int* p_fake_int; |
| 106 | |
| 107 | fake_int_var_.reset(new int(42)); |
| 108 | p_fake_int = eval_ctx_->GetValue(&fake_int_var_); |
| 109 | |
| 110 | // Check that if the variable changes, the EvaluationContext keeps returning |
| 111 | // the cached value. |
| 112 | fake_int_var_.reset(new int(5)); |
| 113 | |
| 114 | p_fake_int = eval_ctx_->GetValue(&fake_int_var_); |
| 115 | PMTEST_ASSERT_NOT_NULL(p_fake_int); |
| 116 | EXPECT_EQ(42, *p_fake_int); |
| 117 | } |
| 118 | |
Alex Deymo | cc0e5cf | 2014-04-23 20:20:11 -0700 | [diff] [blame] | 119 | TEST_F(PmEvaluationContextTest, GetValueCachesNull) { |
Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 120 | const int* p_fake_int = eval_ctx_->GetValue(&fake_int_var_); |
| 121 | PMTEST_EXPECT_NULL(p_fake_int); |
| 122 | |
| 123 | fake_int_var_.reset(new int(42)); |
Alex Deymo | cc0e5cf | 2014-04-23 20:20:11 -0700 | [diff] [blame] | 124 | // A second attempt to read the variable should not work because this |
| 125 | // EvaluationContext already got a NULL value. |
Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 126 | p_fake_int = eval_ctx_->GetValue(&fake_int_var_); |
Alex Deymo | cc0e5cf | 2014-04-23 20:20:11 -0700 | [diff] [blame] | 127 | PMTEST_EXPECT_NULL(p_fake_int); |
Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | TEST_F(PmEvaluationContextTest, GetValueMixedTypes) { |
Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 131 | const int* p_fake_int; |
| 132 | const string* p_fake_string; |
| 133 | |
| 134 | fake_int_var_.reset(new int(42)); |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 135 | fake_poll_var_.reset(new string("Hello world!")); |
Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 136 | // Check that the EvaluationContext can handle multiple Variable types. This |
| 137 | // is mostly a compile-time check due to the template nature of this method. |
| 138 | p_fake_int = eval_ctx_->GetValue(&fake_int_var_); |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 139 | p_fake_string = eval_ctx_->GetValue(&fake_poll_var_); |
Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 140 | |
| 141 | PMTEST_ASSERT_NOT_NULL(p_fake_int); |
| 142 | EXPECT_EQ(42, *p_fake_int); |
| 143 | |
| 144 | PMTEST_ASSERT_NOT_NULL(p_fake_string); |
| 145 | EXPECT_EQ("Hello world!", *p_fake_string); |
| 146 | } |
| 147 | |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 148 | // Test that we don't schedule an event if there's no variable to wait for. |
| 149 | TEST_F(PmEvaluationContextTest, RunOnValueChangeOrTimeoutWithoutVariablesTest) { |
| 150 | fake_const_var_.reset(new string("Hello world!")); |
| 151 | EXPECT_EQ(*eval_ctx_->GetValue(&fake_const_var_), "Hello world!"); |
| 152 | |
| 153 | EXPECT_FALSE(eval_ctx_->RunOnValueChangeOrTimeout(Bind(&DoNothing))); |
| 154 | } |
| 155 | |
| 156 | // Test that we don't schedule an event if there's no variable to wait for. |
| 157 | TEST_F(PmEvaluationContextTest, RunOnValueChangeOrTimeoutWithVariablesTest) { |
| 158 | fake_async_var_.reset(new string("Async value")); |
| 159 | eval_ctx_->GetValue(&fake_async_var_); |
| 160 | |
| 161 | bool value = false; |
| 162 | EXPECT_TRUE(eval_ctx_->RunOnValueChangeOrTimeout(Bind(&SetTrue, &value))); |
| 163 | // Check that the scheduled callback isn't run until we signal a ValueChaged. |
| 164 | RunGMainLoopMaxIterations(100); |
| 165 | EXPECT_FALSE(value); |
| 166 | |
| 167 | fake_async_var_.NotifyValueChanged(); |
| 168 | EXPECT_FALSE(value); |
| 169 | // Ensure that the scheduled callback isn't run until we are back on the main |
| 170 | // loop. |
| 171 | RunGMainLoopMaxIterations(100); |
| 172 | EXPECT_TRUE(value); |
| 173 | } |
| 174 | |
| 175 | // Test that we don't re-schedule the events if we are attending one. |
| 176 | TEST_F(PmEvaluationContextTest, RunOnValueChangeOrTimeoutCalledTwiceTest) { |
| 177 | fake_async_var_.reset(new string("Async value")); |
| 178 | eval_ctx_->GetValue(&fake_async_var_); |
| 179 | |
| 180 | bool value = false; |
| 181 | EXPECT_TRUE(eval_ctx_->RunOnValueChangeOrTimeout(Bind(&SetTrue, &value))); |
| 182 | EXPECT_FALSE(eval_ctx_->RunOnValueChangeOrTimeout(Bind(&SetTrue, &value))); |
| 183 | |
| 184 | // The scheduled event should still work. |
| 185 | fake_async_var_.NotifyValueChanged(); |
| 186 | RunGMainLoopMaxIterations(100); |
| 187 | EXPECT_TRUE(value); |
| 188 | } |
| 189 | |
| 190 | // Test that we clear the events when destroying the EvaluationContext. |
| 191 | TEST_F(PmEvaluationContextTest, RemoveObserversAndTimeoutTest) { |
| 192 | fake_async_var_.reset(new string("Async value")); |
| 193 | eval_ctx_->GetValue(&fake_async_var_); |
| 194 | |
| 195 | bool value = false; |
| 196 | EXPECT_TRUE(eval_ctx_->RunOnValueChangeOrTimeout(Bind(&SetTrue, &value))); |
| 197 | eval_ctx_ = NULL; |
| 198 | |
| 199 | // This should not trigger the callback since the EvaluationContext waiting |
| 200 | // for it is gone, and it should have remove all its observers. |
| 201 | fake_async_var_.NotifyValueChanged(); |
| 202 | RunGMainLoopMaxIterations(100); |
| 203 | EXPECT_FALSE(value); |
| 204 | } |
| 205 | |
| 206 | // Test that we don't schedule an event if there's no variable to wait for. |
| 207 | TEST_F(PmEvaluationContextTest, RunOnValueChangeOrTimeoutRunsFromTimeoutTest) { |
| 208 | fake_poll_var_.reset(new string("Polled value")); |
| 209 | eval_ctx_->GetValue(&fake_poll_var_); |
| 210 | |
| 211 | bool value = false; |
| 212 | EXPECT_TRUE(eval_ctx_->RunOnValueChangeOrTimeout(Bind(&SetTrue, &value))); |
| 213 | // Check that the scheduled callback isn't run until the timeout occurs. |
| 214 | RunGMainLoopMaxIterations(10); |
| 215 | EXPECT_FALSE(value); |
| 216 | RunGMainLoopUntil(10000, Bind(&GetBoolean, &value)); |
| 217 | EXPECT_TRUE(value); |
| 218 | } |
| 219 | |
Alex Deymo | db79953 | 2014-03-21 13:00:00 -0700 | [diff] [blame] | 220 | // Test that we can delete the EvaluationContext while having pending events. |
| 221 | TEST_F(PmEvaluationContextTest, ObjectDeletedWithPendingEventsTest) { |
| 222 | fake_async_var_.reset(new string("Async value")); |
| 223 | fake_poll_var_.reset(new string("Polled value")); |
| 224 | eval_ctx_->GetValue(&fake_async_var_); |
| 225 | eval_ctx_->GetValue(&fake_poll_var_); |
| 226 | EXPECT_TRUE(eval_ctx_->RunOnValueChangeOrTimeout(Bind(&DoNothing))); |
| 227 | // TearDown() checks for leaked observers on this async_variable, which means |
| 228 | // that our object is still alive after removing its reference. |
| 229 | } |
| 230 | |
| 231 | // Test that timed events fired after removal of the EvaluationContext don't |
| 232 | // crash. |
| 233 | TEST_F(PmEvaluationContextTest, TimeoutEventAfterDeleteTest) { |
| 234 | FakeVariable<string> fake_short_poll_var = {"fake_short_poll", TimeDelta()}; |
| 235 | fake_short_poll_var.reset(new string("Polled value")); |
| 236 | eval_ctx_->GetValue(&fake_short_poll_var); |
| 237 | bool value = false; |
| 238 | EXPECT_TRUE(eval_ctx_->RunOnValueChangeOrTimeout(Bind(&SetTrue, &value))); |
| 239 | // Remove the last reference to the EvaluationContext and run the loop for |
| 240 | // 1 second to give time to the main loop to trigger the timeout Event (of 0 |
| 241 | // seconds). Our callback should not be called because the EvaluationContext |
| 242 | // was removed before the timeout event is attended. |
| 243 | eval_ctx_ = NULL; |
| 244 | RunGMainLoopUntil(1000, Bind(&GetBoolean, &value)); |
| 245 | EXPECT_FALSE(value); |
| 246 | } |
| 247 | |
Alex Deymo | 41a75a7 | 2014-04-15 15:36:22 -0700 | [diff] [blame] | 248 | TEST_F(PmEvaluationContextTest, DefaultTimeout) { |
| 249 | // Test that the RemainingTime() uses the default timeout on setup. |
| 250 | EXPECT_CALL(mock_var_async_, GetValue(default_timeout_, _)) |
| 251 | .WillOnce(Return(nullptr)); |
| 252 | PMTEST_EXPECT_NULL(eval_ctx_->GetValue(&mock_var_async_)); |
| 253 | } |
| 254 | |
| 255 | TEST_F(PmEvaluationContextTest, TimeoutUpdatesWithMonotonicTime) { |
| 256 | fake_clock_.SetMonotonicTime( |
| 257 | fake_clock_.GetMonotonicTime() + TimeDelta::FromSeconds(1)); |
| 258 | |
| 259 | TimeDelta timeout = default_timeout_ - TimeDelta::FromSeconds(1); |
| 260 | |
| 261 | EXPECT_CALL(mock_var_async_, GetValue(timeout, _)) |
| 262 | .WillOnce(Return(nullptr)); |
| 263 | PMTEST_EXPECT_NULL(eval_ctx_->GetValue(&mock_var_async_)); |
| 264 | } |
| 265 | |
| 266 | TEST_F(PmEvaluationContextTest, ResetEvaluationResetsTimes) { |
| 267 | base::Time cur_time = fake_clock_.GetWallclockTime(); |
| 268 | // Advance the time on the clock but don't call ResetEvaluation yet. |
| 269 | fake_clock_.SetWallclockTime(cur_time + TimeDelta::FromSeconds(4)); |
| 270 | |
| 271 | EXPECT_TRUE(eval_ctx_->IsTimeGreaterThan(cur_time - |
| 272 | TimeDelta::FromSeconds(1))); |
| 273 | EXPECT_FALSE(eval_ctx_->IsTimeGreaterThan(cur_time)); |
| 274 | EXPECT_FALSE(eval_ctx_->IsTimeGreaterThan(cur_time + |
| 275 | TimeDelta::FromSeconds(1))); |
| 276 | // Call ResetEvaluation now, which should use the new evaluation time. |
| 277 | eval_ctx_->ResetEvaluation(); |
| 278 | |
| 279 | cur_time = fake_clock_.GetWallclockTime(); |
| 280 | EXPECT_TRUE(eval_ctx_->IsTimeGreaterThan(cur_time - |
| 281 | TimeDelta::FromSeconds(1))); |
| 282 | EXPECT_FALSE(eval_ctx_->IsTimeGreaterThan(cur_time)); |
| 283 | EXPECT_FALSE(eval_ctx_->IsTimeGreaterThan(cur_time + |
| 284 | TimeDelta::FromSeconds(1))); |
| 285 | } |
| 286 | |
| 287 | TEST_F(PmEvaluationContextTest, IsTimeGreaterThanSignalsTriggerReevaluation) { |
| 288 | EXPECT_FALSE(eval_ctx_->IsTimeGreaterThan( |
| 289 | fake_clock_.GetWallclockTime() + TimeDelta::FromSeconds(1))); |
| 290 | |
| 291 | // The "false" from IsTimeGreaterThan means that's not that timestamp yet, so |
| 292 | // this should schedule a callback for when that happens. |
| 293 | EXPECT_TRUE(eval_ctx_->RunOnValueChangeOrTimeout(Bind(&DoNothing))); |
| 294 | } |
| 295 | |
| 296 | TEST_F(PmEvaluationContextTest, IsTimeGreaterThanDoesntRecordPastTimestamps) { |
| 297 | // IsTimeGreaterThan() should ignore timestamps on the past for reevaluation. |
| 298 | EXPECT_TRUE(eval_ctx_->IsTimeGreaterThan( |
| 299 | fake_clock_.GetWallclockTime() - TimeDelta::FromSeconds(20))); |
| 300 | EXPECT_TRUE(eval_ctx_->IsTimeGreaterThan( |
| 301 | fake_clock_.GetWallclockTime() - TimeDelta::FromSeconds(1))); |
| 302 | |
| 303 | // Callback should not be scheduled. |
| 304 | EXPECT_FALSE(eval_ctx_->RunOnValueChangeOrTimeout(Bind(&DoNothing))); |
| 305 | } |
| 306 | |
Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 307 | } // namespace chromeos_policy_manager |