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