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