1 /* -*- Mode: C++; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /*
3  *     Copyright 2019 Couchbase, Inc
4  *
5  *   Licensed under the Apache License, Version 2.0 (the "License");
6  *   you may not use this file except in compliance with the License.
7  *   You may obtain a copy of the License at
8  *
9  *       http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *   Unless required by applicable law or agreed to in writing, software
12  *   distributed under the License is distributed on an "AS IS" BASIS,
13  *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *   See the License for the specific language governing permissions and
15  *   limitations under the License.
16  */
17 #pragma once
18 
19 #include <folly/portability/GMock.h>
20 #include <memcached/dcp.h>
21 #include <memcached/durability_spec.h>
22 #include <memcached/engine.h>
23 
24 class Item;
25 
26 /**
27  * Mock of dcp_messsage_producers, using GoogleMock to mock.
28  */
29 class GMockDcpMsgProducers : public dcp_message_producers {
30 public:
31     MOCK_METHOD2(get_failover_log,
32                  ENGINE_ERROR_CODE(uint32_t opaque, Vbid vbucket));
33 
34     MOCK_METHOD9(stream_req,
35                  ENGINE_ERROR_CODE(uint32_t opaque,
36                                    Vbid vbucket,
37                                    uint32_t flags,
38                                    uint64_t start_seqno,
39                                    uint64_t end_seqno,
40                                    uint64_t vbucket_uuid,
41                                    uint64_t snap_start_seqno,
42                                    uint64_t snap_end_seqno,
43                                    const std::string& request_value));
44 
45     MOCK_METHOD3(add_stream_rsp,
46                  ENGINE_ERROR_CODE(uint32_t opaque,
47                                    uint32_t stream_opaque,
48                                    cb::mcbp::Status status));
49 
50     MOCK_METHOD2(marker_rsp,
51                  ENGINE_ERROR_CODE(uint32_t opaque, cb::mcbp::Status status));
52 
53     MOCK_METHOD2(set_vbucket_state_rsp,
54                  ENGINE_ERROR_CODE(uint32_t opaque, cb::mcbp::Status status));
55 
56     MOCK_METHOD4(stream_end,
57                  ENGINE_ERROR_CODE(uint32_t opaque,
58                                    Vbid vbucket,
59                                    uint32_t flags,
60                                    cb::mcbp::DcpStreamId sid));
61 
62     MOCK_METHOD8(
63             marker,
64             ENGINE_ERROR_CODE(uint32_t opaque,
65                               Vbid vbucket,
66                               uint64_t start_seqno,
67                               uint64_t end_seqno,
68                               uint32_t flags,
69                               boost::optional<uint64_t> high_completed_seqno,
70                               boost::optional<uint64_t> maxVisibleSeqno,
71                               cb::mcbp::DcpStreamId sid));
72 
73     MOCK_METHOD8(mutation,
74                  ENGINE_ERROR_CODE(uint32_t opaque,
75                                    Item* itm,
76                                    Vbid vbucket,
77                                    uint64_t by_seqno,
78                                    uint64_t rev_seqno,
79                                    uint32_t lock_time,
80                                    uint8_t nru,
81                                    cb::mcbp::DcpStreamId sid));
82 
83     MOCK_METHOD6(deletion,
84                  ENGINE_ERROR_CODE(uint32_t opaque,
85                                    Item* itm,
86                                    Vbid vbucket,
87                                    uint64_t by_seqno,
88                                    uint64_t rev_seqno,
89                                    cb::mcbp::DcpStreamId sid));
90 
91     MOCK_METHOD7(deletionV2,
92                  ENGINE_ERROR_CODE(uint32_t opaque,
93                                    Item* itm,
94                                    Vbid vbucket,
95                                    uint64_t by_seqno,
96                                    uint64_t rev_seqno,
97                                    uint32_t delete_time,
98                                    cb::mcbp::DcpStreamId sid));
99 
100     MOCK_METHOD7(expiration,
101                  ENGINE_ERROR_CODE(uint32_t opaque,
102                                    Item* itm,
103                                    Vbid vbucket,
104                                    uint64_t by_seqno,
105                                    uint64_t rev_seqno,
106                                    uint32_t delete_time,
107                                    cb::mcbp::DcpStreamId sid));
108 
109     MOCK_METHOD3(set_vbucket_state,
110                  ENGINE_ERROR_CODE(uint32_t opaque,
111                                    Vbid vbucket,
112                                    vbucket_state_t state));
113 
114     MOCK_METHOD1(noop, ENGINE_ERROR_CODE(uint32_t opaque));
115 
116     MOCK_METHOD3(buffer_acknowledgement,
117                  ENGINE_ERROR_CODE(uint32_t opaque,
118                                    Vbid vbucket,
119                                    uint32_t buffer_bytes));
120 
121     MOCK_METHOD3(control,
122                  ENGINE_ERROR_CODE(uint32_t opaque,
123                                    cb::const_char_buffer key,
124                                    cb::const_char_buffer value));
125 
126     MOCK_METHOD8(system_event,
127                  ENGINE_ERROR_CODE(uint32_t opaque,
128                                    Vbid vbucket,
129                                    mcbp::systemevent::id event,
130                                    uint64_t bySeqno,
131                                    mcbp::systemevent::version version,
132                                    cb::const_byte_buffer key,
133                                    cb::const_byte_buffer eventData,
134                                    cb::mcbp::DcpStreamId sid));
135 
136     MOCK_METHOD2(get_error_map,
137                  ENGINE_ERROR_CODE(uint32_t opaque, uint16_t version));
138 
139     MOCK_METHOD9(prepare,
140                  ENGINE_ERROR_CODE(uint32_t opaque,
141                                    Item* itm,
142                                    Vbid vbucket,
143                                    uint64_t by_seqno,
144                                    uint64_t rev_seqno,
145                                    uint32_t lock_time,
146                                    uint8_t nru,
147                                    DocumentState document_state,
148                                    cb::durability::Level level));
149 
150     MOCK_METHOD3(seqno_acknowledged,
151                  ENGINE_ERROR_CODE(uint32_t opaque,
152                                    Vbid vbucket,
153                                    uint64_t prepared_seqno));
154 
155     MOCK_METHOD5(commit,
156                  ENGINE_ERROR_CODE(uint32_t opaque,
157                                    Vbid vbucket,
158                                    const DocKey& key,
159                                    uint64_t prepare_seqno,
160                                    uint64_t commit_seqno));
161 
162     MOCK_METHOD5(abort,
163                  ENGINE_ERROR_CODE(uint32_t opaque,
164                                    Vbid vbucket,
165                                    const DocKey& key,
166                                    uint64_t prepared_seqno,
167                                    uint64_t abort_seqno));
168 
169     // Current version of GMock doesn't support move-only types (e.g.
170     // std::unique_ptr) for mocked function arguments. Workaround directly
171     // implementing the affected methods (without GMock) and have them delegate
172     // to a GMock method which takes a raw Item ptr.
mutation(uint32_t opaque,cb::unique_item_ptr itm,Vbid vbucket,uint64_t by_seqno,uint64_t rev_seqno,uint32_t lock_time,uint8_t nru,cb::mcbp::DcpStreamId sid)173     ENGINE_ERROR_CODE mutation(uint32_t opaque,
174                                cb::unique_item_ptr itm,
175                                Vbid vbucket,
176                                uint64_t by_seqno,
177                                uint64_t rev_seqno,
178                                uint32_t lock_time,
179                                uint8_t nru,
180                                cb::mcbp::DcpStreamId sid) {
181         return mutation(opaque,
182                         reinterpret_cast<Item*>(itm.get()),
183                         vbucket,
184                         by_seqno,
185                         rev_seqno,
186                         lock_time,
187                         nru,
188                         sid);
189     }
190 
deletion(uint32_t opaque,cb::unique_item_ptr itm,Vbid vbucket,uint64_t by_seqno,uint64_t rev_seqno,cb::mcbp::DcpStreamId sid)191     ENGINE_ERROR_CODE deletion(uint32_t opaque,
192                                cb::unique_item_ptr itm,
193                                Vbid vbucket,
194                                uint64_t by_seqno,
195                                uint64_t rev_seqno,
196                                cb::mcbp::DcpStreamId sid) {
197         return deletion(opaque,
198                         reinterpret_cast<Item*>(itm.get()),
199                         vbucket,
200                         by_seqno,
201                         rev_seqno,
202                         sid);
203     }
204 
deletion_v2(uint32_t opaque,cb::unique_item_ptr itm,Vbid vbucket,uint64_t by_seqno,uint64_t rev_seqno,uint32_t delete_time,cb::mcbp::DcpStreamId sid)205     ENGINE_ERROR_CODE deletion_v2(uint32_t opaque,
206                                   cb::unique_item_ptr itm,
207                                   Vbid vbucket,
208                                   uint64_t by_seqno,
209                                   uint64_t rev_seqno,
210                                   uint32_t delete_time,
211                                   cb::mcbp::DcpStreamId sid) {
212         return deletionV2(opaque,
213                           reinterpret_cast<Item*>(itm.get()),
214                           vbucket,
215                           by_seqno,
216                           rev_seqno,
217                           delete_time,
218                           sid);
219     }
220 
expiration(uint32_t opaque,cb::unique_item_ptr itm,Vbid vbucket,uint64_t by_seqno,uint64_t rev_seqno,uint32_t delete_time,cb::mcbp::DcpStreamId sid)221     ENGINE_ERROR_CODE expiration(uint32_t opaque,
222                                  cb::unique_item_ptr itm,
223                                  Vbid vbucket,
224                                  uint64_t by_seqno,
225                                  uint64_t rev_seqno,
226                                  uint32_t delete_time,
227                                  cb::mcbp::DcpStreamId sid) {
228         return expiration(opaque,
229                           reinterpret_cast<Item*>(itm.get()),
230                           vbucket,
231                           by_seqno,
232                           rev_seqno,
233                           delete_time,
234                           sid);
235     }
236 
prepare(uint32_t opaque,cb::unique_item_ptr itm,Vbid vbucket,uint64_t by_seqno,uint64_t rev_seqno,uint32_t lock_time,uint8_t nru,DocumentState document_state,cb::durability::Level level)237     ENGINE_ERROR_CODE prepare(uint32_t opaque,
238                               cb::unique_item_ptr itm,
239                               Vbid vbucket,
240                               uint64_t by_seqno,
241                               uint64_t rev_seqno,
242                               uint32_t lock_time,
243                               uint8_t nru,
244                               DocumentState document_state,
245                               cb::durability::Level level) {
246         return prepare(opaque,
247                        reinterpret_cast<Item*>(itm.get()),
248                        vbucket,
249                        by_seqno,
250                        rev_seqno,
251                        lock_time,
252                        nru,
253                        document_state,
254                        level);
255     }
256 };
257