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