2008-12-05 22:03:35 +01:00
|
|
|
// mockdbclient.h - mocked out client for testing.
|
|
|
|
|
|
|
|
/**
|
2008-12-08 02:58:20 +01:00
|
|
|
* Copyright (C) 2008 10gen Inc.
|
2008-12-29 02:28:49 +01:00
|
|
|
*
|
2008-12-08 02:58:20 +01:00
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License, version 3,
|
|
|
|
* as published by the Free Software Foundation.
|
2008-12-29 02:28:49 +01:00
|
|
|
*
|
2008-12-08 02:58:20 +01:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
2008-12-29 02:28:49 +01:00
|
|
|
*
|
2008-12-08 02:58:20 +01:00
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2008-12-05 22:03:35 +01:00
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "../client/dbclient.h"
|
|
|
|
#include "../db/commands.h"
|
2010-04-22 01:32:36 +02:00
|
|
|
#include "../db/replpair.h"
|
2008-12-05 22:03:35 +01:00
|
|
|
|
|
|
|
class MockDBClientConnection : public DBClientConnection {
|
2008-12-08 02:58:20 +01:00
|
|
|
public:
|
2008-12-29 02:28:49 +01:00
|
|
|
MockDBClientConnection() : connect_() {}
|
|
|
|
virtual
|
2009-04-30 22:25:36 +02:00
|
|
|
BSONObj findOne(const string &ns, Query query, const BSONObj *fieldsToReturn = 0, int queryOptions = 0) {
|
2008-12-29 02:28:49 +01:00
|
|
|
return one_;
|
|
|
|
}
|
|
|
|
virtual
|
2009-03-24 21:58:33 +01:00
|
|
|
bool connect(const string &serverHostname, string& errmsg) {
|
2008-12-29 02:28:49 +01:00
|
|
|
return connect_;
|
|
|
|
}
|
|
|
|
virtual
|
2009-01-12 15:38:11 +01:00
|
|
|
bool isMaster(bool& isMaster, BSONObj *info=0) {
|
|
|
|
return isMaster_;
|
2008-12-29 02:28:49 +01:00
|
|
|
}
|
|
|
|
void one( const BSONObj &one ) {
|
|
|
|
one_ = one;
|
|
|
|
}
|
|
|
|
void connect( bool val ) {
|
|
|
|
connect_ = val;
|
|
|
|
}
|
2009-01-12 15:38:11 +01:00
|
|
|
void setIsMaster( bool val ) {
|
|
|
|
isMaster_ = val;
|
2008-12-29 02:28:49 +01:00
|
|
|
}
|
2008-12-08 02:58:20 +01:00
|
|
|
private:
|
2008-12-29 02:28:49 +01:00
|
|
|
BSONObj one_;
|
|
|
|
bool connect_;
|
2009-01-12 15:38:11 +01:00
|
|
|
bool isMaster_;
|
2008-12-05 22:03:35 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
class DirectDBClientConnection : public DBClientConnection {
|
2008-12-08 02:58:20 +01:00
|
|
|
public:
|
2008-12-29 02:28:49 +01:00
|
|
|
struct ConnectionCallback {
|
2009-02-18 19:42:32 +01:00
|
|
|
virtual ~ConnectionCallback() {}
|
2008-12-29 02:28:49 +01:00
|
|
|
virtual void beforeCommand() {}
|
|
|
|
virtual void afterCommand() {}
|
|
|
|
};
|
|
|
|
DirectDBClientConnection( ReplPair *rp, ConnectionCallback *cc = 0 ) :
|
|
|
|
rp_( rp ),
|
|
|
|
cc_( cc ) {
|
|
|
|
}
|
2009-04-30 22:25:36 +02:00
|
|
|
virtual BSONObj findOne(const string &ns, Query query, const BSONObj *fieldsToReturn = 0, int queryOptions = 0) {
|
2008-12-29 02:28:49 +01:00
|
|
|
if ( cc_ ) cc_->beforeCommand();
|
|
|
|
SetGlobalReplPair s( rp_ );
|
|
|
|
BSONObjBuilder result;
|
2010-01-20 20:02:14 +01:00
|
|
|
result.append( "ok", Command::runAgainstRegistered( "admin.$cmd", query.obj, result ) ? 1.0 : 0.0 );
|
2008-12-29 02:28:49 +01:00
|
|
|
if ( cc_ ) cc_->afterCommand();
|
2009-02-09 19:04:32 +01:00
|
|
|
return result.obj();
|
2008-12-29 02:28:49 +01:00
|
|
|
}
|
2009-03-24 21:58:33 +01:00
|
|
|
virtual bool connect( const string &serverHostname, string& errmsg ) {
|
2008-12-29 02:28:49 +01:00
|
|
|
return true;
|
|
|
|
}
|
2008-12-08 02:58:20 +01:00
|
|
|
private:
|
2008-12-29 02:28:49 +01:00
|
|
|
ReplPair *rp_;
|
|
|
|
ConnectionCallback *cc_;
|
|
|
|
class SetGlobalReplPair {
|
|
|
|
public:
|
|
|
|
SetGlobalReplPair( ReplPair *rp ) {
|
|
|
|
backup_ = replPair;
|
|
|
|
replPair = rp;
|
|
|
|
}
|
|
|
|
~SetGlobalReplPair() {
|
|
|
|
replPair = backup_;
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
ReplPair *backup_;
|
|
|
|
};
|
2008-12-05 22:03:35 +01:00
|
|
|
};
|