Skip to content

Commit ee110ac

Browse files
daprahamianmbroadst
authored andcommitted
fix(comments): adding fixes for PR comments
1 parent a04879a commit ee110ac

9 files changed

+56
-13
lines changed

lib/topologies/topology_base.js

+2-4
Original file line numberDiff line numberDiff line change
@@ -394,11 +394,9 @@ class TopologyBase extends EventEmitter {
394394
}
395395

396396
close(forceClosed) {
397-
// If we have sessions, we want to send a single `endSessions` command for them,
398-
// and then individually clean them up. They will be removed from the internal state
399-
// when they emit their `ended` events.
397+
// If we have sessions, we want to individually move them to the session pool,
398+
// and then send a single endSessions call.
400399
if (this.s.sessions.length) {
401-
this.endSessions(this.s.sessions.map(session => session.id));
402400
this.s.sessions.forEach(session => session.endSession({ skipCommand: true }));
403401
}
404402

test/functional/examples_tests.js

+5-6
Original file line numberDiff line numberDiff line change
@@ -1230,18 +1230,17 @@ describe('Examples', function() {
12301230
* @ignore
12311231
*/
12321232
it('CausalConsistency', {
1233-
metadata: { requires: { topology: ['single'], mongodb: '>=3.6.0' } },
1233+
metadata: {
1234+
requires: { topology: ['single'], mongodb: '>=3.6.0' },
1235+
sessions: { skipLeakTests: true }
1236+
},
12341237

12351238
test: function(done) {
12361239
const configuration = this.configuration;
12371240
const client = configuration.newClient(configuration.writeConcernMax(), { poolSize: 1 });
12381241

12391242
client.connect(function(err, client) {
1240-
let session;
12411243
const cleanup = e => {
1242-
if (session) {
1243-
session.endSession();
1244-
}
12451244
client.close();
12461245
done(e);
12471246
};
@@ -1250,7 +1249,7 @@ describe('Examples', function() {
12501249

12511250
const db = client.db(configuration.db);
12521251
const collection = db.collection('causalConsistencyExample');
1253-
session = client.startSession({ causalConsistency: true });
1252+
const session = client.startSession({ causalConsistency: true });
12541253

12551254
collection.insertOne({ darmok: 'jalad' }, { session });
12561255
collection.updateOne({ darmok: 'jalad' }, { $set: { darmok: 'tanagra' } }, { session });

test/functional/find_tests.js

+5
Original file line numberDiff line numberDiff line change
@@ -2733,6 +2733,8 @@ describe('Find', function() {
27332733
cursors[0].next(function(err) {
27342734
test.equal(null, err);
27352735

2736+
// We need to close the cursor since it is not exhausted,
2737+
// and we need to end the implicit session
27362738
cursors[0].close();
27372739
client.close();
27382740
done();
@@ -2780,6 +2782,9 @@ describe('Find', function() {
27802782

27812783
cursor.next(function(err) {
27822784
test.ok(err !== null);
2785+
2786+
// We need to close the cursor since it is not exhausted,
2787+
// and we need to end the implicit session
27832788
cursor.close();
27842789
client.close();
27852790
done();

test/functional/gridfs_stream_tests.js

+16
Original file line numberDiff line numberDiff line change
@@ -1124,11 +1124,19 @@ describe('GridFS Stream', function() {
11241124
download.on('error', function(error) {
11251125
if (!testSpec.assert.error) {
11261126
test.ok(false);
1127+
1128+
// We need to abort in order to close the underlying cursor,
1129+
// and by extension the implicit session used for the cursor.
1130+
// This is only necessary if the cursor is not exhausted
11271131
download.abort();
11281132
client.close();
11291133
done();
11301134
}
11311135
test.ok(error.toString().indexOf(testSpec.assert.error) !== -1);
1136+
1137+
// We need to abort in order to close the underlying cursor,
1138+
// and by extension the implicit session used for the cursor.
1139+
// This is only necessary if the cursor is not exhausted
11321140
download.abort();
11331141
client.close();
11341142
done();
@@ -1138,12 +1146,20 @@ describe('GridFS Stream', function() {
11381146
var result = testSpec.assert.result;
11391147
if (!result) {
11401148
test.ok(false);
1149+
1150+
// We need to abort in order to close the underlying cursor,
1151+
// and by extension the implicit session used for the cursor.
1152+
// This is only necessary if the cursor is not exhausted
11411153
download.abort();
11421154
client.close();
11431155
done();
11441156
}
11451157

11461158
test.equal(res.toString('hex'), result.$hex);
1159+
1160+
// We need to abort in order to close the underlying cursor,
1161+
// and by extension the implicit session used for the cursor.
1162+
// This is only necessary if the cursor is not exhausted
11471163
download.abort();
11481164
client.close();
11491165
done();

test/functional/operation_changestream_example_tests.js

+17
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ describe('Changestream Examples', function() {
2929
if (err) return console.log(err);
3030
expect(err).to.equal(null);
3131
expect(next).to.exist;
32+
33+
// Since changeStream has an implicit seession,
34+
// we need to close the changeStream for unit testing purposes
3235
changeStream.close();
3336
client.close();
3437
done();
@@ -67,6 +70,9 @@ describe('Changestream Examples', function() {
6770
const changeStream = collection.watch();
6871
changeStream.on('change', function(change) {
6972
expect(change).to.exist;
73+
74+
// Since changeStream has an implicit seession,
75+
// we need to close the changeStream for unit testing purposes
7076
changeStream.close();
7177
client.close();
7278
done();
@@ -102,6 +108,9 @@ describe('Changestream Examples', function() {
102108

103109
changeStream.stream({ transform: JSON.stringify }).once('data', function(chunk) {
104110
expect(chunk).to.exist;
111+
112+
// Since changeStream has an implicit seession,
113+
// we need to close the changeStream for unit testing purposes
105114
changeStream.close();
106115
client.close();
107116
done();
@@ -138,6 +147,9 @@ describe('Changestream Examples', function() {
138147
const changeStream = collection.watch({ fullDocument: 'updateLookup' });
139148
changeStream.on('change', function(change) {
140149
expect(change).to.exist;
150+
151+
// Since changeStream has an implicit seession,
152+
// we need to close the changeStream for unit testing purposes
141153
changeStream.close();
142154
client.close();
143155
done();
@@ -196,6 +208,9 @@ describe('Changestream Examples', function() {
196208
if (err) return console.log(err);
197209
expect(err).to.equal(null);
198210
expect(next).to.exist;
211+
212+
// Since changeStream has an implicit seession,
213+
// we need to close the changeStream for unit testing purposes
199214
newChangeStream.close();
200215
client.close();
201216
done();
@@ -245,6 +260,8 @@ describe('Changestream Examples', function() {
245260
expect(next.newField).to.exist;
246261
expect(next.newField).to.equal('this is an added field!');
247262

263+
// Since changeStream has an implicit seession,
264+
// we need to close the changeStream for unit testing purposes
248265
changeStream.close();
249266
client.close();
250267
done();

test/functional/operation_example_tests.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,8 @@ describe('Operation Examples', function() {
364364
test.ok(docs);
365365
test.equal(null, err);
366366

367-
// Need to close cursor since batchsize = 1
367+
// Need to close cursor since cursor is not
368+
// exhausted, and implicit session is still open
368369
cursor.close();
369370
client.close();
370371
done();
@@ -6097,7 +6098,8 @@ describe('Operation Examples', function() {
60976098
test.equal(null, err);
60986099
test.equal(1, item.a);
60996100

6100-
// Need to close cursor, since it was not exhausted
6101+
// Need to close cursor, since it was not exhausted,
6102+
// and implicit session is still open
61016103
cursor.close();
61026104
client.close();
61036105
done();

test/functional/operation_generators_example_tests.js

+3
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,9 @@ describe('Operation (Generators)', function() {
172172
);
173173
// Get all the aggregation results
174174
yield cursor.next();
175+
176+
// Closing cursor to close implicit session,
177+
// since the cursor is not exhausted
175178
cursor.close();
176179
client.close();
177180
});

test/functional/operation_promises_example_tests.js

+3
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,9 @@ describe('Operation (Promises)', function() {
182182
})
183183
.then(function(docs) {
184184
test.ok(docs);
185+
186+
// Need to close cursor to close implicit session,
187+
// since cursor is not exhausted
185188
cursor.close();
186189
client.close();
187190
});

test/functional/sessions_tests.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ describe('Sessions', function() {
3939

4040
client.close(err => {
4141
expect(err).to.not.exist;
42-
expect(test.commands.started).to.have.length(2);
42+
expect(test.commands.started).to.have.length(1);
4343
expect(test.commands.started[0].commandName).to.equal('endSessions');
4444
expect(test.commands.started[0].command.endSessions).to.include.deep.members(sessions);
4545

0 commit comments

Comments
 (0)