Skip to content

Commit 4e52381

Browse files
committed
do not error on double connect, move tests into describe block
1 parent 8238837 commit 4e52381

File tree

2 files changed

+44
-44
lines changed

2 files changed

+44
-44
lines changed

src/operations/connect.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -197,9 +197,9 @@ export function connect(
197197
throw new Error('no callback function provided');
198198
}
199199

200-
// Has a connection already been established?
200+
// If a connection already been established, we can terminate early
201201
if (mongoClient.topology && mongoClient.topology.isConnected()) {
202-
return callback(new MongoError('MongoClient is already connected'));
202+
return callback(undefined, mongoClient);
203203
}
204204

205205
let didRequestAuthentication = false;

test/functional/connection.test.js

Lines changed: 42 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -273,58 +273,58 @@ describe('Connection', function () {
273273
done();
274274
}
275275
});
276-
});
277-
278-
it(
279-
'should be able to connect again after close',
280-
withClient(function (client, done) {
281-
expect(client.isConnected()).to.be.true;
282276

283-
const collection = client.db('shouldConnectAfterClose').collection('test');
284-
collection.insertOne({ a: 1, b: 2 }, (err, result) => {
285-
expect(err).to.not.exist;
286-
expect(result).to.exist;
277+
it(
278+
'should be able to connect again after close',
279+
withClient(function (client, done) {
280+
expect(client.isConnected()).to.be.true;
287281

288-
client.close(err => {
282+
const collection = client.db('shouldConnectAfterClose').collection('test');
283+
collection.insertOne({ a: 1, b: 2 }, (err, result) => {
289284
expect(err).to.not.exist;
290-
expect(client.isConnected()).to.be.false;
285+
expect(result).to.exist;
291286

292-
client.connect(err => {
287+
client.close(err => {
293288
expect(err).to.not.exist;
294-
expect(client.isConnected()).to.be.true;
289+
expect(client.isConnected()).to.be.false;
295290

296-
collection.findOne({ a: 1 }, (err, result) => {
291+
client.connect(err => {
297292
expect(err).to.not.exist;
298-
expect(result).to.exist;
299-
expect(result).to.have.property('a', 1);
300-
expect(result).to.have.property('b', 2);
301-
expect(client.topology.isDestroyed()).to.be.false;
302-
done();
293+
expect(client.isConnected()).to.be.true;
294+
295+
collection.findOne({ a: 1 }, (err, result) => {
296+
expect(err).to.not.exist;
297+
expect(result).to.exist;
298+
expect(result).to.have.property('a', 1);
299+
expect(result).to.have.property('b', 2);
300+
expect(client.topology.isDestroyed()).to.be.false;
301+
done();
302+
});
303303
});
304304
});
305305
});
306-
});
307-
})
308-
);
309-
310-
it(
311-
'should correctly fail on retry when client has been closed',
312-
withClient(function (client, done) {
313-
expect(client.isConnected()).to.be.true;
314-
const collection = client.db('shouldCorrectlyFailOnRetry').collection('test');
315-
collection.insertOne({ a: 1 }, (err, result) => {
316-
expect(err).to.not.exist;
317-
expect(result).to.exist;
318-
319-
client.close(true, function (err) {
306+
})
307+
);
308+
309+
it(
310+
'should correctly fail on retry when client has been closed',
311+
withClient(function (client, done) {
312+
expect(client.isConnected()).to.be.true;
313+
const collection = client.db('shouldCorrectlyFailOnRetry').collection('test');
314+
collection.insertOne({ a: 1 }, (err, result) => {
320315
expect(err).to.not.exist;
321-
expect(client.isConnected()).to.be.false;
316+
expect(result).to.exist;
322317

323-
expect(() => {
324-
collection.insertOne({ a: 2 });
325-
}).to.throw(/must be connected/);
326-
done();
318+
client.close(true, function (err) {
319+
expect(err).to.not.exist;
320+
expect(client.isConnected()).to.be.false;
321+
322+
expect(() => {
323+
collection.insertOne({ a: 2 });
324+
}).to.throw(/must be connected/);
325+
done();
326+
});
327327
});
328-
});
329-
})
330-
);
328+
})
329+
);
330+
});

0 commit comments

Comments
 (0)