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

Side by Side Diff: chrome/browser/sync/test/integration/single_client_user_events_sync_test.cc

Issue 2973833002: [Sync] Added integ tests to verify UserEvents retry on TRANSIENT_ERROR. (Closed)
Patch Set: Created 3 years, 5 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
« no previous file with comments | « no previous file | components/sync/test/fake_server/fake_server.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2017 The Chromium Authors. All rights reserved. 1 // Copyright 2017 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 "base/macros.h" 5 #include "base/macros.h"
6 #include "base/time/time.h" 6 #include "base/time/time.h"
7 #include "chrome/browser/sync/test/integration/single_client_status_change_check er.h" 7 #include "chrome/browser/sync/test/integration/single_client_status_change_check er.h"
8 #include "chrome/browser/sync/test/integration/status_change_checker.h" 8 #include "chrome/browser/sync/test/integration/status_change_checker.h"
9 #include "chrome/browser/sync/test/integration/sync_test.h" 9 #include "chrome/browser/sync/test/integration/sync_test.h"
10 #include "chrome/browser/sync/user_event_service_factory.h" 10 #include "chrome/browser/sync/user_event_service_factory.h"
11 #include "components/sync/protocol/user_event_specifics.pb.h" 11 #include "components/sync/protocol/user_event_specifics.pb.h"
12 #include "components/sync/user_events/user_event_service.h" 12 #include "components/sync/user_events/user_event_service.h"
13 13
14 using base::Time;
15 using base::TimeDelta;
14 using fake_server::FakeServer; 16 using fake_server::FakeServer;
15 using sync_pb::UserEventSpecifics; 17 using sync_pb::UserEventSpecifics;
16 using sync_pb::SyncEntity; 18 using sync_pb::SyncEntity;
19 using sync_pb::CommitResponse;
17 20
18 namespace { 21 namespace {
19 22
23 UserEventSpecifics CreateEvent(int minutes_ago) {
24 UserEventSpecifics specifics;
25 specifics.set_event_time_usec(
26 (Time::Now() - TimeDelta::FromMinutes(minutes_ago)).ToInternalValue());
27 specifics.mutable_test_event();
28 return specifics;
29 }
30
31 CommitResponse::ResponseType BounceType(
32 CommitResponse::ResponseType type,
33 const fake_server::FakeServerEntity& entity) {
34 return type;
35 }
36
37 CommitResponse::ResponseType TransientErrorFirst(
38 bool* first,
39 UserEventSpecifics* retry_specifics,
40 const fake_server::FakeServerEntity& entity) {
41 if (*first) {
42 *first = false;
43 SyncEntity sync_entity;
44 entity.SerializeAsProto(&sync_entity);
45 *retry_specifics = sync_entity.specifics().user_event();
46 return CommitResponse::TRANSIENT_ERROR;
47 } else {
48 return CommitResponse::SUCCESS;
49 }
50 }
51
20 class UserEventEqualityChecker : public SingleClientStatusChangeChecker { 52 class UserEventEqualityChecker : public SingleClientStatusChangeChecker {
21 public: 53 public:
22 UserEventEqualityChecker(browser_sync::ProfileSyncService* service, 54 UserEventEqualityChecker(browser_sync::ProfileSyncService* service,
23 FakeServer* fake_server, 55 FakeServer* fake_server,
24 std::vector<UserEventSpecifics> expected_specifics) 56 std::vector<UserEventSpecifics> expected_specifics)
25 : SingleClientStatusChangeChecker(service), fake_server_(fake_server) { 57 : SingleClientStatusChangeChecker(service), fake_server_(fake_server) {
26 for (const UserEventSpecifics& specifics : expected_specifics) { 58 for (const UserEventSpecifics& specifics : expected_specifics) {
27 expected_specifics_[specifics.event_time_usec()] = specifics; 59 expected_specifics_.insert(std::pair<int64_t, UserEventSpecifics>(
60 specifics.event_time_usec(), specifics));
28 } 61 }
29 } 62 }
30 63
31 bool IsExitConditionSatisfied() override { 64 bool IsExitConditionSatisfied() override {
32 std::vector<SyncEntity> entities = 65 std::vector<SyncEntity> entities =
33 fake_server_->GetSyncEntitiesByModelType(syncer::USER_EVENTS); 66 fake_server_->GetSyncEntitiesByModelType(syncer::USER_EVENTS);
34 67
35 // |entities.size()| is only going to grow, if |entities.size()| ever 68 // |entities.size()| is only going to grow, if |entities.size()| ever
36 // becomes bigger then all hope is lost of passing, stop now. 69 // becomes bigger then all hope is lost of passing, stop now.
37 EXPECT_GE(expected_specifics_.size(), entities.size()); 70 EXPECT_GE(expected_specifics_.size(), entities.size());
38 71
39 if (expected_specifics_.size() > entities.size()) { 72 if (expected_specifics_.size() > entities.size()) {
40 return false; 73 return false;
41 } 74 }
42 75
76 // Because we have a multimap, we cannot just check counts and equality of
77 // items, but we need to make sure 2 of A and 1 of B is not the same as 1 of
78 // A and 2 of B. So to make this easy, copy the multimap and remove items.
79 std::multimap<int64_t, UserEventSpecifics> copied_expected_(
80 expected_specifics_.begin(), expected_specifics_.end());
43 for (const SyncEntity& entity : entities) { 81 for (const SyncEntity& entity : entities) {
44 UserEventSpecifics server_specifics = entity.specifics().user_event(); 82 UserEventSpecifics server_specifics = entity.specifics().user_event();
45 auto iter = expected_specifics_.find(server_specifics.event_time_usec()); 83 auto iter = copied_expected_.find(server_specifics.event_time_usec());
46 // We don't expect to encounter id matching events with different values, 84 // We don't expect to encounter id matching events with different values,
47 // this isn't going to recover so fail the test case now. 85 // this isn't going to recover so fail the test case now.
48 CHECK(expected_specifics_.end() != iter); 86 CHECK(copied_expected_.end() != iter);
49 // TODO(skym): This may need to change if we start updating navigation_id 87 // TODO(skym): This may need to change if we start updating navigation_id
50 // based on what sessions data is committed, and end up committing the 88 // based on what sessions data is committed, and end up committing the
51 // same event multiple times. 89 // same event multiple times.
52 EXPECT_EQ(iter->second.navigation_id(), server_specifics.navigation_id()); 90 EXPECT_EQ(iter->second.navigation_id(), server_specifics.navigation_id());
53 EXPECT_EQ(iter->second.event_case(), server_specifics.event_case()); 91 EXPECT_EQ(iter->second.event_case(), server_specifics.event_case());
92
93 copied_expected_.erase(iter);
54 } 94 }
55 95
56 return true; 96 return true;
57 } 97 }
58 98
59 std::string GetDebugMessage() const override { 99 std::string GetDebugMessage() const override {
60 return "Waiting server side USER_EVENTS to match expected."; 100 return "Waiting server side USER_EVENTS to match expected.";
61 } 101 }
62 102
63 private: 103 private:
64 FakeServer* fake_server_; 104 FakeServer* fake_server_;
65 std::map<int64_t, UserEventSpecifics> expected_specifics_; 105 std::multimap<int64_t, UserEventSpecifics> expected_specifics_;
66 }; 106 };
67 107
68 class SingleClientUserEventsSyncTest : public SyncTest { 108 class SingleClientUserEventsSyncTest : public SyncTest {
69 public: 109 public:
70 SingleClientUserEventsSyncTest() : SyncTest(SINGLE_CLIENT) { 110 SingleClientUserEventsSyncTest() : SyncTest(SINGLE_CLIENT) {
71 DisableVerifier(); 111 DisableVerifier();
72 } 112 }
73 113
74 private: 114 private:
75 DISALLOW_COPY_AND_ASSIGN(SingleClientUserEventsSyncTest); 115 DISALLOW_COPY_AND_ASSIGN(SingleClientUserEventsSyncTest);
76 }; 116 };
77 117
78 IN_PROC_BROWSER_TEST_F(SingleClientUserEventsSyncTest, Sanity) { 118 IN_PROC_BROWSER_TEST_F(SingleClientUserEventsSyncTest, Sanity) {
79 ASSERT_TRUE(SetupSync()); 119 ASSERT_TRUE(SetupSync());
80 EXPECT_EQ( 120 EXPECT_EQ(
81 0u, 121 0u,
82 GetFakeServer()->GetSyncEntitiesByModelType(syncer::USER_EVENTS).size()); 122 GetFakeServer()->GetSyncEntitiesByModelType(syncer::USER_EVENTS).size());
83 syncer::UserEventService* event_service = 123 syncer::UserEventService* event_service =
84 browser_sync::UserEventServiceFactory::GetForProfile(GetProfile(0)); 124 browser_sync::UserEventServiceFactory::GetForProfile(GetProfile(0));
85 UserEventSpecifics specifics1; 125 UserEventSpecifics specifics = CreateEvent(0);
86 specifics1.set_event_time_usec(base::Time::Now().ToInternalValue()); 126 event_service->RecordUserEvent(specifics);
87 specifics1.mutable_test_event(); 127 UserEventEqualityChecker(GetSyncService(0), GetFakeServer(), {specifics})
128 .Wait();
129 }
130
131 IN_PROC_BROWSER_TEST_F(SingleClientUserEventsSyncTest, RetrySequential) {
132 ASSERT_TRUE(SetupSync());
133 UserEventSpecifics specifics1 = CreateEvent(1);
134 UserEventSpecifics specifics2 = CreateEvent(2);
135 syncer::UserEventService* event_service =
136 browser_sync::UserEventServiceFactory::GetForProfile(GetProfile(0));
137
138 GetFakeServer()->OverrideResponseType(
139 base::Bind(&BounceType, CommitResponse::TRANSIENT_ERROR));
88 event_service->RecordUserEvent(specifics1); 140 event_service->RecordUserEvent(specifics1);
141
142 // This will block until we hit a TRANSIENT_ERROR, at which point we will
143 // regain control and can switch back to SUCCESS.
89 UserEventEqualityChecker(GetSyncService(0), GetFakeServer(), {specifics1}) 144 UserEventEqualityChecker(GetSyncService(0), GetFakeServer(), {specifics1})
90 .Wait(); 145 .Wait();
146 GetFakeServer()->OverrideResponseType(
147 base::Bind(&BounceType, CommitResponse::SUCCESS));
148 // Because the fake server records commits even on failure, we are able to
149 // verify that the commit for this event reached the server twice.
150 UserEventEqualityChecker(GetSyncService(0), GetFakeServer(),
151 {specifics1, specifics1})
152 .Wait();
153
154 // Only record |specifics2| after |specifics1| was successful to avoid race
155 // conditions.
156 event_service->RecordUserEvent(specifics2);
157 UserEventEqualityChecker(GetSyncService(0), GetFakeServer(),
158 {specifics1, specifics1, specifics2})
159 .Wait();
160 }
161
162 IN_PROC_BROWSER_TEST_F(SingleClientUserEventsSyncTest, RetryParallel) {
163 ASSERT_TRUE(SetupSync());
164 bool first = true;
165 UserEventSpecifics specifics1 = CreateEvent(1);
166 UserEventSpecifics specifics2 = CreateEvent(2);
167 UserEventSpecifics retry_specifics;
168
169 syncer::UserEventService* event_service =
170 browser_sync::UserEventServiceFactory::GetForProfile(GetProfile(0));
171
172 // We're not really sure if |specifics1| or |specifics2| is going to see the
173 // error, so record the one that does into |retry_specifics| and use it in
174 // expectations.
175 GetFakeServer()->OverrideResponseType(
176 base::Bind(&TransientErrorFirst, &first, &retry_specifics));
177
178 event_service->RecordUserEvent(specifics2);
179 event_service->RecordUserEvent(specifics1);
180 UserEventEqualityChecker(GetSyncService(0), GetFakeServer(),
181 {specifics1, specifics2})
182 .Wait();
183 UserEventEqualityChecker(GetSyncService(0), GetFakeServer(),
184 {specifics1, specifics2, retry_specifics})
185 .Wait();
91 } 186 }
92 187
93 } // namespace 188 } // namespace
OLDNEW
« no previous file with comments | « no previous file | components/sync/test/fake_server/fake_server.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698