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

Commit

Permalink
feat(inital-cluster-time): allow session to define initia value
Browse files Browse the repository at this point in the history
NODE-1088
  • Loading branch information
mbroadst committed Oct 6, 2017
1 parent a1d5b22 commit e3a1c8b
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
12 changes: 11 additions & 1 deletion lib/sessions.js
Expand Up @@ -13,9 +13,13 @@ class ClientSession {
}

this.topology = topology;
this.options = options || {};
this.hasEnded = false;
this._serverSession = undefined; // TBD

options = options || {};
if (typeof options.initialClusterTime !== 'undefined') {
this.clusterTime = options.initialClusterTime;
}
}

/**
Expand All @@ -26,6 +30,12 @@ class ClientSession {
return callback(null, null);
}

// TODO:
// When connected to a sharded cluster the endSessions command
// can be sent to any mongos. When connected to a replica set the
// endSessions command MUST be sent to the primary if the primary
// is available, otherwise it MUST be sent to any available secondary.

this.topology.command('admin.$cmd', { endSessions: 1, ids: [this.id] }, err => {
this.hasEnded = true;

Expand Down
26 changes: 24 additions & 2 deletions test/tests/unit/sessions_tests.js
Expand Up @@ -4,10 +4,33 @@ const Server = require('../../..').Server,
mock = require('../../mock'),
expect = require('chai').expect,
ServerSessionPool = require('../../../lib/sessions').ServerSessionPool,
ServerSession = require('../../../lib/sessions').ServerSession;
ServerSession = require('../../../lib/sessions').ServerSession,
ClientSession = require('../../../lib/sessions').ClientSession,
genClusterTime = require('./common').genClusterTime;

let test = {};
describe('Sessions', function() {
describe('ClientSession', function() {
it('should default to `null` for `clusterTime`', {
metadata: { requires: { topology: 'single' } },
test: function() {
const client = new Server();
const session = new ClientSession(client);
expect(session.clusterTime).to.not.exist;
}
});

it('should set the internal clusterTime to `initialClusterTime` if provided', {
metadata: { requires: { topology: 'single' } },
test: function() {
const clusterTime = genClusterTime(Date.now());
const client = new Server();
const session = new ClientSession(client, { initialClusterTime: clusterTime });
expect(session.clusterTime).to.eql(clusterTime);
}
});
});

describe('ServerSessionPool', function() {
afterEach(() => {
test.client.destroy();
Expand Down Expand Up @@ -41,7 +64,6 @@ describe('Sessions', function() {

it('should create a new session if the pool is empty', {
metadata: { requires: { topology: 'single' } },

test: function(done) {
const pool = new ServerSessionPool(test.client);
expect(pool.sessions).to.have.length(0);
Expand Down

0 comments on commit e3a1c8b

Please sign in to comment.