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