@@ -114,7 +114,6 @@ exports['Should correctly connect with default replicaset and socket options set
114
114
115
115
var db = new Db ( 'integration_test_' , replSet , { w :0 } ) ;
116
116
db . open ( function ( err , p_db ) {
117
- console . dir ( err )
118
117
test . equal ( null , err ) ;
119
118
// Get a connection
120
119
var connection = db . serverConfig . connections ( ) [ 0 ] ;
@@ -237,13 +236,10 @@ var ensureConnection = function(configuration, numberOfTries, callback) {
237
236
{ rs_name :configuration . replicasetName , socketOptions : { connectTimeoutMS : 1000 } }
238
237
) ;
239
238
240
- // console.log("===================== ensureConnection 0 :: " + numberOfTries)
241
-
242
239
if ( numberOfTries <= 0 ) return callback ( new Error ( "could not connect correctly" ) , null ) ;
243
240
// Open the db
244
241
var db = new Db ( 'integration_test_' , replSet , { w :0 } ) ;
245
242
db . open ( function ( err , p_db ) {
246
- // console.log("===================== ensureConnection 1")
247
243
// Close the connection
248
244
db . close ( ) ;
249
245
@@ -959,3 +955,98 @@ exports['Should give an error when using a two server seeds and no setName'] = {
959
955
} ) ;
960
956
}
961
957
}
958
+
959
+ var waitForPrimary = function ( count , config , options , callback ) {
960
+ var ReplSet = require ( 'mongodb-core' ) . ReplSet ;
961
+ if ( count == 0 ) return callback ( new Error ( "could not connect" ) ) ;
962
+ // Attempt to connect
963
+ var server = new ReplSet ( config , options ) ;
964
+ server . on ( 'error' , function ( err ) {
965
+ server . destroy ( ) ;
966
+
967
+ setTimeout ( function ( ) {
968
+ waitForPrimary ( count - 1 , config , options , callback ) ;
969
+ } , 1000 ) ;
970
+ } ) ;
971
+
972
+ server . on ( 'fullsetup' , function ( _server ) {
973
+ server . destroy ( ) ;
974
+ callback ( ) ;
975
+ } ) ;
976
+
977
+ // Start connection
978
+ server . connect ( ) ;
979
+ }
980
+
981
+ exports [ 'Replicaset connection where a server is standalone' ] = {
982
+ metadata : {
983
+ requires : {
984
+ topology : "replicaset"
985
+ }
986
+ } ,
987
+
988
+ test : function ( configuration , test ) {
989
+ var Server = configuration . require . Server
990
+ , ReplSet = configuration . require . ReplSet
991
+ , ServerManager = require ( 'mongodb-core' ) . ServerManager
992
+ , MongoClient = configuration . require . MongoClient
993
+ , manager = configuration . manager ;
994
+
995
+ // State
996
+ var joined = { 'primary' :[ ] , 'secondary' : [ ] , 'arbiter' : [ ] , 'passive' : [ ] } ;
997
+ var left = { 'primary' :[ ] , 'secondary' : [ ] , 'arbiter' : [ ] , 'passive' : [ ] } ;
998
+ // Get the primary server
999
+ manager . getServerManagerByType ( 'primary' , function ( err , primaryServerManager ) {
1000
+ test . equal ( null , err ) ;
1001
+
1002
+ // Get the secondary server
1003
+ manager . getServerManagerByType ( 'secondary' , function ( err , serverManager ) {
1004
+ test . equal ( null , err ) ;
1005
+
1006
+ // Start a new server manager
1007
+ var nonReplSetMember = new ServerManager ( {
1008
+ host : primaryServerManager . host
1009
+ , port : primaryServerManager . port
1010
+ , dbpath : primaryServerManager . dbpath
1011
+ , logpath : primaryServerManager . logpath
1012
+ } ) ;
1013
+
1014
+ var config = [ {
1015
+ host : serverManager . host
1016
+ , port : serverManager . port
1017
+ } ] ;
1018
+
1019
+ var options = {
1020
+ setName : configuration . replicasetName
1021
+ } ;
1022
+
1023
+ // Stop the primary
1024
+ primaryServerManager . stop ( function ( err , r ) {
1025
+
1026
+ // Start a non replset member
1027
+ nonReplSetMember . start ( function ( ) {
1028
+
1029
+ // Wait for primary
1030
+ waitForPrimary ( 30 , config , options , function ( err , r ) {
1031
+ test . equal ( null , err ) ;
1032
+
1033
+ var url = f ( "mongodb://localhost:%s,localhost:%s,localhost:%s/integration_test_?replicaSet=%s"
1034
+ , configuration . port , configuration . port + 1 , configuration . port + 2 , configuration . replicasetName )
1035
+ // Attempt to connect using MongoClient uri
1036
+ MongoClient . connect ( url , function ( err , db ) {
1037
+ test . equal ( null , err ) ;
1038
+ test . ok ( db . serverConfig instanceof ReplSet ) ;
1039
+
1040
+ // Stop the normal server
1041
+ nonReplSetMember . stop ( function ( ) {
1042
+ restartAndDone ( configuration , test ) ;
1043
+ } ) ;
1044
+ } ) ;
1045
+ } ) ;
1046
+ } ) ;
1047
+ } ) ;
1048
+ } ) ;
1049
+ } ) ;
1050
+ }
1051
+ }
1052
+
0 commit comments