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