christkv/node-mongodb-native — Issue #373: FindandModify returns correct result when it should return a wtimeout error. Insert behaves properly.
Author: andrewjstone
Created: 2011-10-14 02:01:51+00:00
State: open

If you setup a 2 server replica set and attempt a findAndModify with {safe: {w:3, wtimeout: 5000}} the request takes over 5 seconds, but then returns the proper result. It should return a wtimeout error similar to insert. The following test below demonstrates the behavior. I couldn't get the replica set tests packaged with the driver to run on either my desktop or VPS. That's why I wrote the test this way, in order to connect to an already existing 2 member replica set.

var mongo = require('mongodb'),
Db = mongo.Db,
Server = mongo.Server,
ReplSetServers = mongo.ReplSetServers
;
var inspect = require('util').inspect;

exports['findAndModify - w:3 timeout'] = function(test) {
var replSet = new ReplSetServers([
new Server(host1, port1)
], {rs_name: 'rs'});

var db = new Db('test', replSet);

db.open(function(err, client) {
if (err) console.log(err);
test.ok(!err);

client.dropDatabase(function(err) {
test.ok(!err);

var collection = new mongo.Collection(client, 'stuff');
collection.insert({a:1, b: 'red'}, {safe: {w:2, wtimeout:5000}}, function(err, docs) {
test.ok(!err);
var doc = docs[0];
collection.findAndModify(
{_id: doc._id},
[],
{$inc: {a: 1}, $set : {b: 'blue'}},
{new: true, safe: {w:3, wtimeout: 5000}},
function(err, contest) {
test.ok(err);
test.ok(!contest);
db.close();
test.done();
}
);
});
});
});
};