Skip to content

Commit 4b2021a

Browse files
committed
fix: move session support check to operation layer
Monitor checks run on a timer and network errors cause a reset of the topology that would be corrected in the next cycle of the monitor, we were checking a property that gets updated by this async work. By moving the check to the operations layer we will allow users to obtain a session regardless of support and then emit an error if there is not support when the session is used in an operation. NODE-3100
1 parent 8bd9777 commit 4b2021a

File tree

3 files changed

+36
-18
lines changed

3 files changed

+36
-18
lines changed

lib/mongo_client.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -457,10 +457,6 @@ MongoClient.prototype.startSession = function(options) {
457457
throw new MongoError('Must connect to a server before calling this method');
458458
}
459459

460-
if (!this.topology.hasSessionSupport()) {
461-
throw new MongoError('Current topology does not support sessions');
462-
}
463-
464460
return this.topology.startSession(options, this.s.options);
465461
};
466462

lib/operations/execute_operation.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ function executeOperation(topology, operation, callback) {
4747
} else if (operation.session.hasEnded) {
4848
throw new MongoError('Use of expired sessions is not permitted');
4949
}
50+
} else if (operation.session && operation.session.explicit) {
51+
throw new MongoError('Current topology does not support sessions');
5052
}
5153

5254
let result;

test/unit/sessions/client.test.js

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ describe('Sessions', function() {
1616

1717
it('should throw an exception if sessions are not supported', {
1818
metadata: { requires: { topology: 'single' } },
19-
test: function(done) {
19+
test() {
2020
test.server.setMessageHandler(request => {
2121
var doc = request.document;
2222
if (doc.ismaster) {
@@ -27,21 +27,32 @@ describe('Sessions', function() {
2727
});
2828

2929
const client = this.configuration.newClient(`mongodb://${test.server.uri()}/test`);
30-
client.connect(function(err, client) {
31-
expect(err).to.not.exist;
32-
expect(() => {
33-
client.startSession();
34-
}).to.throw(/Current topology does not support sessions/);
35-
36-
client.close(done);
37-
});
30+
return client
31+
.connect()
32+
.then(function(client) {
33+
const session = client.startSession();
34+
return client
35+
.db()
36+
.collection('t')
37+
.insertOne({ a: 1 }, { session });
38+
})
39+
.then(() => {
40+
expect.fail('Expected an error to be thrown about not supporting sessions');
41+
})
42+
.catch(error => {
43+
expect(error.message).to.equal('Current topology does not support sessions');
44+
})
45+
.then(() => {
46+
return client.close();
47+
});
3848
}
3949
});
4050

4151
it('should throw an exception if sessions are not supported on some servers', {
4252
metadata: { requires: { topology: 'single' } },
4353
test() {
4454
const replicaSetMock = new ReplSetFixture();
55+
let client;
4556
return replicaSetMock
4657
.setup({ doNotInitHandlers: true })
4758
.then(() => {
@@ -92,14 +103,23 @@ describe('Sessions', function() {
92103
return replicaSetMock.uri();
93104
})
94105
.then(uri => {
95-
const client = this.configuration.newClient(uri);
106+
client = this.configuration.newClient(uri);
96107
return client.connect();
97108
})
98109
.then(client => {
99-
expect(client.topology.s.description.logicalSessionTimeoutMinutes).to.not.exist;
100-
expect(() => {
101-
client.startSession();
102-
}).to.throw(/Current topology does not support sessions/);
110+
const session = client.startSession();
111+
return client
112+
.db()
113+
.collection('t')
114+
.insertOne({ a: 1 }, { session });
115+
})
116+
.then(() => {
117+
expect.fail('Expected an error to be thrown about not supporting sessions');
118+
})
119+
.catch(error => {
120+
expect(error.message).to.equal('Current topology does not support sessions');
121+
})
122+
.then(() => {
103123
return client.close();
104124
});
105125
}

0 commit comments

Comments
 (0)