-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathdns_seedlist.test.ts
More file actions
139 lines (119 loc) · 4.15 KB
/
dns_seedlist.test.ts
File metadata and controls
139 lines (119 loc) · 4.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import { expect } from 'chai';
import * as dns from 'dns';
import * as sinon from 'sinon';
import { MongoClient } from '../../mongodb';
const metadata: MongoDBMetadataUI = { requires: { topology: '!single', tls: 'disabled' } };
// 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 });
stub = sinon.stub(dns.promises, 'resolve').callThrough();
});
afterEach(async function () {
stub = undefined;
sinon.restore();
await client.close();
});
const restoreDNS = rrtype => async hostname => {
stub.restore();
return await dns.promises.resolve(hostname, rrtype);
};
describe('when SRV record look up times out', () => {
beforeEach(() => {
stub
.withArgs(sinon.match.string, 'SRV')
.onFirstCall()
.rejects(new DNSTimeoutError())
.onSecondCall()
.callsFake(restoreDNS('SRV'));
});
it('retries timeout error', metadata, async () => {
await client.connect();
expect(stub.withArgs(sinon.match.string, 'SRV')).to.have.been.calledTwice;
});
});
describe('when TXT record look up times out', () => {
beforeEach(() => {
stub
.withArgs(sinon.match.string, 'TXT')
.onFirstCall()
.rejects(new DNSTimeoutError())
.onSecondCall()
.callsFake(restoreDNS('TXT'));
});
it('retries timeout error', metadata, async () => {
await client.connect();
expect(stub.withArgs(sinon.match.string, 'TXT')).to.have.been.calledTwice;
});
});
describe('when SRV record look up times out twice', () => {
beforeEach(() => {
stub
.withArgs(sinon.match.string, 'SRV')
.onFirstCall()
.rejects(new DNSTimeoutError())
.onSecondCall()
.rejects(new DNSTimeoutError());
});
it('throws timeout error', metadata, async () => {
const error = await client.connect().catch(error => error);
expect(error).to.be.instanceOf(DNSTimeoutError);
expect(stub.withArgs(sinon.match.string, 'SRV')).to.have.been.calledTwice;
});
});
describe('when TXT record look up times out twice', () => {
beforeEach(() => {
stub
.withArgs(sinon.match.string, 'TXT')
.onFirstCall()
.rejects(new DNSTimeoutError())
.onSecondCall()
.rejects(new DNSTimeoutError());
});
it('throws timeout error', metadata, async () => {
const error = await client.connect().catch(error => error);
expect(error).to.be.instanceOf(DNSTimeoutError);
expect(stub.withArgs(sinon.match.string, 'TXT')).to.have.been.calledTwice;
});
});
describe('when SRV record look up throws a non-timeout error', () => {
beforeEach(() => {
stub
.withArgs(sinon.match.string, 'SRV')
.onFirstCall()
.rejects(new DNSSomethingError())
.onSecondCall()
.callsFake(restoreDNS('SRV'));
});
it('throws that error', metadata, async () => {
const error = await client.connect().catch(error => error);
expect(error).to.be.instanceOf(DNSSomethingError);
expect(stub.withArgs(sinon.match.string, 'SRV')).to.have.been.calledOnce;
});
});
describe('when TXT record look up throws a non-timeout error', () => {
beforeEach(() => {
stub
.withArgs(sinon.match.string, 'TXT')
.onFirstCall()
.rejects(new DNSSomethingError())
.onSecondCall()
.callsFake(restoreDNS('TXT'));
});
it('throws that error', metadata, async () => {
const error = await client.connect().catch(error => error);
expect(error).to.be.instanceOf(DNSSomethingError);
expect(stub.withArgs(sinon.match.string, 'TXT')).to.have.been.calledOnce;
});
});
});