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

Commit e3a1c8b

Browse files
committed
feat(inital-cluster-time): allow session to define initia value
NODE-1088
1 parent a1d5b22 commit e3a1c8b

File tree

2 files changed

+35
-3
lines changed

2 files changed

+35
-3
lines changed

lib/sessions.js

+11-1
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,13 @@ class ClientSession {
1313
}
1414

1515
this.topology = topology;
16-
this.options = options || {};
1716
this.hasEnded = false;
1817
this._serverSession = undefined; // TBD
18+
19+
options = options || {};
20+
if (typeof options.initialClusterTime !== 'undefined') {
21+
this.clusterTime = options.initialClusterTime;
22+
}
1923
}
2024

2125
/**
@@ -26,6 +30,12 @@ class ClientSession {
2630
return callback(null, null);
2731
}
2832

33+
// TODO:
34+
// When connected to a sharded cluster the endSessions command
35+
// can be sent to any mongos. When connected to a replica set the
36+
// endSessions command MUST be sent to the primary if the primary
37+
// is available, otherwise it MUST be sent to any available secondary.
38+
2939
this.topology.command('admin.$cmd', { endSessions: 1, ids: [this.id] }, err => {
3040
this.hasEnded = true;
3141

test/tests/unit/sessions_tests.js

+24-2
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,33 @@ const Server = require('../../..').Server,
44
mock = require('../../mock'),
55
expect = require('chai').expect,
66
ServerSessionPool = require('../../../lib/sessions').ServerSessionPool,
7-
ServerSession = require('../../../lib/sessions').ServerSession;
7+
ServerSession = require('../../../lib/sessions').ServerSession,
8+
ClientSession = require('../../../lib/sessions').ClientSession,
9+
genClusterTime = require('./common').genClusterTime;
810

911
let test = {};
1012
describe('Sessions', function() {
13+
describe('ClientSession', function() {
14+
it('should default to `null` for `clusterTime`', {
15+
metadata: { requires: { topology: 'single' } },
16+
test: function() {
17+
const client = new Server();
18+
const session = new ClientSession(client);
19+
expect(session.clusterTime).to.not.exist;
20+
}
21+
});
22+
23+
it('should set the internal clusterTime to `initialClusterTime` if provided', {
24+
metadata: { requires: { topology: 'single' } },
25+
test: function() {
26+
const clusterTime = genClusterTime(Date.now());
27+
const client = new Server();
28+
const session = new ClientSession(client, { initialClusterTime: clusterTime });
29+
expect(session.clusterTime).to.eql(clusterTime);
30+
}
31+
});
32+
});
33+
1134
describe('ServerSessionPool', function() {
1235
afterEach(() => {
1336
test.client.destroy();
@@ -41,7 +64,6 @@ describe('Sessions', function() {
4164

4265
it('should create a new session if the pool is empty', {
4366
metadata: { requires: { topology: 'single' } },
44-
4567
test: function(done) {
4668
const pool = new ServerSessionPool(test.client);
4769
expect(pool.sessions).to.have.length(0);

0 commit comments

Comments
 (0)