Skip to content

Commit 4706c3b

Browse files
mtrussottoevergreen
authored andcommitted
SERVER-43937 Allow MockDBClientConnection/MockDBServer to mock errors.
1 parent b9c13fa commit 4706c3b

File tree

4 files changed

+31
-23
lines changed

4 files changed

+31
-23
lines changed

src/mongo/dbtests/mock/mock_dbclient_connection.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#include "mongo/dbtests/mock/mock_dbclient_connection.h"
3333

3434
#include "mongo/client/dbclient_mockcursor.h"
35+
#include "mongo/rpc/get_status_from_command_result.h"
3536
#include "mongo/util/net/socket_exception.h"
3637
#include "mongo/util/time_support.h"
3738

@@ -68,12 +69,20 @@ std::pair<rpc::UniqueReply, DBClientBase*> MockDBClientConnection::runCommandWit
6869
checkConnection();
6970

7071
try {
71-
return {_remoteServer->runCommand(_remoteServerInstanceID, request), this};
72+
auto reply = _remoteServer->runCommand(_remoteServerInstanceID, request);
73+
auto status = getStatusFromCommandResult(reply->getCommandReply());
74+
// The real DBClientBase always throws HostUnreachable on network error, so we do the
75+
// same here.
76+
uassert(ErrorCodes::HostUnreachable,
77+
str::stream() << "network error while attempting to run "
78+
<< "command '" << request.getCommandName() << "' " << status,
79+
!ErrorCodes::isNetworkError(status));
80+
return {std::move(reply), this};
7281
} catch (const mongo::DBException&) {
7382
_isFailed = true;
7483
throw;
7584
}
76-
}
85+
} // namespace mongo
7786

7887

7988
std::unique_ptr<mongo::DBClientCursor> MockDBClientConnection::query(

src/mongo/dbtests/mock/mock_remote_db_server.cpp

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -47,20 +47,19 @@ using std::vector;
4747

4848
namespace mongo {
4949

50-
MockRemoteDBServer::CircularBSONIterator::CircularBSONIterator(const vector<BSONObj>& replyVector) {
51-
for (std::vector<mongo::BSONObj>::const_iterator iter = replyVector.begin();
52-
iter != replyVector.end();
53-
++iter) {
54-
_replyObjs.push_back(iter->copy());
50+
MockRemoteDBServer::CircularBSONIterator::CircularBSONIterator(
51+
const vector<StatusWith<BSONObj>>& replyVector) {
52+
for (auto iter = replyVector.begin(); iter != replyVector.end(); ++iter) {
53+
_replyObjs.push_back(iter->isOK() ? StatusWith(iter->getValue().copy()) : *iter);
5554
}
5655

5756
_iter = _replyObjs.begin();
5857
}
5958

60-
BSONObj MockRemoteDBServer::CircularBSONIterator::next() {
59+
StatusWith<BSONObj> MockRemoteDBServer::CircularBSONIterator::next() {
6160
verify(_iter != _replyObjs.end());
6261

63-
BSONObj reply = _iter->copy();
62+
StatusWith<BSONObj> reply = _iter->isOK() ? StatusWith(_iter->getValue().copy()) : *_iter;
6463
++_iter;
6564

6665
if (_iter == _replyObjs.end()) {
@@ -109,14 +108,15 @@ bool MockRemoteDBServer::isRunning() const {
109108
return _isRunning;
110109
}
111110

112-
void MockRemoteDBServer::setCommandReply(const string& cmdName, const mongo::BSONObj& replyObj) {
113-
vector<BSONObj> replySequence;
111+
void MockRemoteDBServer::setCommandReply(const string& cmdName,
112+
const StatusWith<mongo::BSONObj>& replyObj) {
113+
vector<StatusWith<BSONObj>> replySequence;
114114
replySequence.push_back(replyObj);
115115
setCommandReply(cmdName, replySequence);
116116
}
117117

118118
void MockRemoteDBServer::setCommandReply(const string& cmdName,
119-
const vector<BSONObj>& replySequence) {
119+
const vector<StatusWith<BSONObj>>& replySequence) {
120120
scoped_spinlock sLock(_lock);
121121
_cmdMap[cmdName].reset(new CircularBSONIterator(replySequence));
122122
}
@@ -146,16 +146,15 @@ rpc::UniqueReply MockRemoteDBServer::runCommand(InstanceID id, const OpMsgReques
146146
checkIfUp(id);
147147
std::string cmdName = request.getCommandName().toString();
148148

149-
BSONObj reply;
150-
{
149+
StatusWith<BSONObj> reply([this, &cmdName] {
151150
scoped_spinlock lk(_lock);
152151

153152
uassert(ErrorCodes::IllegalOperation,
154153
str::stream() << "no reply for command: " << cmdName,
155154
_cmdMap.count(cmdName));
156155

157-
reply = _cmdMap[cmdName]->next();
158-
}
156+
return _cmdMap[cmdName]->next();
157+
}());
159158

160159
if (_delayMilliSec > 0) {
161160
mongo::sleepmillis(_delayMilliSec);

src/mongo/dbtests/mock/mock_remote_db_server.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ class MockRemoteDBServer {
116116
* @param cmdName the name of the command
117117
* @param replyObj the exact reply for the command
118118
*/
119-
void setCommandReply(const std::string& cmdName, const mongo::BSONObj& replyObj);
119+
void setCommandReply(const std::string& cmdName, const StatusWith<mongo::BSONObj>& replyObj);
120120

121121
/**
122122
* Sets the reply for a command.
@@ -128,7 +128,7 @@ class MockRemoteDBServer {
128128
* that requires different results when calling a method.
129129
*/
130130
void setCommandReply(const std::string& cmdName,
131-
const std::vector<mongo::BSONObj>& replySequence);
131+
const std::vector<StatusWith<mongo::BSONObj>>& replySequence);
132132

133133
/**
134134
* Inserts a single document to this server.
@@ -203,12 +203,12 @@ class MockRemoteDBServer {
203203
/**
204204
* Creates a new iterator with a deep copy of the vector.
205205
*/
206-
CircularBSONIterator(const std::vector<mongo::BSONObj>& replyVector);
207-
mongo::BSONObj next();
206+
CircularBSONIterator(const std::vector<StatusWith<mongo::BSONObj>>& replyVector);
207+
StatusWith<mongo::BSONObj> next();
208208

209209
private:
210-
std::vector<mongo::BSONObj>::iterator _iter;
211-
std::vector<mongo::BSONObj> _replyObjs;
210+
std::vector<StatusWith<mongo::BSONObj>>::iterator _iter;
211+
std::vector<StatusWith<mongo::BSONObj>> _replyObjs;
212212
};
213213

214214
/**

src/mongo/dbtests/mock_dbclient_conn_test.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,7 @@ TEST(MockDBClientConnTest, CyclingCmd) {
411411
MockRemoteDBServer server("test");
412412

413413
{
414-
vector<BSONObj> isMasterSequence;
414+
vector<mongo::StatusWith<BSONObj>> isMasterSequence;
415415
isMasterSequence.push_back(BSON("set"
416416
<< "a"
417417
<< "isMaster" << true << "ok" << 1));

0 commit comments

Comments
 (0)