OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef SYNC_TEST_ENGINE_MOCK_MODEL_TYPE_PROCESSOR_H_ | |
6 #define SYNC_TEST_ENGINE_MOCK_MODEL_TYPE_PROCESSOR_H_ | |
7 | |
8 #include <stddef.h> | |
9 #include <stdint.h> | |
10 | |
11 #include <map> | |
12 #include <string> | |
13 #include <vector> | |
14 | |
15 #include "base/callback.h" | |
16 #include "base/macros.h" | |
17 #include "sync/internal_api/public/model_type_processor.h" | |
18 #include "sync/internal_api/public/non_blocking_sync_common.h" | |
19 #include "sync/protocol/data_type_state.pb.h" | |
20 | |
21 namespace syncer_v2 { | |
22 | |
23 // Mocks the ModelTypeProcessor. | |
24 // | |
25 // This mock is made simpler by not using any threads. It does still have the | |
26 // ability to defer execution if we need to test race conditions, though. | |
27 // | |
28 // It maintains some state to try to make its behavior more realistic. It | |
29 // updates this state as it creates commit requests or receives update and | |
30 // commit responses. | |
31 // | |
32 // It keeps a log of all received messages so tests can make assertions based | |
33 // on their value. | |
34 class MockModelTypeProcessor : public ModelTypeProcessor { | |
35 public: | |
36 typedef base::Callback<void()> DisconnectCallback; | |
37 | |
38 MockModelTypeProcessor(); | |
39 ~MockModelTypeProcessor() override; | |
40 | |
41 // Implementation of ModelTypeProcessor. | |
42 void ConnectSync(std::unique_ptr<CommitQueue> commit_queue) override; | |
43 void DisconnectSync() override; | |
44 void OnCommitCompleted(const sync_pb::DataTypeState& type_state, | |
45 const CommitResponseDataList& response_list) override; | |
46 void OnUpdateReceived(const sync_pb::DataTypeState& type_state, | |
47 const UpdateResponseDataList& response_list) override; | |
48 | |
49 // By default, this object behaves as if all messages are processed | |
50 // immediately. Sometimes it is useful to defer work until later, as might | |
51 // happen in the real world if the model thread's task queue gets backed up. | |
52 void SetSynchronousExecution(bool is_synchronous); | |
53 | |
54 // Runs any work that was deferred while this class was in asynchronous mode. | |
55 // | |
56 // This function is not useful unless this object is set to synchronous | |
57 // execution mode, which is the default. | |
58 void RunQueuedTasks(); | |
59 | |
60 // Generate commit or deletion requests to be sent to the server. | |
61 // These functions update local state to keep sequence numbers consistent. | |
62 // | |
63 // A real ModelTypeProcessor would forward these kinds of messages | |
64 // directly to its attached CommitQueue. These methods | |
65 // return the value to the caller so the test framework can handle them as it | |
66 // sees fit. | |
67 CommitRequestData CommitRequest(const std::string& tag_hash, | |
68 const sync_pb::EntitySpecifics& specifics); | |
69 CommitRequestData DeleteRequest(const std::string& tag_hash); | |
70 | |
71 // Getters to access the log of received update responses. | |
72 // | |
73 // Does not includes repsonses that are in pending tasks. | |
74 size_t GetNumUpdateResponses() const; | |
75 UpdateResponseDataList GetNthUpdateResponse(size_t n) const; | |
76 sync_pb::DataTypeState GetNthTypeStateReceivedInUpdateResponse( | |
77 size_t n) const; | |
78 | |
79 // Getters to access the log of received commit responses. | |
80 // | |
81 // Does not includes responses that are in pending tasks. | |
82 size_t GetNumCommitResponses() const; | |
83 CommitResponseDataList GetNthCommitResponse(size_t n) const; | |
84 sync_pb::DataTypeState GetNthTypeStateReceivedInCommitResponse( | |
85 size_t n) const; | |
86 | |
87 // Getters to access the lastest update response for a given tag_hash. | |
88 bool HasUpdateResponse(const std::string& tag_hash) const; | |
89 UpdateResponseData GetUpdateResponse(const std::string& tag_hash) const; | |
90 | |
91 // Getters to access the lastest commit response for a given tag_hash. | |
92 bool HasCommitResponse(const std::string& tag_hash) const; | |
93 CommitResponseData GetCommitResponse(const std::string& tag_hash) const; | |
94 | |
95 void SetDisconnectCallback(const DisconnectCallback& callback); | |
96 | |
97 private: | |
98 // Process a received commit response. | |
99 // | |
100 // Implemented as an Impl method so we can defer its execution in some cases. | |
101 void OnCommitCompletedImpl(const sync_pb::DataTypeState& type_state, | |
102 const CommitResponseDataList& response_list); | |
103 | |
104 // Process a received update response. | |
105 // | |
106 // Implemented as an Impl method so we can defer its execution in some cases. | |
107 void OnUpdateReceivedImpl(const sync_pb::DataTypeState& type_state, | |
108 const UpdateResponseDataList& response_list); | |
109 | |
110 // Getter and setter for per-item sequence number tracking. | |
111 int64_t GetCurrentSequenceNumber(const std::string& tag_hash) const; | |
112 int64_t GetNextSequenceNumber(const std::string& tag_hash); | |
113 | |
114 // Getter and setter for per-item base version tracking. | |
115 int64_t GetBaseVersion(const std::string& tag_hash) const; | |
116 void SetBaseVersion(const std::string& tag_hash, int64_t version); | |
117 | |
118 // Getters and setter for server-assigned ID values. | |
119 bool HasServerAssignedId(const std::string& tag_hash) const; | |
120 const std::string& GetServerAssignedId(const std::string& tag_hash) const; | |
121 void SetServerAssignedId(const std::string& tag_hash, const std::string& id); | |
122 | |
123 // State related to the implementation of deferred work. | |
124 // See SetSynchronousExecution() for details. | |
125 bool is_synchronous_; | |
126 std::vector<base::Closure> pending_tasks_; | |
127 std::unique_ptr<CommitQueue> commit_queue_; | |
128 | |
129 // A log of messages received by this object. | |
130 std::vector<CommitResponseDataList> received_commit_responses_; | |
131 std::vector<UpdateResponseDataList> received_update_responses_; | |
132 std::vector<sync_pb::DataTypeState> type_states_received_on_update_; | |
133 std::vector<sync_pb::DataTypeState> type_states_received_on_commit_; | |
134 | |
135 // Latest responses received, indexed by tag_hash. | |
136 std::map<const std::string, CommitResponseData> commit_response_items_; | |
137 std::map<const std::string, UpdateResponseData> update_response_items_; | |
138 | |
139 // The per-item state maps. | |
140 std::map<const std::string, int64_t> sequence_numbers_; | |
141 std::map<const std::string, int64_t> base_versions_; | |
142 std::map<const std::string, std::string> assigned_ids_; | |
143 | |
144 // Callback which will be call during disconnection | |
145 DisconnectCallback disconnect_callback_; | |
146 | |
147 DISALLOW_COPY_AND_ASSIGN(MockModelTypeProcessor); | |
148 }; | |
149 | |
150 } // namespace syncer_v2 | |
151 | |
152 #endif // SYNC_TEST_ENGINE_MOCK_MODEL_TYPE_PROCESSOR_H_ | |
OLD | NEW |