-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(NODE-5106): refactor connect locking and add test
- Loading branch information
1 parent
b139f17
commit ed65746
Showing
2 changed files
with
103 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { expect } from 'chai'; | ||
import * as sinon from 'sinon'; | ||
|
||
import { MongoClient } from '../../src'; | ||
|
||
describe('MongoClient', () => { | ||
let client: MongoClient; | ||
let topologyOpenEvents; | ||
|
||
beforeEach(async function () { | ||
client = this.configuration.newClient(); | ||
topologyOpenEvents = []; | ||
client.on('open', event => topologyOpenEvents.push(event)); | ||
}); | ||
|
||
afterEach(async function () { | ||
await client.close(); | ||
}); | ||
|
||
it('Concurrents client connect correctly locked (only one topology created)', async function () { | ||
await Promise.all([client.connect(), client.connect(), client.connect()]); | ||
|
||
expect(topologyOpenEvents).to.have.lengthOf(1); | ||
expect(client.topology?.isConnected()).to.be.true; | ||
}); | ||
|
||
it('Failed client connect must properly release lock', async function () { | ||
const internalConnectStub = sinon.stub(client, '_connect' as keyof MongoClient); | ||
internalConnectStub.onFirstCall().rejects(); | ||
|
||
// first call rejected to simulate a connection failure | ||
try { | ||
await client.connect(); | ||
} catch (err) { | ||
expect(err).to.exist; | ||
} | ||
|
||
internalConnectStub.restore(); | ||
|
||
// second call should connect | ||
try { | ||
await client.connect(); | ||
} catch (err) { | ||
expect.fail(`client connect throwed unexpected error`); | ||
} | ||
|
||
expect(topologyOpenEvents).to.have.lengthOf(1); | ||
expect(client.topology?.isConnected()).to.be.true; | ||
}); | ||
}); |