-
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.
Browse files
Browse the repository at this point in the history
Co-authored-by: Aditi Khare <[email protected]>
- Loading branch information
1 parent
70d476a
commit fd902d3
Showing
3 changed files
with
196 additions
and
4 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
164 changes: 164 additions & 0 deletions
164
test/integration/initial-dns-seedlist-discovery/dns_seedlist.test.ts
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,164 @@ | ||
import { expect } from 'chai'; | ||
import * as dns from 'dns'; | ||
import * as sinon from 'sinon'; | ||
|
||
import { MongoClient } from '../../mongodb'; | ||
|
||
const metadata: MongoDBMetadataUI = { requires: { topology: '!single' } }; | ||
|
||
// This serves as a placeholder for _whatever_ node.js may throw. We only rely upon `.code` | ||
class DNSTimeoutError extends Error { | ||
code = 'ETIMEOUT'; | ||
} | ||
// This serves as a placeholder for _whatever_ node.js may throw. We only rely upon `.code` | ||
class DNSSomethingError extends Error { | ||
code = undefined; | ||
} | ||
|
||
const CONNECTION_STRING = `mongodb+srv://test1.test.build.10gen.cc`; | ||
|
||
describe('DNS timeout errors', () => { | ||
let client: MongoClient; | ||
let stub; | ||
|
||
beforeEach(async function () { | ||
client = new MongoClient(CONNECTION_STRING, { serverSelectionTimeoutMS: 2000, tls: false }); | ||
}); | ||
|
||
afterEach(async function () { | ||
stub = undefined; | ||
sinon.restore(); | ||
await client.close(); | ||
}); | ||
|
||
const restoreDNS = | ||
api => | ||
async (...args) => { | ||
sinon.restore(); | ||
return await dns.promises[api](...args); | ||
}; | ||
|
||
describe('when SRV record look up times out', () => { | ||
beforeEach(() => { | ||
stub = sinon | ||
.stub(dns.promises, 'resolveSrv') | ||
.onFirstCall() | ||
.rejects(new DNSTimeoutError()) | ||
.onSecondCall() | ||
.callsFake(restoreDNS('resolveSrv')); | ||
}); | ||
|
||
afterEach(async function () { | ||
sinon.restore(); | ||
}); | ||
|
||
it('retries timeout error', metadata, async () => { | ||
await client.connect(); | ||
expect(stub).to.have.been.calledTwice; | ||
}); | ||
}); | ||
|
||
describe('when TXT record look up times out', () => { | ||
beforeEach(() => { | ||
stub = sinon | ||
.stub(dns.promises, 'resolveTxt') | ||
.onFirstCall() | ||
.rejects(new DNSTimeoutError()) | ||
.onSecondCall() | ||
.callsFake(restoreDNS('resolveTxt')); | ||
}); | ||
|
||
afterEach(async function () { | ||
sinon.restore(); | ||
}); | ||
|
||
it('retries timeout error', metadata, async () => { | ||
await client.connect(); | ||
expect(stub).to.have.been.calledTwice; | ||
}); | ||
}); | ||
|
||
describe('when SRV record look up times out twice', () => { | ||
beforeEach(() => { | ||
stub = sinon | ||
.stub(dns.promises, 'resolveSrv') | ||
.onFirstCall() | ||
.rejects(new DNSTimeoutError()) | ||
.onSecondCall() | ||
.rejects(new DNSTimeoutError()); | ||
}); | ||
|
||
afterEach(async function () { | ||
sinon.restore(); | ||
}); | ||
|
||
it('throws timeout error', metadata, async () => { | ||
const error = await client.connect().catch(error => error); | ||
expect(error).to.be.instanceOf(DNSTimeoutError); | ||
expect(stub).to.have.been.calledTwice; | ||
}); | ||
}); | ||
|
||
describe('when TXT record look up times out twice', () => { | ||
beforeEach(() => { | ||
stub = sinon | ||
.stub(dns.promises, 'resolveTxt') | ||
.onFirstCall() | ||
.rejects(new DNSTimeoutError()) | ||
.onSecondCall() | ||
.rejects(new DNSTimeoutError()); | ||
}); | ||
|
||
afterEach(async function () { | ||
sinon.restore(); | ||
}); | ||
|
||
it('throws timeout error', metadata, async () => { | ||
const error = await client.connect().catch(error => error); | ||
expect(error).to.be.instanceOf(DNSTimeoutError); | ||
expect(stub).to.have.been.calledTwice; | ||
}); | ||
}); | ||
|
||
describe('when SRV record look up throws a non-timeout error', () => { | ||
beforeEach(() => { | ||
stub = sinon | ||
.stub(dns.promises, 'resolveSrv') | ||
.onFirstCall() | ||
.rejects(new DNSSomethingError()) | ||
.onSecondCall() | ||
.callsFake(restoreDNS('resolveSrv')); | ||
}); | ||
|
||
afterEach(async function () { | ||
sinon.restore(); | ||
}); | ||
|
||
it('throws that error', metadata, async () => { | ||
const error = await client.connect().catch(error => error); | ||
expect(error).to.be.instanceOf(DNSSomethingError); | ||
expect(stub).to.have.been.calledOnce; | ||
}); | ||
}); | ||
|
||
describe('when TXT record look up throws a non-timeout error', () => { | ||
beforeEach(() => { | ||
stub = sinon | ||
.stub(dns.promises, 'resolveTxt') | ||
.onFirstCall() | ||
.rejects(new DNSSomethingError()) | ||
.onSecondCall() | ||
.callsFake(restoreDNS('resolveTxt')); | ||
}); | ||
|
||
afterEach(async function () { | ||
sinon.restore(); | ||
}); | ||
|
||
it('throws that error', metadata, async () => { | ||
const error = await client.connect().catch(error => error); | ||
expect(error).to.be.instanceOf(DNSSomethingError); | ||
expect(stub).to.have.been.calledOnce; | ||
}); | ||
}); | ||
}); |