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

Side by Side Diff: components/sync/test/fake_server/fake_server.h

Issue 2969643002: Reland - Replace FakeServer's implementation with LoopbackServer invocations. (Closed)
Patch Set: Rebased on master. 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
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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 #ifndef COMPONENTS_SYNC_TEST_FAKE_SERVER_FAKE_SERVER_H_ 5 #ifndef COMPONENTS_SYNC_TEST_FAKE_SERVER_FAKE_SERVER_H_
6 #define COMPONENTS_SYNC_TEST_FAKE_SERVER_FAKE_SERVER_H_ 6 #define COMPONENTS_SYNC_TEST_FAKE_SERVER_FAKE_SERVER_H_
7 7
8 #include <stdint.h> 8 #include <stdint.h>
9 9
10 #include <map> 10 #include <map>
11 #include <memory> 11 #include <memory>
12 #include <string> 12 #include <string>
13 #include <vector> 13 #include <vector>
14 14
15 #include "base/callback.h" 15 #include "base/callback.h"
16 #include "base/files/scoped_temp_dir.h"
16 #include "base/observer_list.h" 17 #include "base/observer_list.h"
17 #include "base/threading/thread_checker.h" 18 #include "base/threading/thread_checker.h"
18 #include "base/values.h" 19 #include "base/values.h"
19 #include "components/sync/base/model_type.h" 20 #include "components/sync/base/model_type.h"
21 #include "components/sync/engine_impl/loopback_server/loopback_server.h"
22 #include "components/sync/engine_impl/loopback_server/loopback_server_entity.h"
23 #include "components/sync/engine_impl/loopback_server/persistent_bookmark_entity .h"
24 #include "components/sync/engine_impl/loopback_server/persistent_tombstone_entit y.h"
25 #include "components/sync/engine_impl/loopback_server/persistent_unique_client_e ntity.h"
20 #include "components/sync/protocol/sync.pb.h" 26 #include "components/sync/protocol/sync.pb.h"
21 #include "components/sync/test/fake_server/fake_server_entity.h"
22 27
23 namespace fake_server { 28 namespace fake_server {
24 29
25 // A fake version of the Sync server used for testing. This class is not thread 30 // A fake version of the Sync server used for testing. This class is not thread
26 // safe. 31 // safe.
27 class FakeServer { 32 class FakeServer : public syncer::LoopbackServer::ObserverForTests {
28 public: 33 public:
29 class Observer { 34 class Observer {
30 public: 35 public:
31 virtual ~Observer() {} 36 virtual ~Observer() {}
32 37
33 // Called after FakeServer has processed a successful commit. The types 38 // Called after FakeServer has processed a successful commit. The types
34 // updated as part of the commit are passed in |committed_model_types|. 39 // updated as part of the commit are passed in |committed_model_types|.
35 virtual void OnCommit(const std::string& committer_id, 40 virtual void OnCommit(const std::string& committer_id,
36 syncer::ModelTypeSet committed_model_types) = 0; 41 syncer::ModelTypeSet committed_model_types) = 0;
37 }; 42 };
38 43
39 FakeServer(); 44 FakeServer();
40 virtual ~FakeServer(); 45 ~FakeServer() override;
41 46
42 // Handles a /command POST (with the given |request|) to the server. Three 47 // Handles a /command POST (with the given |request|) to the server. Three
43 // output arguments, |error_code|, |response_code|, and |response|, are used 48 // output arguments, |error_code|, |response_code|, and |response|, are used
44 // to pass data back to the caller. The command has failed if the value 49 // to pass data back to the caller. The command has failed if the value
45 // pointed to by |error_code| is nonzero. |completion_closure| will be called 50 // pointed to by |error_code| is nonzero. |completion_closure| will be called
46 // immediately before return. 51 // immediately before return.
47 void HandleCommand(const std::string& request, 52 void HandleCommand(const std::string& request,
48 const base::Closure& completion_closure, 53 const base::Closure& completion_closure,
49 int* error_code, 54 int* error_code,
50 int* response_code, 55 int* response_code,
51 std::string* response); 56 std::string* response);
52 57
53 // Helpers for fetching the last Commit or GetUpdates messages, respectively. 58 // Helpers for fetching the last Commit or GetUpdates messages, respectively.
54 // Returns true if the specified message existed, and false if no message has 59 // Returns true if the specified message existed, and false if no message has
55 // been received. 60 // been received.
56 bool GetLastCommitMessage(sync_pb::ClientToServerMessage* message); 61 bool GetLastCommitMessage(sync_pb::ClientToServerMessage* message);
57 bool GetLastGetUpdatesMessage(sync_pb::ClientToServerMessage* message); 62 bool GetLastGetUpdatesMessage(sync_pb::ClientToServerMessage* message);
58 63
59 // Creates a DicionaryValue representation of all entities present in the 64 // Creates a DicionaryValue representation of all entities present in the
60 // server. The dictionary keys are the strings generated by ModelTypeToString 65 // server. The dictionary keys are the strings generated by ModelTypeToString
61 // and the values are ListValues containing StringValue versions of entity 66 // and the values are ListValues containing StringValue versions of entity
62 // names. 67 // names.
63 std::unique_ptr<base::DictionaryValue> GetEntitiesAsDictionaryValue(); 68 std::unique_ptr<base::DictionaryValue> GetEntitiesAsDictionaryValue();
64 69
65 // Returns all entities stored by the server of the given |model_type|. 70 // Returns all entities stored by the server of the given |model_type|.
66 // This method returns SyncEntity protocol buffer objects (instead of 71 // This method returns SyncEntity protocol buffer objects (instead of
67 // FakeServerEntity) so that callers can inspect datatype-specific data 72 // LoopbackServerEntity) so that callers can inspect datatype-specific data
68 // (e.g., the URL of a session tab). 73 // (e.g., the URL of a session tab).
69 std::vector<sync_pb::SyncEntity> GetSyncEntitiesByModelType( 74 std::vector<sync_pb::SyncEntity> GetSyncEntitiesByModelType(
70 syncer::ModelType model_type); 75 syncer::ModelType model_type);
71 76
72 // Adds |entity| to the server's collection of entities. This method makes no 77 // Adds |entity| to the server's collection of entities. This method makes no
73 // guarantees that the added entity will result in successful server 78 // guarantees that the added entity will result in successful server
74 // operations. 79 // operations.
75 void InjectEntity(std::unique_ptr<FakeServerEntity> entity); 80 void InjectEntity(std::unique_ptr<syncer::LoopbackServerEntity> entity);
76 81
77 // Modifies the entity on the server with the given |id|. The entity's 82 // Modifies the entity on the server with the given |id|. The entity's
78 // EntitySpecifics are replaced with |updated_specifics| and its version is 83 // EntitySpecifics are replaced with |updated_specifics| and its version is
79 // updated. If the given |id| does not exist or the ModelType of 84 // updated. If the given |id| does not exist or the ModelType of
80 // |updated_specifics| does not match the entity, false is returned. 85 // |updated_specifics| does not match the entity, false is returned.
81 // Otherwise, true is returned to represent a successful modification. 86 // Otherwise, true is returned to represent a successful modification.
82 // 87 //
83 // This method sometimes updates entity data beyond EntitySpecifics. For 88 // This method sometimes updates entity data beyond EntitySpecifics. For
84 // example, in the case of a bookmark, changing the BookmarkSpecifics title 89 // example, in the case of a bookmark, changing the BookmarkSpecifics title
85 // field will modify the top-level entity's name field. 90 // field will modify the top-level entity's name field.
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
133 // must be called if AddObserver was ever called with |observer|. 138 // must be called if AddObserver was ever called with |observer|.
134 void RemoveObserver(Observer* observer); 139 void RemoveObserver(Observer* observer);
135 140
136 // Undoes the effects of DisableNetwork. 141 // Undoes the effects of DisableNetwork.
137 void EnableNetwork(); 142 void EnableNetwork();
138 143
139 // Forces every request to fail in a way that simulates a network failure. 144 // Forces every request to fail in a way that simulates a network failure.
140 // This can be used to trigger exponential backoff in the client. 145 // This can be used to trigger exponential backoff in the client.
141 void DisableNetwork(); 146 void DisableNetwork();
142 147
143 // Returns the entity ID of the Bookmark Bar folder. 148 // Implement LoopbackServer::ObserverForTests:
144 std::string GetBookmarkBarFolderId() const; 149 void OnCommit(const std::string& committer_id,
150 syncer::ModelTypeSet committed_model_types) override;
145 151
146 // Returns the current FakeServer as a WeakPtr. 152 // Returns the current FakeServer as a WeakPtr.
147 base::WeakPtr<FakeServer> AsWeakPtr(); 153 base::WeakPtr<FakeServer> AsWeakPtr();
148 154
149 using ResponseTypeProvider =
150 base::RepeatingCallback<sync_pb::CommitResponse::ResponseType(
151 const FakeServerEntity& entity)>;
152
153 // Use this callback to generate response types for entities. They will still 155 // Use this callback to generate response types for entities. They will still
154 // be "committed" and stored as normal, this only affects the response type 156 // be "committed" and stored as normal, this only affects the response type
155 // the client sees. This allows tests to still inspect what the client has 157 // the client sees. This allows tests to still inspect what the client has
156 // done, although not as useful of a mechanism for multi client tests. Care 158 // done, although not as useful of a mechanism for multi client tests. Care
157 // should be taken when failing responses, as the client will go into 159 // should be taken when failing responses, as the client will go into
158 // exponential backoff, which can cause tests to be slow or time out. 160 // exponential backoff, which can cause tests to be slow or time out.
159 void OverrideResponseType(ResponseTypeProvider response_type_override); 161 void OverrideResponseType(
162 syncer::LoopbackServer::ResponseTypeProvider response_type_override);
160 163
161 private: 164 private:
162 using EntityMap = std::map<std::string, std::unique_ptr<FakeServerEntity>>;
163
164 // Gets FakeServer ready for syncing.
165 void Init();
166
167 // Processes a GetUpdates call.
168 bool HandleGetUpdatesRequest(const sync_pb::GetUpdatesMessage& get_updates,
169 sync_pb::GetUpdatesResponse* response);
170
171 // Processes a Commit call.
172 bool HandleCommitRequest(const sync_pb::CommitMessage& message,
173 const std::string& invalidator_client_id,
174 sync_pb::CommitResponse* response);
175
176 // Creates and saves a permanent folder for Bookmarks (e.g., Bookmark Bar).
177 bool CreatePermanentBookmarkFolder(const std::string& server_tag,
178 const std::string& name);
179
180 // Inserts the default permanent items in |entities_|.
181 bool CreateDefaultPermanentItems();
182
183 // Saves a |entity| to |entities_|.
184 void SaveEntity(std::unique_ptr<FakeServerEntity> entity);
185
186 // Commits a client-side SyncEntity to the server as a FakeServerEntity.
187 // The client that sent the commit is identified via |client_guid|. The
188 // parent ID string present in |client_entity| should be ignored in favor
189 // of |parent_id|. If the commit is successful, the entity's server ID string
190 // is returned and a new FakeServerEntity is saved in |entities_|.
191 std::string CommitEntity(
192 const sync_pb::SyncEntity& client_entity,
193 sync_pb::CommitResponse_EntryResponse* entry_response,
194 const std::string& client_guid,
195 const std::string& parent_id);
196
197 // Populates |entry_response| based on the stored entity identified by
198 // |entity_id|. It is assumed that the entity identified by |entity_id| has
199 // already been stored using SaveEntity.
200 void BuildEntryResponseForSuccessfulCommit(
201 const std::string& entity_id,
202 sync_pb::CommitResponse_EntryResponse* entry_response);
203
204 // Determines whether the SyncEntity with id_string |id| is a child of an
205 // entity with id_string |potential_parent_id|.
206 bool IsChild(const std::string& id, const std::string& potential_parent_id);
207
208 // Creates and saves tombstones for all children of the entity with the given
209 // |id|. A tombstone is not created for the entity itself.
210 void DeleteChildren(const std::string& id);
211
212 // Returns whether a triggered error should be sent for the request. 165 // Returns whether a triggered error should be sent for the request.
213 bool ShouldSendTriggeredError() const; 166 bool ShouldSendTriggeredError() const;
214 167
215 // Updates the |entity| to a new version and increments the version counter
216 // that the server uses to assign versions.
217 void UpdateEntityVersion(FakeServerEntity* entity);
218
219 // Returns the store birthday.
220 std::string GetStoreBirthday() const;
221
222 // This is the last version number assigned to an entity. The next entity will
223 // have a version number of version_ + 1.
224 int64_t version_;
225
226 // The current store birthday value.
227 int64_t store_birthday_;
228
229 // Whether the server should act as if incoming connections are properly 168 // Whether the server should act as if incoming connections are properly
230 // authenticated. 169 // authenticated.
231 bool authenticated_; 170 bool authenticated_;
232 171
233 // All SyncEntity objects saved by the server. The key value is the entity's
234 // id string.
235 EntityMap entities_;
236
237 // All Keystore keys known to the server. 172 // All Keystore keys known to the server.
238 std::vector<std::string> keystore_keys_; 173 std::vector<std::string> keystore_keys_;
239 174
240 // Used as the error_code field of ClientToServerResponse on all responses 175 // Used as the error_code field of ClientToServerResponse on all responses
241 // except when |triggered_actionable_error_| is set. 176 // except when |triggered_actionable_error_| is set.
242 sync_pb::SyncEnums::ErrorType error_type_; 177 sync_pb::SyncEnums::ErrorType error_type_;
243 178
244 // Used as the error field of ClientToServerResponse when its pointer is not 179 // Used as the error field of ClientToServerResponse when its pointer is not
245 // null. 180 // null.
246 std::unique_ptr<sync_pb::ClientToServerResponse_Error> 181 std::unique_ptr<sync_pb::ClientToServerResponse_Error>
(...skipping 13 matching lines...) Expand all
260 base::ObserverList<Observer, true> observers_; 195 base::ObserverList<Observer, true> observers_;
261 196
262 // When true, the server operates normally. When false, a failure is returned 197 // When true, the server operates normally. When false, a failure is returned
263 // on every request. This is used to simulate a network failure on the client. 198 // on every request. This is used to simulate a network failure on the client.
264 bool network_enabled_; 199 bool network_enabled_;
265 200
266 // The last received client to server messages. 201 // The last received client to server messages.
267 sync_pb::ClientToServerMessage last_commit_message_; 202 sync_pb::ClientToServerMessage last_commit_message_;
268 sync_pb::ClientToServerMessage last_getupdates_message_; 203 sync_pb::ClientToServerMessage last_getupdates_message_;
269 204
270 ResponseTypeProvider response_type_override_;
271
272 // Used to verify that FakeServer is only used from one thread. 205 // Used to verify that FakeServer is only used from one thread.
273 base::ThreadChecker thread_checker_; 206 base::ThreadChecker thread_checker_;
274 207
208 std::unique_ptr<syncer::LoopbackServer> loopback_server_;
209 std::unique_ptr<base::ScopedTempDir> loopback_server_storage_;
210
275 // Creates WeakPtr versions of the current FakeServer. This must be the last 211 // Creates WeakPtr versions of the current FakeServer. This must be the last
276 // data member! 212 // data member!
277 base::WeakPtrFactory<FakeServer> weak_ptr_factory_; 213 base::WeakPtrFactory<FakeServer> weak_ptr_factory_;
278 }; 214 };
279 215
280 } // namespace fake_server 216 } // namespace fake_server
281 217
282 #endif // COMPONENTS_SYNC_TEST_FAKE_SERVER_FAKE_SERVER_H_ 218 #endif // COMPONENTS_SYNC_TEST_FAKE_SERVER_FAKE_SERVER_H_
OLDNEW
« no previous file with comments | « components/sync/test/fake_server/bookmark_entity_builder.cc ('k') | components/sync/test/fake_server/fake_server.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698