Skip to content

Commit ba9a9e4

Browse files
tests added
1 parent 68da446 commit ba9a9e4

File tree

3 files changed

+37
-3
lines changed

3 files changed

+37
-3
lines changed

src/mongo_client.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,7 @@ export class MongoClient extends TypedEventEmitter<MongoClientEvents> implements
367367
/** @internal */
368368
private connectionLock?: Promise<this>;
369369
/** @internal */
370-
private closeLock?: Promise<void>
370+
private closeLock?: Promise<void>;
371371

372372
/**
373373
* The consolidate, parsed, transformed and merged options.
@@ -642,14 +642,14 @@ export class MongoClient extends TypedEventEmitter<MongoClientEvents> implements
642642
async close(force = false): Promise<void> {
643643
if (this.closeLock) {
644644
return await this.closeLock;
645-
}
645+
}
646646

647647
try {
648648
this.closeLock = this._close(force);
649649
await this.closeLock;
650650
} finally {
651651
// release
652-
this.connectionLock = undefined;
652+
this.closeLock = undefined;
653653
}
654654
}
655655

test/integration/node-specific/mongo_client.test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -722,6 +722,22 @@ describe('class MongoClient', function () {
722722
expect(client.s.sessionPool.sessions).to.have.lengthOf(1);
723723
});
724724
});
725+
726+
context('concurrent calls', () => {
727+
context('when two concurrent calls to close() occur', () => {
728+
it('no error is thrown', async function () {
729+
expect(() => Promise.all([client.close(), client.close()])).to.not.throw;
730+
});
731+
});
732+
733+
context('when more than two concurrent calls to close() occur', () => {
734+
it('no error is thrown', async function () {
735+
expect(() =>
736+
Promise.all([client.close(), client.close(), client.close(), client.close()])
737+
).to.not.throw;
738+
});
739+
});
740+
});
725741
});
726742

727743
context('when connecting', function () {

test/unit/mongo_client.test.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1223,4 +1223,22 @@ describe('MongoClient', function () {
12231223
});
12241224
});
12251225
});
1226+
1227+
describe('closeLock', function () {
1228+
let client;
1229+
1230+
beforeEach(async function () {
1231+
client = new MongoClient('mongodb://blah');
1232+
});
1233+
1234+
it('when client.close is pending, client.closeLock is set', () => {
1235+
client.close();
1236+
expect(client.closeLock).to.be.instanceOf(Promise);
1237+
});
1238+
1239+
it('when client.close is not pending, client.closeLock is not set', async () => {
1240+
await client.close();
1241+
expect(client.closeLock).to.be.undefined;
1242+
});
1243+
});
12261244
});

0 commit comments

Comments
 (0)