From e3a1c8b22ab9a8f29f6bc268876a40cc9bb3ea1e Mon Sep 17 00:00:00 2001 From: Matt Broadstone Date: Sat, 30 Sep 2017 13:18:28 -0400 Subject: [PATCH] feat(inital-cluster-time): allow session to define initia value NODE-1088 --- lib/sessions.js | 12 +++++++++++- test/tests/unit/sessions_tests.js | 26 ++++++++++++++++++++++++-- 2 files changed, 35 insertions(+), 3 deletions(-) diff --git a/lib/sessions.js b/lib/sessions.js index ebfeff45a..af5dce901 100644 --- a/lib/sessions.js +++ b/lib/sessions.js @@ -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; + } } /** @@ -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; diff --git a/test/tests/unit/sessions_tests.js b/test/tests/unit/sessions_tests.js index 8d4ff887f..6630b06b0 100644 --- a/test/tests/unit/sessions_tests.js +++ b/test/tests/unit/sessions_tests.js @@ -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(); @@ -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);