1 /* -*- Mode: C++; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /*
3  *     Copyright 2016 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 
18 #pragma once
19 
20 #include "dcp/consumer.h"
21 
22 #include <boost/optional/optional_fwd.hpp>
23 
24 /*
25  * Mock of the DcpConsumer class.  Wraps the real DcpConsumer class
26  * and provides get/set access to lastMessageTime.
27  */
28 class MockDcpConsumer: public DcpConsumer {
29 public:
30     MockDcpConsumer(EventuallyPersistentEngine& theEngine,
31                     const void* cookie,
32                     const std::string& name,
33                     const std::string& consumerName = {});
34 
setPendingAddStream(bool value)35     void setPendingAddStream(bool value) {
36         // The unit tests was written before memcached marked the
37         // connection as DCP as part of a successfull DCP Open (and wouldn't
38         // call step() before add stream was received.
39         //
40         // Some of the tests we've got just want to verify other things
41         // so let's allow them to fake that the message was received
42         pendingAddStream = value;
43     }
44 
setLastMessageTime(const rel_time_t timeValue)45     void setLastMessageTime(const rel_time_t timeValue) {
46         lastMessageTime = timeValue;
47     }
48 
getLastMessageTime()49     rel_time_t getLastMessageTime() {
50         return lastMessageTime;
51     }
52 
getVbucketStream(Vbid vbid)53     std::shared_ptr<PassiveStream> getVbucketStream(Vbid vbid) {
54         return findStream(vbid);
55     }
56 
public_notifyVbucketReady(Vbid vbid)57     void public_notifyVbucketReady(Vbid vbid) {
58         notifyVbucketReady(vbid);
59     }
60 
getNumBackoffs() const61     uint32_t getNumBackoffs() const {
62         return backoffs.load();
63     }
64 
getGetErrorMapState()65     GetErrorMapState getGetErrorMapState() {
66         return getErrorMapState;
67     }
68 
getProducerIsVersion5orHigher()69     bool getProducerIsVersion5orHigher() {
70         return producerIsVersion5orHigher;
71     }
72 
getFlowControl()73     FlowControl& getFlowControl() {
74         return flowControl;
75     }
76 
77     /*
78      * Creates a PassiveStream.
79      * @return a SingleThreadedRCPtr to the newly created MockPassiveStream.
80      */
81     std::shared_ptr<PassiveStream> makePassiveStream(
82             EventuallyPersistentEngine& e,
83             std::shared_ptr<DcpConsumer> consumer,
84             const std::string& name,
85             uint32_t flags,
86             uint32_t opaque,
87             Vbid vb,
88             uint64_t start_seqno,
89             uint64_t end_seqno,
90             uint64_t vb_uuid,
91             uint64_t snap_start_seqno,
92             uint64_t snap_end_seqno,
93             uint64_t vb_high_seqno,
94             const Collections::ManifestUid vb_manifest_uid) override;
95 
96     /**
97      * @return the opaque sent to the Producer as part of the DCP_CONTROL
98      *     request for the Sync Replication negotiation
99      */
public_getSyncReplNegotiationOpaque() const100     uint32_t public_getSyncReplNegotiationOpaque() const {
101         return syncReplNegotiation.opaque;
102     }
103 
104     /**
105      * @return the entire SyncReplNegotiation struct used to send DCP_CONTROL
106      *         messages for testing.
107      */
public_getSyncReplNegotiation() const108     BlockingDcpControlNegotiation public_getSyncReplNegotiation() const {
109         return syncReplNegotiation;
110     }
111 
public_getPendingSendConsumerName() const112     bool public_getPendingSendConsumerName() const {
113         return pendingSendConsumerName;
114     }
115 
116     /**
117      * Used for simulating a successful Consumer-Producer Sync Repl handshake.
118      */
enableSyncReplication()119     void enableSyncReplication() {
120         supportsSyncReplication = SyncReplication::SyncReplication;
121     }
122 
123     /**
124      * Disable SyncRepl for testing
125      */
disableSyncReplication()126     void disableSyncReplication() {
127         supportsSyncReplication = SyncReplication::No;
128     }
129 
130     /**
131      *
132      * Map from the opaque used to create a stream to the internal opaque
133      * as is used by streamEnd
134      *
135      * DcpConsumer maintains an internal opaque map; rather than assuming
136      * the value that will be used for the opaque of a given stream, use this
137      * map to get the correct value.
138      */
139     boost::optional<uint32_t> getStreamOpaque(uint32_t opaque);
140 
public_streamAccepted(uint32_t opaque, cb::mcbp::Status status, const uint8_t* body, uint32_t bodylen)141     void public_streamAccepted(uint32_t opaque,
142                                cb::mcbp::Status status,
143                                const uint8_t* body,
144                                uint32_t bodylen) {
145         streamAccepted(opaque, status, body, bodylen);
146     }
147 
public_getDeletedUserXattrsNegotiation() const148     BlockingDcpControlNegotiation public_getDeletedUserXattrsNegotiation()
149             const {
150         return deletedUserXattrsNegotiation;
151     }
152 
public_getIncludeDeletedUserXattrs() const153     IncludeDeletedUserXattrs public_getIncludeDeletedUserXattrs() const {
154         return includeDeletedUserXattrs;
155     }
156 
public_setIncludeDeletedUserXattrs(IncludeDeletedUserXattrs value)157     void public_setIncludeDeletedUserXattrs(IncludeDeletedUserXattrs value) {
158         includeDeletedUserXattrs = value;
159     }
160 };
161