summaryrefslogtreecommitdiff
path: root/src/mongo/dbtests/replica_set_monitor_test.cpp
diff options
context:
space:
mode:
authorRandolph Tan <randolph@10gen.com>2012-09-10 15:30:01 -0400
committerRandolph Tan <randolph@10gen.com>2012-12-05 10:16:07 -0500
commitf75d3a2e104bd2f99be2f0cb585c26dea6f563d3 (patch)
tree263e721d73ec2895a90693173f1693c5a9975f42 /src/mongo/dbtests/replica_set_monitor_test.cpp
parent33d7543c12a21f6496081ca9fa6c3676190aa991 (diff)
downloadmongo-f75d3a2e104bd2f99be2f0cb585c26dea6f563d3.tar.gz
SERVER-6754 Create a mock for testing replica set connections
Final step: Modify the replica set client to be able to use custom hooks for creating connections.
Diffstat (limited to 'src/mongo/dbtests/replica_set_monitor_test.cpp')
-rw-r--r--src/mongo/dbtests/replica_set_monitor_test.cpp83
1 files changed, 70 insertions, 13 deletions
diff --git a/src/mongo/dbtests/replica_set_monitor_test.cpp b/src/mongo/dbtests/replica_set_monitor_test.cpp
index d3525574061..db909a5c205 100644
--- a/src/mongo/dbtests/replica_set_monitor_test.cpp
+++ b/src/mongo/dbtests/replica_set_monitor_test.cpp
@@ -19,23 +19,30 @@
* ReplicaSetMonitor::selectNode and TagSet
*/
-#include <vector>
-
+#include "mongo/client/dbclientinterface.h"
#include "mongo/client/dbclient_rs.h"
+#include "mongo/dbtests/mock/mock_conn_registry.h"
+#include "mongo/dbtests/mock/mock_replica_set.h"
#include "mongo/unittest/unittest.h"
-namespace {
- using std::vector;
- using boost::scoped_ptr;
-
- using mongo::BSONObj;
- using mongo::BSONArray;
- using mongo::BSONArrayBuilder;
- using mongo::ReplicaSetMonitor;
- using mongo::HostAndPort;
- using mongo::ReadPreference;
- using mongo::TagSet;
+#include <vector>
+using std::vector;
+using std::string;
+using boost::scoped_ptr;
+
+using mongo::BSONObj;
+using mongo::BSONArray;
+using mongo::BSONArrayBuilder;
+using mongo::ConnectionString;
+using mongo::HostAndPort;
+using mongo::MockReplicaSet;
+using mongo::ReadPreference;
+using mongo::ReplicaSetMonitor;
+using mongo::ReplicaSetMonitorPtr;
+using mongo::TagSet;
+
+namespace mongo_test {
const BSONObj SampleIsMasterDoc = BSON("tags"
<< BSON("dc" << "NYC"
<< "p" << "2"
@@ -1475,4 +1482,54 @@ namespace {
ASSERT_THROWS(tags.getCurrentTag(), mongo::AssertionException);
#endif
}
+
+ // TODO: Port these existing tests here: replmonitor_bad_seed.js, repl_monitor_refresh.js
+
+ /**
+ * Warning: Tests running this fixture cannot be run in parallel with other tests
+ * that uses ConnectionString::setConnectionHook
+ */
+ class ReplicaSetMonitorTest: public mongo::unittest::Test {
+ protected:
+ void setUp() {
+ _replSet.reset(new MockReplicaSet("test", 3));
+ _originalConnectionHook = ConnectionString::getConnectionHook();
+ ConnectionString::setConnectionHook(
+ mongo::MockConnRegistry::get()->getConnStrHook());
+ }
+
+ void tearDown() {
+ ConnectionString::setConnectionHook(_originalConnectionHook);
+ ReplicaSetMonitor::remove(_replSet->getSetName(), true);
+ }
+
+ MockReplicaSet* getReplSet() {
+ return _replSet.get();
+ }
+
+ private:
+ ConnectionString::ConnectionHook* _originalConnectionHook;
+ boost::scoped_ptr<MockReplicaSet> _replSet;
+ };
+
+ TEST_F(ReplicaSetMonitorTest, SeedWithPriOnlySecDown) {
+ // Test to make sure that the monitor doesn't crash when
+ // ConnectionString::connect returns NULL
+ MockReplicaSet* replSet = getReplSet();
+ replSet->kill(replSet->getSecondaries());
+
+ // Create a monitor with primary as the only seed list and the two secondaries
+ // down so a NULL connection object will be stored for these secondaries in
+ // the _nodes vector.
+ const string replSetName(replSet->getSetName());
+ vector<HostAndPort> seedList;
+ seedList.push_back(HostAndPort(replSet->getPrimary()));
+ ReplicaSetMonitor::createIfNeeded(replSetName, seedList);
+
+ replSet->kill(replSet->getPrimary());
+
+ ReplicaSetMonitorPtr monitor = ReplicaSetMonitor::get(replSet->getSetName());
+ // Trigger calls to Node::getConnWithRefresh
+ monitor->check(true);
+ }
}