0
0
mirror of https://github.com/mongodb/mongo.git synced 2024-12-01 09:32:32 +01:00
mongodb/jstests/repl/replacePeer2.js
2009-04-02 18:20:57 -04:00

93 lines
3.2 KiB
JavaScript

// test replace peer
var baseName = "jstests_replacepeer2test";
ismaster = function( n ) {
im = n.getDB( "admin" ).runCommand( { "ismaster" : 1 } );
// print( "ismaster: " + tojson( im ) );
assert( im );
return im.ismaster;
}
var writeOneIdx = 0;
writeOne = function( n ) {
n.getDB( baseName ).z.save( { _id: new ObjectId(), i: ++writeOneIdx } );
}
getCount = function( n ) {
return n.getDB( baseName ).z.find( { i: writeOneIdx } ).toArray().length;
}
checkWrite = function( m, s ) {
writeOne( m );
assert.eq( 1, getCount( m ) );
s.setSlaveOk();
assert.soon( function() {
if ( -1 == s.getDBNames().indexOf( baseName ) )
return false;
if ( -1 == s.getDB( baseName ).getCollectionNames().indexOf( "z" ) )
return false;
return 1 == getCount( s );
} );
}
doTest = function( signal ) {
ports = allocatePorts( 4 );
// spec small oplog for fast startup on 64bit machines
a = startMongod( "--port", ports[ 0 ], "--dbpath", "/data/db/" + baseName + "-arbiter", "--nohttpinterface" );
l = startMongod( "--port", ports[ 1 ], "--dbpath", "/data/db/" + baseName + "-left", "--pairwith", "127.0.0.1:" + ports[ 3 ], "127.0.0.1:" + ports[ 0 ], "--oplogSize", "1", "--nohttpinterface" );
r = startMongod( "--port", ports[ 3 ], "--dbpath", "/data/db/" + baseName + "-right", "--pairwith", "127.0.0.1:" + ports[ 1 ], "127.0.0.1:" + ports[ 0 ], "--oplogSize", "1", "--nohttpinterface" );
assert.soon( function() {
am = ismaster( a );
lm = ismaster( l );
rm = ismaster( r );
assert( am == 1 );
assert( lm == -1 || lm == 0 );
assert( rm == -1 || rm == 0 || rm == 1 );
return ( lm == 0 && rm == 1 );
} );
checkWrite( r, l );
assert.eq( 1, l.getDB( "admin" ).runCommand( {replacepeer:1} ).ok );
// Should not be saved to l.
writeOne( r );
// Make sure there would be enough time to save to l if we hadn't called replacepeer.
sleep( 10000 );
stopMongod( ports[ 3 ], signal );
stopMongod( ports[ 1 ], signal );
l = startMongoProgram( "mongod", "--port", ports[ 1 ], "--dbpath", "/data/db/" + baseName + "-left", "--pairwith", "127.0.0.1:" + ports[ 2 ], "127.0.0.1:" + ports[ 0 ], "--oplogSize", "1", "--nohttpinterface" );
r = startMongod( "--port", ports[ 2 ], "--dbpath", "/data/db/" + baseName + "-right", "--pairwith", "127.0.0.1:" + ports[ 1 ], "127.0.0.1:" + ports[ 0 ], "--oplogSize", "1", "--nohttpinterface" );
assert.soon( function() {
am = ismaster( a );
lm = ismaster( l );
rm = ismaster( r );
assert( am == 1 );
assert( lm == -1 || lm == 0 || lm == 1 );
assert( rm == -1 || rm == 0 );
return ( lm == 1 && rm == 0 );
} );
checkWrite( l, r );
r.setSlaveOk();
assert.eq( 2, r.getDB( baseName ).z.find().toArray().length );
ports.forEach( function( x ) { stopMongod( x ); } );
}
doTest( 15 ); // SIGTERM
doTest( 9 ); // SIGKILL