Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(89)

Side by Side Diff: chrome/browser/policy/cloud/cloud_policy_invalidator_unittest.cc

Issue 213743014: Add an extra delay for policy invalidations to be available. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include <string> 5 #include <string>
6 6
7 #include "base/basictypes.h" 7 #include "base/basictypes.h"
8 #include "base/bind.h" 8 #include "base/bind.h"
9 #include "base/memory/ref_counted.h" 9 #include "base/memory/ref_counted.h"
10 #include "base/memory/scoped_ptr.h" 10 #include "base/memory/scoped_ptr.h"
11 #include "base/message_loop/message_loop.h" 11 #include "base/message_loop/message_loop.h"
12 #include "base/metrics/histogram.h" 12 #include "base/metrics/histogram.h"
13 #include "base/metrics/histogram_samples.h" 13 #include "base/metrics/histogram_samples.h"
14 #include "base/metrics/sample_map.h" 14 #include "base/metrics/sample_map.h"
15 #include "base/metrics/statistics_recorder.h" 15 #include "base/metrics/statistics_recorder.h"
16 #include "base/run_loop.h" 16 #include "base/run_loop.h"
17 #include "base/test/simple_test_clock.h"
17 #include "base/test/test_simple_task_runner.h" 18 #include "base/test/test_simple_task_runner.h"
18 #include "base/time/time.h" 19 #include "base/time/time.h"
19 #include "base/values.h" 20 #include "base/values.h"
20 #include "chrome/browser/invalidation/fake_invalidation_service.h" 21 #include "chrome/browser/invalidation/fake_invalidation_service.h"
21 #include "chrome/browser/policy/cloud/cloud_policy_invalidator.h" 22 #include "chrome/browser/policy/cloud/cloud_policy_invalidator.h"
22 #include "components/policy/core/common/cloud/cloud_policy_constants.h" 23 #include "components/policy/core/common/cloud/cloud_policy_constants.h"
23 #include "components/policy/core/common/cloud/cloud_policy_core.h" 24 #include "components/policy/core/common/cloud/cloud_policy_core.h"
24 #include "components/policy/core/common/cloud/cloud_policy_refresh_scheduler.h" 25 #include "components/policy/core/common/cloud/cloud_policy_refresh_scheduler.h"
25 #include "components/policy/core/common/cloud/enterprise_metrics.h" 26 #include "components/policy/core/common/cloud/enterprise_metrics.h"
26 #include "components/policy/core/common/cloud/mock_cloud_policy_client.h" 27 #include "components/policy/core/common/cloud/mock_cloud_policy_client.h"
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after
140 bool IsInvalidationAcknowledged(const syncer::Invalidation& invalidation); 141 bool IsInvalidationAcknowledged(const syncer::Invalidation& invalidation);
141 142
142 // Determines if the invalidator has registered for an object with the 143 // Determines if the invalidator has registered for an object with the
143 // invalidation service. 144 // invalidation service.
144 bool IsInvalidatorRegistered(); 145 bool IsInvalidatorRegistered();
145 146
146 // Get the current count for the given metric. 147 // Get the current count for the given metric.
147 base::HistogramBase::Count GetCount(MetricPolicyRefresh metric); 148 base::HistogramBase::Count GetCount(MetricPolicyRefresh metric);
148 base::HistogramBase::Count GetInvalidationCount(bool with_payload); 149 base::HistogramBase::Count GetInvalidationCount(bool with_payload);
149 150
151 // Advance the test clock.
152 void AdvanceClock(base::TimeDelta delta);
153
150 private: 154 private:
151 // Checks that the policy was refreshed due to an invalidation with the given 155 // Checks that the policy was refreshed due to an invalidation with the given
152 // base delay. 156 // base delay.
153 bool CheckPolicyRefreshed(base::TimeDelta delay); 157 bool CheckPolicyRefreshed(base::TimeDelta delay);
154 158
155 // Checks that the policy was refreshed the given number of times. 159 // Checks that the policy was refreshed the given number of times.
156 bool CheckPolicyRefreshCount(int count); 160 bool CheckPolicyRefreshCount(int count);
157 161
158 // Returns the object id of the given policy object. 162 // Returns the object id of the given policy object.
159 const invalidation::ObjectId& GetPolicyObjectId(PolicyObject object) const; 163 const invalidation::ObjectId& GetPolicyObjectId(PolicyObject object) const;
160 164
161 // Get histogram samples for the given histogram. 165 // Get histogram samples for the given histogram.
162 scoped_ptr<base::HistogramSamples> GetHistogramSamples( 166 scoped_ptr<base::HistogramSamples> GetHistogramSamples(
163 const std::string& name) const; 167 const std::string& name) const;
164 168
165 base::MessageLoop loop_; 169 base::MessageLoop loop_;
166 170
167 // Objects the invalidator depends on. 171 // Objects the invalidator depends on.
168 invalidation::FakeInvalidationService invalidation_service_; 172 invalidation::FakeInvalidationService invalidation_service_;
169 MockCloudPolicyStore store_; 173 MockCloudPolicyStore store_;
170 CloudPolicyCore core_; 174 CloudPolicyCore core_;
171 MockCloudPolicyClient* client_; 175 MockCloudPolicyClient* client_;
172 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; 176 scoped_refptr<base::TestSimpleTaskRunner> task_runner_;
177 base::SimpleTestClock* clock_;
173 178
174 // The invalidator which will be tested. 179 // The invalidator which will be tested.
175 scoped_ptr<CloudPolicyInvalidator> invalidator_; 180 scoped_ptr<CloudPolicyInvalidator> invalidator_;
176 181
177 // Object ids for the test policy objects. 182 // Object ids for the test policy objects.
178 invalidation::ObjectId object_id_a_; 183 invalidation::ObjectId object_id_a_;
179 invalidation::ObjectId object_id_b_; 184 invalidation::ObjectId object_id_b_;
180 185
181 // Increasing policy timestamp. 186 // Increasing policy timestamp.
182 int64 timestamp_; 187 int64 timestamp_;
(...skipping 13 matching lines...) Expand all
196 scoped_ptr<base::HistogramSamples> invalidations_samples_; 201 scoped_ptr<base::HistogramSamples> invalidations_samples_;
197 }; 202 };
198 203
199 CloudPolicyInvalidatorTest::CloudPolicyInvalidatorTest() 204 CloudPolicyInvalidatorTest::CloudPolicyInvalidatorTest()
200 : core_(PolicyNamespaceKey(dm_protocol::kChromeUserPolicyType, 205 : core_(PolicyNamespaceKey(dm_protocol::kChromeUserPolicyType,
201 std::string()), 206 std::string()),
202 &store_, 207 &store_,
203 loop_.message_loop_proxy()), 208 loop_.message_loop_proxy()),
204 client_(NULL), 209 client_(NULL),
205 task_runner_(new base::TestSimpleTaskRunner()), 210 task_runner_(new base::TestSimpleTaskRunner()),
211 clock_(new base::SimpleTestClock()),
206 object_id_a_(135, "asdf"), 212 object_id_a_(135, "asdf"),
207 object_id_b_(246, "zxcv"), 213 object_id_b_(246, "zxcv"),
208 timestamp_(123456), 214 timestamp_(123456),
209 policy_value_a_("asdf"), 215 policy_value_a_("asdf"),
210 policy_value_b_("zxcv"), 216 policy_value_b_("zxcv"),
211 policy_value_cur_(policy_value_a_) {} 217 policy_value_cur_(policy_value_a_) {}
212 218
213 void CloudPolicyInvalidatorTest::SetUp() { 219 void CloudPolicyInvalidatorTest::SetUp() {
214 base::StatisticsRecorder::Initialize(); 220 base::StatisticsRecorder::Initialize();
215 refresh_samples_ = GetHistogramSamples(kMetricPolicyRefresh); 221 refresh_samples_ = GetHistogramSamples(kMetricPolicyRefresh);
216 invalidations_samples_ = GetHistogramSamples(kMetricPolicyInvalidations); 222 invalidations_samples_ = GetHistogramSamples(kMetricPolicyInvalidations);
217 } 223 }
218 224
219 void CloudPolicyInvalidatorTest::TearDown() { 225 void CloudPolicyInvalidatorTest::TearDown() {
220 if (invalidator_) 226 if (invalidator_)
221 invalidator_->Shutdown(); 227 invalidator_->Shutdown();
222 core_.Disconnect(); 228 core_.Disconnect();
223 } 229 }
224 230
225 void CloudPolicyInvalidatorTest::StartInvalidator( 231 void CloudPolicyInvalidatorTest::StartInvalidator(
226 bool initialize, 232 bool initialize,
227 bool start_refresh_scheduler) { 233 bool start_refresh_scheduler) {
228 invalidator_.reset(new CloudPolicyInvalidator(&core_, task_runner_)); 234 invalidator_.reset(new CloudPolicyInvalidator(
235 &core_,
236 task_runner_,
237 scoped_ptr<base::Clock>(clock_)));
229 if (start_refresh_scheduler) { 238 if (start_refresh_scheduler) {
230 ConnectCore(); 239 ConnectCore();
231 StartRefreshScheduler(); 240 StartRefreshScheduler();
232 } 241 }
233 if (initialize) 242 if (initialize)
234 InitializeInvalidator(); 243 InitializeInvalidator();
235 } 244 }
236 245
237 void CloudPolicyInvalidatorTest::InitializeInvalidator() { 246 void CloudPolicyInvalidatorTest::InitializeInvalidator() {
238 invalidator_->Initialize(&invalidation_service_); 247 invalidator_->Initialize(&invalidation_service_);
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after
373 refresh_samples_->GetCount(metric); 382 refresh_samples_->GetCount(metric);
374 } 383 }
375 384
376 base::HistogramBase::Count CloudPolicyInvalidatorTest::GetInvalidationCount( 385 base::HistogramBase::Count CloudPolicyInvalidatorTest::GetInvalidationCount(
377 bool with_payload) { 386 bool with_payload) {
378 int metric = with_payload ? 1 : 0; 387 int metric = with_payload ? 1 : 0;
379 return GetHistogramSamples(kMetricPolicyInvalidations)->GetCount(metric) - 388 return GetHistogramSamples(kMetricPolicyInvalidations)->GetCount(metric) -
380 invalidations_samples_->GetCount(metric); 389 invalidations_samples_->GetCount(metric);
381 } 390 }
382 391
392 void CloudPolicyInvalidatorTest::AdvanceClock(base::TimeDelta delta) {
393 clock_->Advance(delta);
394 }
395
383 bool CloudPolicyInvalidatorTest::CheckPolicyRefreshed(base::TimeDelta delay) { 396 bool CloudPolicyInvalidatorTest::CheckPolicyRefreshed(base::TimeDelta delay) {
384 base::TimeDelta max_delay = delay + base::TimeDelta::FromMilliseconds( 397 base::TimeDelta max_delay = delay + base::TimeDelta::FromMilliseconds(
385 CloudPolicyInvalidator::kMaxFetchDelayMin); 398 CloudPolicyInvalidator::kMaxFetchDelayMin);
386 399
387 if (task_runner_->GetPendingTasks().empty()) 400 if (task_runner_->GetPendingTasks().empty())
388 return false; 401 return false;
389 base::TimeDelta actual_delay = task_runner_->GetPendingTasks().back().delay; 402 base::TimeDelta actual_delay = task_runner_->GetPendingTasks().back().delay;
390 EXPECT_GE(actual_delay, delay); 403 EXPECT_GE(actual_delay, delay);
391 EXPECT_LE(actual_delay, max_delay); 404 EXPECT_LE(actual_delay, max_delay);
392 405
(...skipping 376 matching lines...) Expand 10 before | Expand all | Expand 10 after
769 EXPECT_EQ(0, GetCount(METRIC_POLICY_REFRESH_UNCHANGED)); 782 EXPECT_EQ(0, GetCount(METRIC_POLICY_REFRESH_UNCHANGED));
770 EXPECT_EQ(0, GetCount(METRIC_POLICY_REFRESH_INVALIDATED_CHANGED)); 783 EXPECT_EQ(0, GetCount(METRIC_POLICY_REFRESH_INVALIDATED_CHANGED));
771 EXPECT_EQ(0, GetCount(METRIC_POLICY_REFRESH_INVALIDATED_UNCHANGED)); 784 EXPECT_EQ(0, GetCount(METRIC_POLICY_REFRESH_INVALIDATED_UNCHANGED));
772 } 785 }
773 786
774 TEST_F(CloudPolicyInvalidatorTest, RefreshMetricsNoInvalidations) { 787 TEST_F(CloudPolicyInvalidatorTest, RefreshMetricsNoInvalidations) {
775 // Store loads occurring while registered should be differentiated depending 788 // Store loads occurring while registered should be differentiated depending
776 // on whether the invalidation service was enabled or not. 789 // on whether the invalidation service was enabled or not.
777 StorePolicy(POLICY_OBJECT_A); 790 StorePolicy(POLICY_OBJECT_A);
778 StartInvalidator(); 791 StartInvalidator();
792
793 // Initially, invalidations have not been enabled past the grace period, so
794 // invalidations are OFF.
779 StorePolicy(POLICY_OBJECT_A, 0, false /* policy_changed */); 795 StorePolicy(POLICY_OBJECT_A, 0, false /* policy_changed */);
780 StorePolicy(POLICY_OBJECT_A, 0, true /* policy_changed */); 796 StorePolicy(POLICY_OBJECT_A, 0, true /* policy_changed */);
797 EXPECT_EQ(1, GetCount(METRIC_POLICY_REFRESH_CHANGED_NO_INVALIDATIONS));
798
799 // If the clock advances less than the grace period, invalidations are OFF.
800 AdvanceClock(base::TimeDelta::FromSeconds(1));
801 StorePolicy(POLICY_OBJECT_A, 0, false /* policy_changed */);
802 StorePolicy(POLICY_OBJECT_A, 0, true /* policy_changed */);
803 EXPECT_EQ(2, GetCount(METRIC_POLICY_REFRESH_CHANGED_NO_INVALIDATIONS));
804
805 // After the grace period elapses, invalidations are ON.
806 AdvanceClock(base::TimeDelta::FromSeconds(
807 CloudPolicyInvalidator::kInvalidationGracePeriod));
808 StorePolicy(POLICY_OBJECT_A, 0, false /* policy_changed */);
809 StorePolicy(POLICY_OBJECT_A, 0, true /* policy_changed */);
810 EXPECT_EQ(1, GetCount(METRIC_POLICY_REFRESH_CHANGED));
811
812 // After the invalidation service is disabled, invalidations are OFF.
781 DisableInvalidationService(); 813 DisableInvalidationService();
782 StorePolicy(POLICY_OBJECT_A, 0, false /* policy_changed */); 814 StorePolicy(POLICY_OBJECT_A, 0, false /* policy_changed */);
783 StorePolicy(POLICY_OBJECT_A, 0, true /* policy_changed */); 815 StorePolicy(POLICY_OBJECT_A, 0, true /* policy_changed */);
816 EXPECT_EQ(3, GetCount(METRIC_POLICY_REFRESH_CHANGED_NO_INVALIDATIONS));
817
818 // Enabling the invalidation service results in a new grace period, so
819 // invalidations are OFF.
820 EnableInvalidationService();
784 StorePolicy(POLICY_OBJECT_A, 0, false /* policy_changed */); 821 StorePolicy(POLICY_OBJECT_A, 0, false /* policy_changed */);
785 StorePolicy(POLICY_OBJECT_A, 0, true /* policy_changed */); 822 StorePolicy(POLICY_OBJECT_A, 0, true /* policy_changed */);
786 EXPECT_EQ(1, GetCount(METRIC_POLICY_REFRESH_CHANGED)); 823 EXPECT_EQ(4, GetCount(METRIC_POLICY_REFRESH_CHANGED_NO_INVALIDATIONS));
787 EXPECT_EQ(2, GetCount(METRIC_POLICY_REFRESH_CHANGED_NO_INVALIDATIONS)); 824
788 EXPECT_EQ(3, GetCount(METRIC_POLICY_REFRESH_UNCHANGED)); 825 // After the grace period elapses, invalidations are ON.
826 AdvanceClock(base::TimeDelta::FromSeconds(
827 CloudPolicyInvalidator::kInvalidationGracePeriod));
828 StorePolicy(POLICY_OBJECT_A, 0, false /* policy_changed */);
829 StorePolicy(POLICY_OBJECT_A, 0, true /* policy_changed */);
830
831 EXPECT_EQ(2, GetCount(METRIC_POLICY_REFRESH_CHANGED));
832 EXPECT_EQ(4, GetCount(METRIC_POLICY_REFRESH_CHANGED_NO_INVALIDATIONS));
833 EXPECT_EQ(6, GetCount(METRIC_POLICY_REFRESH_UNCHANGED));
789 EXPECT_EQ(0, GetCount(METRIC_POLICY_REFRESH_INVALIDATED_CHANGED)); 834 EXPECT_EQ(0, GetCount(METRIC_POLICY_REFRESH_INVALIDATED_CHANGED));
790 EXPECT_EQ(0, GetCount(METRIC_POLICY_REFRESH_INVALIDATED_UNCHANGED)); 835 EXPECT_EQ(0, GetCount(METRIC_POLICY_REFRESH_INVALIDATED_UNCHANGED));
791 } 836 }
792
793 TEST_F(CloudPolicyInvalidatorTest, RefreshMetricsStoreSameTimestamp) {
794 // Store loads with the same timestamp as the load which causes registration
795 // are not counted.
796 StartInvalidator();
797 StorePolicy(
798 POLICY_OBJECT_A, 0, false /* policy_changed */, 12 /* timestamp */);
799 StorePolicy(
800 POLICY_OBJECT_A, 0, false /* policy_changed */, 12 /* timestamp */);
801 StorePolicy(
802 POLICY_OBJECT_A, 0, true /* policy_changed */, 12 /* timestamp */);
803
804 // The next load with a different timestamp counts.
805 StorePolicy(
806 POLICY_OBJECT_A, 0, true /* policy_changed */, 13 /* timestamp */);
807
808 EXPECT_EQ(1, GetCount(METRIC_POLICY_REFRESH_CHANGED));
809 EXPECT_EQ(0, GetCount(METRIC_POLICY_REFRESH_CHANGED_NO_INVALIDATIONS));
810 EXPECT_EQ(0, GetCount(METRIC_POLICY_REFRESH_UNCHANGED));
811 EXPECT_EQ(0, GetCount(METRIC_POLICY_REFRESH_INVALIDATED_CHANGED));
812 EXPECT_EQ(0, GetCount(METRIC_POLICY_REFRESH_INVALIDATED_UNCHANGED));
813 }
814 837
815 TEST_F(CloudPolicyInvalidatorTest, RefreshMetricsInvalidation) { 838 TEST_F(CloudPolicyInvalidatorTest, RefreshMetricsInvalidation) {
816 // Store loads after an invalidation are counted as invalidated, even if 839 // Store loads after an invalidation are counted as invalidated, even if
817 // the loads do not result in the invalidation being acknowledged. 840 // the loads do not result in the invalidation being acknowledged.
818 StartInvalidator(); 841 StartInvalidator();
819 StorePolicy(POLICY_OBJECT_A); 842 StorePolicy(POLICY_OBJECT_A);
843 AdvanceClock(base::TimeDelta::FromSeconds(
844 CloudPolicyInvalidator::kInvalidationGracePeriod));
820 FireInvalidation(POLICY_OBJECT_A, 5, "test"); 845 FireInvalidation(POLICY_OBJECT_A, 5, "test");
821 StorePolicy(POLICY_OBJECT_A, 0, false /* policy_changed */); 846 StorePolicy(POLICY_OBJECT_A, 0, false /* policy_changed */);
822 StorePolicy(POLICY_OBJECT_A, 0, true /* policy_changed */); 847 StorePolicy(POLICY_OBJECT_A, 0, true /* policy_changed */);
823 StorePolicy(POLICY_OBJECT_A, 5, true /* policy_changed */); 848 StorePolicy(POLICY_OBJECT_A, 5, true /* policy_changed */);
824 849
825 // Store loads after the invalidation is complete are not counted as 850 // Store loads after the invalidation is complete are not counted as
826 // invalidated. 851 // invalidated.
827 StorePolicy(POLICY_OBJECT_A, 0, false /* policy_changed */); 852 StorePolicy(POLICY_OBJECT_A, 0, false /* policy_changed */);
828 StorePolicy(POLICY_OBJECT_A, 0, true /* policy_changed */); 853 StorePolicy(POLICY_OBJECT_A, 0, true /* policy_changed */);
829 StorePolicy(POLICY_OBJECT_A, 0, false /* policy_changed */); 854 StorePolicy(POLICY_OBJECT_A, 0, false /* policy_changed */);
(...skipping 22 matching lines...) Expand all
852 FireUnknownVersionInvalidation(POLICY_OBJECT_A); 877 FireUnknownVersionInvalidation(POLICY_OBJECT_A);
853 FireInvalidation(POLICY_OBJECT_A, 3, "test"); 878 FireInvalidation(POLICY_OBJECT_A, 3, "test");
854 FireInvalidation(POLICY_OBJECT_A, 4, "test"); 879 FireInvalidation(POLICY_OBJECT_A, 4, "test");
855 880
856 // Verify that received invalidations metrics are correct. 881 // Verify that received invalidations metrics are correct.
857 EXPECT_EQ(3, GetInvalidationCount(false /* with_payload */)); 882 EXPECT_EQ(3, GetInvalidationCount(false /* with_payload */));
858 EXPECT_EQ(4, GetInvalidationCount(true /* with_payload */)); 883 EXPECT_EQ(4, GetInvalidationCount(true /* with_payload */));
859 } 884 }
860 885
861 } // namespace policy 886 } // namespace policy
OLDNEW
« no previous file with comments | « chrome/browser/policy/cloud/cloud_policy_invalidator.cc ('k') | chrome/browser/policy/cloud/user_cloud_policy_invalidator.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698