Skip to content
This repository has been archived by the owner on Feb 4, 2022. It is now read-only.

Commit

Permalink
feat(cluster-time): ensure clusterTime makes it to outgoing commands
Browse files Browse the repository at this point in the history
NODE-1088
  • Loading branch information
mbroadst committed Oct 2, 2017
1 parent 3bfac0b commit e700b79
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
14 changes: 14 additions & 0 deletions lib/connection/pool.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ var DESTROYED = 'destroyed';

var _id = 0;

function supportsClusterTime(topology) {
return topology.ismaster == null ? false : topology.ismaster.maxWireVersion >= 6;
}

/**
* Creates a new Pool instance
* @class
Expand Down Expand Up @@ -1137,6 +1141,16 @@ Pool.prototype.write = function(commands, options, cb) {
// Get the requestId
operation.requestId = commands[commands.length - 1].requestId;

if (supportsClusterTime(this.topology) && this.topology.clusterTime) {
commands.forEach(command => {
if (command instanceof Query) {
command.query.$clusterTime = this.topology.clusterTime;
} else {
command.$clusterTime = this.topology.clusterTime;
}
});
}

// Prepare the operation buffer
serializeCommands(self, commands, [], function(err, serializedCommands) {
if (err) throw err;
Expand Down
34 changes: 34 additions & 0 deletions test/tests/unit/single/sessions_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,40 @@ describe('Sessions (Single)', function() {
}
});

it('should send `clusterTime` on outgoing messages', {
metadata: { requires: { topology: 'single' } },
test: function(done) {
const clusterTime = genClusterTime(Date.now());
let sentIsMaster = false;
test.server.setMessageHandler(request => {
if (sentIsMaster) {
expect(request.document.$clusterTime).to.eql(clusterTime);
request.reply({ ok: 1 });
return;
}

sentIsMaster = true;
request.reply(
assign({}, mock.DEFAULT_ISMASTER, {
maxWireVersion: 6,
$clusterTime: clusterTime
})
);
});

const client = new Server(test.server.address());
client.on('error', done);
client.once('connect', () => {
client.command('admin.$cmd', { ping: 1 }, err => {
expect(err).to.not.exist;
done();
});
});

client.connect();
}
});

it('should default `logicalSessionTimeoutMinutes` to `null`', {
metadata: { requires: { topology: 'single' } },
test: function() {
Expand Down

0 comments on commit e700b79

Please sign in to comment.