diff options
Diffstat (limited to 'src/mongo/db/repl/vote_requester_test.cpp')
| -rw-r--r-- | src/mongo/db/repl/vote_requester_test.cpp | 39 |
1 files changed, 38 insertions, 1 deletions
diff --git a/src/mongo/db/repl/vote_requester_test.cpp b/src/mongo/db/repl/vote_requester_test.cpp index 1dd97c456ea..02adb0a919a 100644 --- a/src/mongo/db/repl/vote_requester_test.cpp +++ b/src/mongo/db/repl/vote_requester_test.cpp @@ -31,6 +31,7 @@ #include <memory> #include "mongo/base/status.h" +#include "mongo/db/commands.h" #include "mongo/db/jsobj.h" #include "mongo/db/repl/repl_set_request_votes_args.h" #include "mongo/db/repl/replication_executor.h" @@ -109,7 +110,17 @@ protected: } void processResponse(const RemoteCommandRequest& request, const ResponseStatus& response) { - _requester->processResponse(request, response); + if (!response.isOK()) { + _requester->processResponse(request, response); + return; + } + BSONObjBuilder builder; + builder.appendElements(response.data); + // Appends ok:1.0 (status ok) to response data if 'ok' field is missing. + Command::appendCommandStatus(builder, Status::OK()); + ResponseStatus responseWithCmdStatus = response; + responseWithCmdStatus.data = builder.obj(); + _requester->processResponse(request, responseWithCmdStatus); } int getNumResponders() { @@ -140,6 +151,19 @@ protected: NetworkInterfaceMock::Response(response.toBSON(), BSONObj(), Milliseconds(10))); } + ResponseStatus votedYesStatusNotOkBecauseFailedToStoreLastVote() { + ReplSetRequestVotesResponse response; + BSONObjBuilder result; + response.setVoteGranted(true); + response.setTerm(1); + response.addToBSON(&result); + auto status = + Status(ErrorCodes::InterruptedDueToReplStateChange, "operation was interrupted"); + Command::appendCommandStatus(result, status); + return ResponseStatus( + NetworkInterfaceMock::Response(result.obj(), BSONObj(), Milliseconds(10))); + } + ResponseStatus votedNoBecauseConfigVersionDoesNotMatch() { ReplSetRequestVotesResponse response; response.setVoteGranted(false); @@ -236,6 +260,19 @@ TEST_F(VoteRequesterTest, ImmediateGoodResponseWinElection) { ASSERT_EQUALS(1, getNumResponders()); } +TEST_F(VoteRequesterTest, VoterFailedToStoreLastVote) { + startCapturingLogMessages(); + ASSERT_FALSE(hasReceivedSufficientResponses()); + processResponse(requestFrom("host1"), votedYesStatusNotOkBecauseFailedToStoreLastVote()); + ASSERT_FALSE(hasReceivedSufficientResponses()); + ASSERT_EQUALS(1, countLogLinesContaining("received an invalid response from host1:27017")); + processResponse(requestFrom("host2"), votedYes()); + ASSERT_TRUE(hasReceivedSufficientResponses()); + ASSERT(VoteRequester::Result::kSuccessfullyElected == getResult()); + ASSERT_EQUALS(2, getNumResponders()); + stopCapturingLogMessages(); +} + TEST_F(VoteRequesterTest, BadConfigVersionWinElection) { startCapturingLogMessages(); ASSERT_FALSE(hasReceivedSufficientResponses()); |
