0
0
mirror of https://github.com/mongodb/mongo.git synced 2024-12-01 09:32:32 +01:00
mongodb/jstests/gle/gle_explicit_optime.js
2016-05-28 17:55:12 -04:00

61 lines
2.0 KiB
JavaScript

//
// Tests the use of the wOpTime option in getLastError
//
// This test requires fsync to lock the secondary, so cannot be run on storage engines which do not
// support the command.
// @tags: [requires_fsync]
var rst = new ReplSetTest({nodes: 2});
rst.startSet();
rst.initiate();
var primary = rst.getPrimary();
var secondary = rst.getSecondary();
var coll = primary.getCollection("foo.bar");
// Insert a doc and replicate it to two servers
coll.insert({some: "doc"});
var gleObj = coll.getDB().getLastErrorObj(2); // w : 2
assert.eq(null, gleObj.err);
var opTimeBeforeFailure = gleObj.lastOp;
// Lock the secondary
assert.commandWorked(secondary.getDB("admin").fsyncLock());
// Insert a doc and replicate it to the primary only
coll.insert({some: "doc"});
gleObj = coll.getDB().getLastErrorObj(1); // w : 1
assert.eq(null, gleObj.err);
var opTimeAfterFailure = gleObj.lastOp;
printjson(opTimeBeforeFailure);
printjson(opTimeAfterFailure);
printjson(primary.getDB("admin").runCommand({replSetGetStatus: true}));
// Create a new connection with new client and no opTime
var newClientConn = new Mongo(primary.host);
// New client has no set opTime, so w : 2 has no impact
gleObj = newClientConn.getCollection(coll.toString()).getDB().getLastErrorObj(2); // w : 2
assert.eq(null, gleObj.err);
// Using an explicit optime on the new client should work if the optime is earlier than the
// secondary was locked
var gleOpTimeBefore = {getLastError: true, w: 2, wOpTime: opTimeBeforeFailure};
gleObj = newClientConn.getCollection(coll.toString()).getDB().runCommand(gleOpTimeBefore);
assert.eq(null, gleObj.err);
// Using an explicit optime on the new client should not work if the optime is later than the
// secondary was locked
var gleOpTimeAfter = {getLastError: true, w: 2, wtimeout: 1000, wOpTime: opTimeAfterFailure};
gleObj = newClientConn.getCollection(coll.toString()).getDB().runCommand(gleOpTimeAfter);
assert.neq(null, gleObj.err);
assert(gleObj.wtimeout);
jsTest.log("DONE!");
// Unlock the secondary
secondary.getDB("admin").fsyncUnlock();
rst.stopSet();