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 | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 16 | |
Alex Deymo | aab50e3 | 2014-11-10 19:55:35 -0800 | [diff] [blame] | 17 | #include "update_engine/update_manager/update_manager.h" |
| 18 | |
Gilad Arnold | f9f85d6 | 2014-06-19 18:07:01 -0700 | [diff] [blame] | 19 | #include <unistd.h> |
| 20 | |
Alex Deymo | 7b948f0 | 2014-03-10 17:01:10 -0700 | [diff] [blame] | 21 | #include <algorithm> |
Ben Chan | 02f7c1d | 2014-10-18 15:18:02 -0700 | [diff] [blame] | 22 | #include <memory> |
Alex Deymo | 7b948f0 | 2014-03-10 17:01:10 -0700 | [diff] [blame] | 23 | #include <string> |
Gilad Arnold | dc4bb26 | 2014-07-23 10:45:19 -0700 | [diff] [blame] | 24 | #include <tuple> |
Alex Deymo | 94c0616 | 2014-03-21 20:34:46 -0700 | [diff] [blame] | 25 | #include <utility> |
Alex Deymo | 7b948f0 | 2014-03-10 17:01:10 -0700 | [diff] [blame] | 26 | #include <vector> |
| 27 | |
| 28 | #include <base/bind.h> |
Alex Deymo | 509dd53 | 2015-06-10 14:11:05 -0700 | [diff] [blame] | 29 | #include <base/test/simple_test_clock.h> |
Gilad Arnold | f62a4b8 | 2014-05-01 07:41:07 -0700 | [diff] [blame] | 30 | #include <base/time/time.h> |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 31 | #include <brillo/message_loops/fake_message_loop.h> |
| 32 | #include <brillo/message_loops/message_loop.h> |
| 33 | #include <brillo/message_loops/message_loop_utils.h> |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 34 | #include <gmock/gmock.h> |
Alex Deymo | 1f01291 | 2014-04-24 19:08:04 -0700 | [diff] [blame] | 35 | #include <gtest/gtest.h> |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 36 | |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 37 | #include "update_engine/common/fake_clock.h" |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 38 | #include "update_engine/update_manager/default_policy.h" |
| 39 | #include "update_engine/update_manager/fake_state.h" |
| 40 | #include "update_engine/update_manager/mock_policy.h" |
| 41 | #include "update_engine/update_manager/umtest_utils.h" |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 42 | |
Alex Deymo | 7b948f0 | 2014-03-10 17:01:10 -0700 | [diff] [blame] | 43 | using base::Bind; |
| 44 | using base::Callback; |
Gilad Arnold | f62a4b8 | 2014-05-01 07:41:07 -0700 | [diff] [blame] | 45 | using base::Time; |
| 46 | using base::TimeDelta; |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 47 | using brillo::MessageLoop; |
| 48 | using brillo::MessageLoopRunMaxIterations; |
Gilad Arnold | b3b0544 | 2014-05-30 14:25:05 -0700 | [diff] [blame] | 49 | using chromeos_update_engine::ErrorCode; |
Alex Deymo | 41a75a7 | 2014-04-15 15:36:22 -0700 | [diff] [blame] | 50 | using chromeos_update_engine::FakeClock; |
Alex Deymo | 7b948f0 | 2014-03-10 17:01:10 -0700 | [diff] [blame] | 51 | using std::pair; |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 52 | using std::string; |
Gilad Arnold | dc4bb26 | 2014-07-23 10:45:19 -0700 | [diff] [blame] | 53 | using std::tuple; |
Ben Chan | 02f7c1d | 2014-10-18 15:18:02 -0700 | [diff] [blame] | 54 | using std::unique_ptr; |
Alex Deymo | 7b948f0 | 2014-03-10 17:01:10 -0700 | [diff] [blame] | 55 | using std::vector; |
Gilad Arnold | 5ef9c48 | 2014-03-03 13:51:02 -0800 | [diff] [blame] | 56 | |
Gilad Arnold | f62a4b8 | 2014-05-01 07:41:07 -0700 | [diff] [blame] | 57 | namespace { |
| 58 | |
| 59 | // Generates a fixed timestamp for use in faking the current time. |
| 60 | Time FixedTime() { |
| 61 | Time::Exploded now_exp; |
| 62 | now_exp.year = 2014; |
| 63 | now_exp.month = 3; |
| 64 | now_exp.day_of_week = 2; |
| 65 | now_exp.day_of_month = 18; |
| 66 | now_exp.hour = 8; |
| 67 | now_exp.minute = 5; |
| 68 | now_exp.second = 33; |
| 69 | now_exp.millisecond = 675; |
Hidehiko Abe | 2b9d241 | 2017-12-13 18:56:18 +0900 | [diff] [blame^] | 70 | Time time; |
| 71 | ignore_result(Time::FromLocalExploded(now_exp, &time)); |
| 72 | return time; |
Gilad Arnold | f62a4b8 | 2014-05-01 07:41:07 -0700 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | } // namespace |
| 76 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 77 | namespace chromeos_update_manager { |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 78 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 79 | class UmUpdateManagerTest : public ::testing::Test { |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 80 | protected: |
Alex Vakulenko | 157fe30 | 2014-08-11 15:59:58 -0700 | [diff] [blame] | 81 | void SetUp() override { |
Alex Deymo | 509dd53 | 2015-06-10 14:11:05 -0700 | [diff] [blame] | 82 | loop_.SetAsCurrent(); |
Alex Deymo | 42c30c3 | 2014-04-24 18:41:18 -0700 | [diff] [blame] | 83 | fake_state_ = new FakeState(); |
Gilad Arnold | b227199 | 2014-06-19 12:35:24 -0700 | [diff] [blame] | 84 | umut_.reset(new UpdateManager(&fake_clock_, TimeDelta::FromSeconds(5), |
Gilad Arnold | fd45a73 | 2014-08-07 15:53:46 -0700 | [diff] [blame] | 85 | TimeDelta::FromSeconds(1), fake_state_)); |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 86 | } |
| 87 | |
Alex Deymo | 509dd53 | 2015-06-10 14:11:05 -0700 | [diff] [blame] | 88 | void TearDown() override { |
| 89 | EXPECT_FALSE(loop_.PendingTasks()); |
| 90 | } |
| 91 | |
| 92 | base::SimpleTestClock test_clock_; |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 93 | brillo::FakeMessageLoop loop_{&test_clock_}; |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 94 | FakeState* fake_state_; // Owned by the umut_. |
Alex Deymo | 41a75a7 | 2014-04-15 15:36:22 -0700 | [diff] [blame] | 95 | FakeClock fake_clock_; |
Ben Chan | 02f7c1d | 2014-10-18 15:18:02 -0700 | [diff] [blame] | 96 | unique_ptr<UpdateManager> umut_; |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 97 | }; |
| 98 | |
| 99 | // The FailingPolicy implements a single method and make it always fail. This |
| 100 | // class extends the DefaultPolicy class to allow extensions of the Policy |
| 101 | // class without extending nor changing this test. |
| 102 | class FailingPolicy : public DefaultPolicy { |
Gilad Arnold | f9f85d6 | 2014-06-19 18:07:01 -0700 | [diff] [blame] | 103 | public: |
| 104 | explicit FailingPolicy(int* num_called_p) : num_called_p_(num_called_p) {} |
| 105 | FailingPolicy() : FailingPolicy(nullptr) {} |
Alex Vakulenko | 157fe30 | 2014-08-11 15:59:58 -0700 | [diff] [blame] | 106 | EvalStatus UpdateCheckAllowed(EvaluationContext* ec, State* state, |
| 107 | string* error, |
| 108 | UpdateCheckParams* result) const override { |
Gilad Arnold | f9f85d6 | 2014-06-19 18:07:01 -0700 | [diff] [blame] | 109 | if (num_called_p_) |
| 110 | (*num_called_p_)++; |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 111 | *error = "FailingPolicy failed."; |
Alex Deymo | e636c3c | 2014-03-11 19:02:08 -0700 | [diff] [blame] | 112 | return EvalStatus::kFailed; |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 113 | } |
Gilad Arnold | f9f85d6 | 2014-06-19 18:07:01 -0700 | [diff] [blame] | 114 | |
| 115 | protected: |
Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 116 | string PolicyName() const override { return "FailingPolicy"; } |
Gilad Arnold | f9f85d6 | 2014-06-19 18:07:01 -0700 | [diff] [blame] | 117 | |
| 118 | private: |
| 119 | int* num_called_p_; |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 120 | }; |
| 121 | |
Alex Deymo | 7b948f0 | 2014-03-10 17:01:10 -0700 | [diff] [blame] | 122 | // The LazyPolicy always returns EvalStatus::kAskMeAgainLater. |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 123 | class LazyPolicy : public DefaultPolicy { |
Alex Vakulenko | 157fe30 | 2014-08-11 15:59:58 -0700 | [diff] [blame] | 124 | EvalStatus UpdateCheckAllowed(EvaluationContext* ec, State* state, |
| 125 | string* error, |
| 126 | UpdateCheckParams* result) const override { |
Alex Deymo | e636c3c | 2014-03-11 19:02:08 -0700 | [diff] [blame] | 127 | return EvalStatus::kAskMeAgainLater; |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 128 | } |
Gilad Arnold | f9f85d6 | 2014-06-19 18:07:01 -0700 | [diff] [blame] | 129 | |
| 130 | protected: |
Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 131 | string PolicyName() const override { return "LazyPolicy"; } |
Gilad Arnold | f9f85d6 | 2014-06-19 18:07:01 -0700 | [diff] [blame] | 132 | }; |
| 133 | |
Gilad Arnold | fd45a73 | 2014-08-07 15:53:46 -0700 | [diff] [blame] | 134 | // A policy that sleeps for a predetermined amount of time, then checks for a |
| 135 | // wallclock-based time threshold (if given) and returns |
| 136 | // EvalStatus::kAskMeAgainLater if not passed; otherwise, returns |
| 137 | // EvalStatus::kSucceeded. Increments a counter every time it is being queried, |
| 138 | // if a pointer to it is provided. |
Gilad Arnold | f9f85d6 | 2014-06-19 18:07:01 -0700 | [diff] [blame] | 139 | class DelayPolicy : public DefaultPolicy { |
| 140 | public: |
Gilad Arnold | fd45a73 | 2014-08-07 15:53:46 -0700 | [diff] [blame] | 141 | DelayPolicy(int sleep_secs, Time time_threshold, int* num_called_p) |
Gilad Arnold | f9f85d6 | 2014-06-19 18:07:01 -0700 | [diff] [blame] | 142 | : sleep_secs_(sleep_secs), time_threshold_(time_threshold), |
| 143 | num_called_p_(num_called_p) {} |
Alex Vakulenko | 157fe30 | 2014-08-11 15:59:58 -0700 | [diff] [blame] | 144 | EvalStatus UpdateCheckAllowed(EvaluationContext* ec, State* state, |
| 145 | string* error, |
| 146 | UpdateCheckParams* result) const override { |
Gilad Arnold | f9f85d6 | 2014-06-19 18:07:01 -0700 | [diff] [blame] | 147 | if (num_called_p_) |
| 148 | (*num_called_p_)++; |
Gilad Arnold | fd45a73 | 2014-08-07 15:53:46 -0700 | [diff] [blame] | 149 | |
| 150 | // Sleep for a predetermined amount of time. |
| 151 | if (sleep_secs_ > 0) |
| 152 | sleep(sleep_secs_); |
| 153 | |
| 154 | // Check for a time threshold. This can be used to ensure that the policy |
| 155 | // has some non-constant dependency. |
| 156 | if (time_threshold_ < Time::Max() && |
| 157 | ec->IsWallclockTimeGreaterThan(time_threshold_)) |
| 158 | return EvalStatus::kSucceeded; |
| 159 | |
Gilad Arnold | f9f85d6 | 2014-06-19 18:07:01 -0700 | [diff] [blame] | 160 | return EvalStatus::kAskMeAgainLater; |
| 161 | } |
| 162 | |
| 163 | protected: |
Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 164 | string PolicyName() const override { return "DelayPolicy"; } |
Gilad Arnold | f9f85d6 | 2014-06-19 18:07:01 -0700 | [diff] [blame] | 165 | |
| 166 | private: |
| 167 | int sleep_secs_; |
Gilad Arnold | fd45a73 | 2014-08-07 15:53:46 -0700 | [diff] [blame] | 168 | Time time_threshold_; |
Gilad Arnold | f9f85d6 | 2014-06-19 18:07:01 -0700 | [diff] [blame] | 169 | int* num_called_p_; |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 170 | }; |
| 171 | |
Alex Deymo | 7b948f0 | 2014-03-10 17:01:10 -0700 | [diff] [blame] | 172 | // AccumulateCallsCallback() adds to the passed |acc| accumulator vector pairs |
| 173 | // of EvalStatus and T instances. This allows to create a callback that keeps |
| 174 | // track of when it is called and the arguments passed to it, to be used with |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 175 | // the UpdateManager::AsyncPolicyRequest(). |
Alex Deymo | 7b948f0 | 2014-03-10 17:01:10 -0700 | [diff] [blame] | 176 | template<typename T> |
| 177 | static void AccumulateCallsCallback(vector<pair<EvalStatus, T>>* acc, |
| 178 | EvalStatus status, const T& result) { |
| 179 | acc->push_back(std::make_pair(status, result)); |
| 180 | } |
| 181 | |
Alex Deymo | 0d11c60 | 2014-04-23 20:12:20 -0700 | [diff] [blame] | 182 | // Tests that policy requests are completed successfully. It is important that |
| 183 | // this tests cover all policy requests as defined in Policy. |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 184 | TEST_F(UmUpdateManagerTest, PolicyRequestCallUpdateCheckAllowed) { |
Alex Deymo | 0d11c60 | 2014-04-23 20:12:20 -0700 | [diff] [blame] | 185 | UpdateCheckParams result; |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 186 | EXPECT_EQ(EvalStatus::kSucceeded, umut_->PolicyRequest( |
Alex Deymo | 0d11c60 | 2014-04-23 20:12:20 -0700 | [diff] [blame] | 187 | &Policy::UpdateCheckAllowed, &result)); |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 188 | } |
| 189 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 190 | TEST_F(UmUpdateManagerTest, PolicyRequestCallUpdateCanStart) { |
Gilad Arnold | dc4bb26 | 2014-07-23 10:45:19 -0700 | [diff] [blame] | 191 | UpdateState update_state = UpdateState(); |
| 192 | update_state.is_interactive = true; |
| 193 | update_state.is_delta_payload = false; |
| 194 | update_state.first_seen = FixedTime(); |
| 195 | update_state.num_checks = 1; |
| 196 | update_state.num_failures = 0; |
| 197 | update_state.failures_last_updated = Time(); |
| 198 | update_state.download_urls = vector<string>{"http://fake/url/"}; |
| 199 | update_state.download_errors_max = 10; |
Gilad Arnold | 78ecbfc | 2014-10-22 14:38:25 -0700 | [diff] [blame] | 200 | update_state.p2p_downloading_disabled = false; |
| 201 | update_state.p2p_sharing_disabled = false; |
Gilad Arnold | 349ac83 | 2014-10-06 14:20:28 -0700 | [diff] [blame] | 202 | update_state.p2p_num_attempts = 0; |
| 203 | update_state.p2p_first_attempted = Time(); |
Gilad Arnold | dc4bb26 | 2014-07-23 10:45:19 -0700 | [diff] [blame] | 204 | update_state.last_download_url_idx = -1; |
| 205 | update_state.last_download_url_num_errors = 0; |
| 206 | update_state.download_errors = vector<tuple<int, ErrorCode, Time>>(); |
| 207 | update_state.backoff_expiry = Time(); |
| 208 | update_state.is_backoff_disabled = false; |
| 209 | update_state.scatter_wait_period = TimeDelta::FromSeconds(15); |
| 210 | update_state.scatter_check_threshold = 4; |
| 211 | update_state.scatter_wait_period_max = TimeDelta::FromSeconds(60); |
| 212 | update_state.scatter_check_threshold_min = 2; |
| 213 | update_state.scatter_check_threshold_max = 8; |
| 214 | |
Gilad Arnold | 42f253b | 2014-06-25 12:39:17 -0700 | [diff] [blame] | 215 | UpdateDownloadParams result; |
Gilad Arnold | f62a4b8 | 2014-05-01 07:41:07 -0700 | [diff] [blame] | 216 | EXPECT_EQ(EvalStatus::kSucceeded, |
Gilad Arnold | dc4bb26 | 2014-07-23 10:45:19 -0700 | [diff] [blame] | 217 | umut_->PolicyRequest(&Policy::UpdateCanStart, &result, |
Gilad Arnold | f62a4b8 | 2014-05-01 07:41:07 -0700 | [diff] [blame] | 218 | update_state)); |
| 219 | } |
| 220 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 221 | TEST_F(UmUpdateManagerTest, PolicyRequestCallsDefaultOnError) { |
| 222 | umut_->set_policy(new FailingPolicy()); |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 223 | |
| 224 | // Tests that the DefaultPolicy instance is called when the method fails, |
| 225 | // which will set this as true. |
Alex Deymo | 0d11c60 | 2014-04-23 20:12:20 -0700 | [diff] [blame] | 226 | UpdateCheckParams result; |
| 227 | result.updates_enabled = false; |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 228 | EvalStatus status = umut_->PolicyRequest( |
Alex Deymo | 680d022 | 2014-04-24 21:00:08 -0700 | [diff] [blame] | 229 | &Policy::UpdateCheckAllowed, &result); |
Gilad Arnold | af2f6ae | 2014-04-28 14:14:52 -0700 | [diff] [blame] | 230 | EXPECT_EQ(EvalStatus::kSucceeded, status); |
Alex Deymo | 0d11c60 | 2014-04-23 20:12:20 -0700 | [diff] [blame] | 231 | EXPECT_TRUE(result.updates_enabled); |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 232 | } |
| 233 | |
Gilad Arnold | 897b5e5 | 2014-05-21 09:37:18 -0700 | [diff] [blame] | 234 | // This test only applies to debug builds where DCHECK is enabled. |
| 235 | #if DCHECK_IS_ON |
| 236 | TEST_F(UmUpdateManagerTest, PolicyRequestDoesntBlockDeathTest) { |
| 237 | // The update manager should die (DCHECK) if a policy called synchronously |
| 238 | // returns a kAskMeAgainLater value. |
Alex Deymo | 0d11c60 | 2014-04-23 20:12:20 -0700 | [diff] [blame] | 239 | UpdateCheckParams result; |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 240 | umut_->set_policy(new LazyPolicy()); |
Gilad Arnold | 897b5e5 | 2014-05-21 09:37:18 -0700 | [diff] [blame] | 241 | EXPECT_DEATH(umut_->PolicyRequest(&Policy::UpdateCheckAllowed, &result), ""); |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 242 | } |
Gilad Arnold | 897b5e5 | 2014-05-21 09:37:18 -0700 | [diff] [blame] | 243 | #endif // DCHECK_IS_ON |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 244 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 245 | TEST_F(UmUpdateManagerTest, AsyncPolicyRequestDelaysEvaluation) { |
Alex Deymo | 7b948f0 | 2014-03-10 17:01:10 -0700 | [diff] [blame] | 246 | // To avoid differences in code execution order between an AsyncPolicyRequest |
| 247 | // call on a policy that returns AskMeAgainLater the first time and one that |
| 248 | // succeeds the first time, we ensure that the passed callback is called from |
| 249 | // the main loop in both cases even when we could evaluate it right now. |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 250 | umut_->set_policy(new FailingPolicy()); |
Alex Deymo | 7b948f0 | 2014-03-10 17:01:10 -0700 | [diff] [blame] | 251 | |
Alex Deymo | 0d11c60 | 2014-04-23 20:12:20 -0700 | [diff] [blame] | 252 | vector<pair<EvalStatus, UpdateCheckParams>> calls; |
Gilad Arnold | 44dc3bf | 2014-07-18 23:39:38 -0700 | [diff] [blame] | 253 | Callback<void(EvalStatus, const UpdateCheckParams&)> callback = Bind( |
| 254 | AccumulateCallsCallback<UpdateCheckParams>, &calls); |
Alex Deymo | 7b948f0 | 2014-03-10 17:01:10 -0700 | [diff] [blame] | 255 | |
Gilad Arnold | fd45a73 | 2014-08-07 15:53:46 -0700 | [diff] [blame] | 256 | umut_->AsyncPolicyRequest(callback, &Policy::UpdateCheckAllowed); |
Alex Deymo | 7b948f0 | 2014-03-10 17:01:10 -0700 | [diff] [blame] | 257 | // The callback should wait until we run the main loop for it to be executed. |
Alex Deymo | 80f70ff | 2016-02-10 16:08:11 -0800 | [diff] [blame] | 258 | EXPECT_EQ(0U, calls.size()); |
Alex Deymo | 509dd53 | 2015-06-10 14:11:05 -0700 | [diff] [blame] | 259 | MessageLoopRunMaxIterations(MessageLoop::current(), 100); |
Alex Deymo | 80f70ff | 2016-02-10 16:08:11 -0800 | [diff] [blame] | 260 | EXPECT_EQ(1U, calls.size()); |
Alex Deymo | 7b948f0 | 2014-03-10 17:01:10 -0700 | [diff] [blame] | 261 | } |
| 262 | |
Gilad Arnold | fd45a73 | 2014-08-07 15:53:46 -0700 | [diff] [blame] | 263 | TEST_F(UmUpdateManagerTest, AsyncPolicyRequestTimeoutDoesNotFire) { |
Gilad Arnold | f9f85d6 | 2014-06-19 18:07:01 -0700 | [diff] [blame] | 264 | // Set up an async policy call to return immediately, then wait a little and |
| 265 | // ensure that the timeout event does not fire. |
| 266 | int num_called = 0; |
| 267 | umut_->set_policy(new FailingPolicy(&num_called)); |
| 268 | |
| 269 | vector<pair<EvalStatus, UpdateCheckParams>> calls; |
| 270 | Callback<void(EvalStatus, const UpdateCheckParams&)> callback = |
| 271 | Bind(AccumulateCallsCallback<UpdateCheckParams>, &calls); |
| 272 | |
Gilad Arnold | fd45a73 | 2014-08-07 15:53:46 -0700 | [diff] [blame] | 273 | umut_->AsyncPolicyRequest(callback, &Policy::UpdateCheckAllowed); |
Gilad Arnold | f9f85d6 | 2014-06-19 18:07:01 -0700 | [diff] [blame] | 274 | // Run the main loop, ensure that policy was attempted once before deferring |
| 275 | // to the default. |
Alex Deymo | 509dd53 | 2015-06-10 14:11:05 -0700 | [diff] [blame] | 276 | MessageLoopRunMaxIterations(MessageLoop::current(), 100); |
Gilad Arnold | f9f85d6 | 2014-06-19 18:07:01 -0700 | [diff] [blame] | 277 | EXPECT_EQ(1, num_called); |
Alex Deymo | 80f70ff | 2016-02-10 16:08:11 -0800 | [diff] [blame] | 278 | ASSERT_EQ(1U, calls.size()); |
Gilad Arnold | f9f85d6 | 2014-06-19 18:07:01 -0700 | [diff] [blame] | 279 | EXPECT_EQ(EvalStatus::kSucceeded, calls[0].first); |
| 280 | // Wait for the timeout to expire, run the main loop again, ensure that |
| 281 | // nothing happened. |
Alex Deymo | 509dd53 | 2015-06-10 14:11:05 -0700 | [diff] [blame] | 282 | test_clock_.Advance(TimeDelta::FromSeconds(2)); |
| 283 | MessageLoopRunMaxIterations(MessageLoop::current(), 10); |
Gilad Arnold | f9f85d6 | 2014-06-19 18:07:01 -0700 | [diff] [blame] | 284 | EXPECT_EQ(1, num_called); |
Alex Deymo | 80f70ff | 2016-02-10 16:08:11 -0800 | [diff] [blame] | 285 | EXPECT_EQ(1U, calls.size()); |
Gilad Arnold | f9f85d6 | 2014-06-19 18:07:01 -0700 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | TEST_F(UmUpdateManagerTest, AsyncPolicyRequestTimesOut) { |
Gilad Arnold | fd45a73 | 2014-08-07 15:53:46 -0700 | [diff] [blame] | 289 | // Set up an async policy call to exceed its expiration timeout, make sure |
| 290 | // that the default policy was not used (no callback) and that evaluation is |
| 291 | // reattempted. |
Gilad Arnold | f9f85d6 | 2014-06-19 18:07:01 -0700 | [diff] [blame] | 292 | int num_called = 0; |
| 293 | umut_->set_policy(new DelayPolicy( |
Gilad Arnold | fd45a73 | 2014-08-07 15:53:46 -0700 | [diff] [blame] | 294 | 0, fake_clock_.GetWallclockTime() + TimeDelta::FromSeconds(3), |
Gilad Arnold | f9f85d6 | 2014-06-19 18:07:01 -0700 | [diff] [blame] | 295 | &num_called)); |
| 296 | |
| 297 | vector<pair<EvalStatus, UpdateCheckParams>> calls; |
| 298 | Callback<void(EvalStatus, const UpdateCheckParams&)> callback = |
| 299 | Bind(AccumulateCallsCallback<UpdateCheckParams>, &calls); |
| 300 | |
Gilad Arnold | fd45a73 | 2014-08-07 15:53:46 -0700 | [diff] [blame] | 301 | umut_->AsyncPolicyRequest(callback, &Policy::UpdateCheckAllowed); |
Gilad Arnold | f9f85d6 | 2014-06-19 18:07:01 -0700 | [diff] [blame] | 302 | // Run the main loop, ensure that policy was attempted once but the callback |
| 303 | // was not invoked. |
Alex Deymo | 509dd53 | 2015-06-10 14:11:05 -0700 | [diff] [blame] | 304 | MessageLoopRunMaxIterations(MessageLoop::current(), 100); |
Gilad Arnold | f9f85d6 | 2014-06-19 18:07:01 -0700 | [diff] [blame] | 305 | EXPECT_EQ(1, num_called); |
Alex Deymo | 80f70ff | 2016-02-10 16:08:11 -0800 | [diff] [blame] | 306 | EXPECT_EQ(0U, calls.size()); |
Gilad Arnold | fd45a73 | 2014-08-07 15:53:46 -0700 | [diff] [blame] | 307 | // Wait for the expiration timeout to expire, run the main loop again, |
| 308 | // ensure that reevaluation occurred but callback was not invoked (i.e. |
| 309 | // default policy was not consulted). |
Alex Deymo | 509dd53 | 2015-06-10 14:11:05 -0700 | [diff] [blame] | 310 | test_clock_.Advance(TimeDelta::FromSeconds(2)); |
Gilad Arnold | fd45a73 | 2014-08-07 15:53:46 -0700 | [diff] [blame] | 311 | fake_clock_.SetWallclockTime(fake_clock_.GetWallclockTime() + |
| 312 | TimeDelta::FromSeconds(2)); |
Alex Deymo | 509dd53 | 2015-06-10 14:11:05 -0700 | [diff] [blame] | 313 | MessageLoopRunMaxIterations(MessageLoop::current(), 10); |
Gilad Arnold | fd45a73 | 2014-08-07 15:53:46 -0700 | [diff] [blame] | 314 | EXPECT_EQ(2, num_called); |
Alex Deymo | 80f70ff | 2016-02-10 16:08:11 -0800 | [diff] [blame] | 315 | EXPECT_EQ(0U, calls.size()); |
Gilad Arnold | fd45a73 | 2014-08-07 15:53:46 -0700 | [diff] [blame] | 316 | // Wait for reevaluation due to delay to happen, ensure that it occurs and |
| 317 | // that the callback is invoked. |
Alex Deymo | 509dd53 | 2015-06-10 14:11:05 -0700 | [diff] [blame] | 318 | test_clock_.Advance(TimeDelta::FromSeconds(2)); |
Gilad Arnold | fd45a73 | 2014-08-07 15:53:46 -0700 | [diff] [blame] | 319 | fake_clock_.SetWallclockTime(fake_clock_.GetWallclockTime() + |
| 320 | TimeDelta::FromSeconds(2)); |
Alex Deymo | 509dd53 | 2015-06-10 14:11:05 -0700 | [diff] [blame] | 321 | MessageLoopRunMaxIterations(MessageLoop::current(), 10); |
Gilad Arnold | fd45a73 | 2014-08-07 15:53:46 -0700 | [diff] [blame] | 322 | EXPECT_EQ(3, num_called); |
Alex Deymo | 80f70ff | 2016-02-10 16:08:11 -0800 | [diff] [blame] | 323 | ASSERT_EQ(1U, calls.size()); |
Gilad Arnold | f9f85d6 | 2014-06-19 18:07:01 -0700 | [diff] [blame] | 324 | EXPECT_EQ(EvalStatus::kSucceeded, calls[0].first); |
Gilad Arnold | f9f85d6 | 2014-06-19 18:07:01 -0700 | [diff] [blame] | 325 | } |
| 326 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 327 | } // namespace chromeos_update_manager |