1
/* Copyright 2012 10gen Inc.
3
* Licensed under the Apache License, Version 2.0 (the "License");
4
* you may not use this file except in compliance with the License.
5
* You may obtain a copy of the License at
7
* http://www.apache.org/licenses/LICENSE-2.0
9
* Unless required by applicable law or agreed to in writing, software
10
* distributed under the License is distributed on an "AS IS" BASIS,
11
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
* See the License for the specific language governing permissions and
13
* limitations under the License.
18
#include <boost/shared_ptr.hpp>
22
#include "mongo/client/dbclientinterface.h"
23
#include "mongo/dbtests/mock/mock_remote_db_server.h"
27
* A simple class for mocking mongo::DBClientConnection.
29
* Also check out sample usage in dbtests/mock_dbclient_conn_test.cpp
31
class MockDBClientConnection : public mongo::DBClientConnection {
34
* Create a mock connection to a mock server.
36
* @param remoteServer the remote server to connect to. The caller is
37
* responsible for making sure that the life of remoteServer is
38
* longer than this connection.
39
* @param autoReconnect will automatically re-establish connection the
40
* next time an operation is requested when the last operation caused
41
* this connection to fall into a failed state.
43
MockDBClientConnection(MockRemoteDBServer* remoteServer, bool autoReconnect = false);
44
virtual ~MockDBClientConnection();
47
// DBClientBase methods
50
bool connect(const char* hostName, std::string& errmsg);
52
inline bool connect(const HostAndPort& host, std::string& errmsg) {
53
return connect(host.toString().c_str(), errmsg);
56
bool runCommand(const std::string& dbname, const mongo::BSONObj& cmdObj,
57
mongo::BSONObj &info, int options = 0);
59
std::auto_ptr<mongo::DBClientCursor> query(const std::string &ns,
60
mongo::Query query = mongo::Query(),
63
const mongo::BSONObj* fieldsToReturn = 0,
67
uint64_t getSockCreationMicroSec() const;
69
virtual void insert(const string& ns, BSONObj obj, int flags = 0);
71
virtual void insert(const string& ns, const vector<BSONObj>& objList, int flags = 0);
73
virtual void remove(const string& ns, Query query, bool justOne = false);
75
virtual void remove(const string& ns, Query query, int flags = 0);
81
mongo::ConnectionString::ConnectionType type() const;
82
bool isFailed() const;
83
double getSoTimeout() const;
84
std::string getServerAddress() const;
85
std::string toString();
88
// Unsupported methods (defined to get rid of virtual function was hidden error)
90
unsigned long long query(boost::function<void(const mongo::BSONObj&)> f,
91
const std::string& ns, mongo::Query query,
92
const mongo::BSONObj* fieldsToReturn = 0, int queryOptions = 0);
94
unsigned long long query(boost::function<void(mongo::DBClientCursorBatchIterator&)> f,
95
const std::string& ns, mongo::Query query,
96
const mongo::BSONObj* fieldsToReturn = 0,
97
int queryOptions = 0);
100
// Unsupported methods (these are pure virtuals in the base class)
103
void killCursor(long long cursorID);
104
bool callRead(mongo::Message& toSend , mongo::Message& response);
105
bool call(mongo::Message& toSend, mongo::Message& response, bool assertOk = true,
106
std::string* actualServer = 0);
107
void say(mongo::Message& toSend, bool isRetry = false, std::string* actualServer = 0);
108
void sayPiggyBack(mongo::Message& toSend);
109
bool lazySupported() const;
112
void checkConnection();
114
MockRemoteDBServer::InstanceID _remoteServerInstanceID;
115
MockRemoteDBServer* _remoteServer;
117
uint64_t _sockCreationTime;