-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
server.test.ts
164 lines (155 loc) · 5.85 KB
/
server.test.ts
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/* eslint-disable @typescript-eslint/no-non-null-assertion */
import { expect } from 'chai';
import { once } from 'events';
import * as sinon from 'sinon';
import {
Connection,
MongoError,
MongoErrorLabel,
MongoNetworkError,
MongoNetworkTimeoutError,
ObjectId,
ServerType,
TopologyType
} from '../../../src';
import { Server } from '../../../src/sdam/server';
import { ServerDescription } from '../../../src/sdam/server_description';
import { Topology } from '../../../src/sdam/topology';
import { sleep } from '../../tools/utils';
const handledErrors = [
{
description: 'any non-timeout network error',
errorClass: MongoNetworkError,
errorArgs: ['TestError']
},
{
description: 'a network timeout error before handshake',
errorClass: MongoNetworkTimeoutError,
errorArgs: ['TestError', { beforeHandshake: true }]
},
{
description: 'an auth handshake error',
errorClass: MongoError,
errorArgs: ['TestError'],
errorLabel: MongoErrorLabel.HandshakeError
}
];
const unhandledErrors = [
{
description: 'a non-MongoError',
errorClass: Error,
errorArgs: ['TestError']
},
{
description: 'a network timeout error after handshake',
errorClass: MongoNetworkTimeoutError,
errorArgs: ['TestError', { beforeHandshake: false }]
},
{
description: 'a non-network non-handshake MongoError',
errorClass: MongoError,
errorArgs: ['TestError']
}
];
describe('Server', () => {
describe('#handleError', () => {
let server: Server, connection: Connection | undefined;
beforeEach(() => {
server = new Server(new Topology([], {} as any), new ServerDescription('a:1'), {} as any);
});
for (const loadBalanced of [true, false]) {
const mode = loadBalanced ? 'loadBalanced' : 'non-loadBalanced';
const contextSuffix = loadBalanced ? ' with connection provided' : '';
context(`in ${mode} mode${contextSuffix}`, () => {
beforeEach(() => {
if (loadBalanced) {
server.s.topology.description.type = TopologyType.LoadBalanced;
connection = { serviceId: new ObjectId() } as Connection;
server.s.pool.clear = sinon.stub();
} else {
connection = undefined;
}
});
for (const { description, errorClass, errorArgs, errorLabel } of handledErrors) {
const handledDescription = loadBalanced
? `should reset the pool but not attach a ResetPool label to the error or mark the server unknown on ${description}`
: `should attach a ResetPool label to the error and mark the server unknown on ${description}`;
it(`${handledDescription}`, async () => {
// @ts-expect-error because of varied number of args
const error = new errorClass(...errorArgs);
if (errorLabel) {
error.addErrorLabel(errorLabel);
}
const newDescriptionEvent = Promise.race([
once(server, Server.DESCRIPTION_RECEIVED),
sleep(1000)
]);
server.handleError(error, connection);
if (!loadBalanced) {
expect(
error.hasErrorLabel(MongoErrorLabel.ResetPool),
'expected error to have a ResetPool label'
).to.be.true;
} else {
expect(
error.hasErrorLabel(MongoErrorLabel.ResetPool),
'expected error NOT to have a ResetPool label'
).to.be.false;
}
const newDescription = await newDescriptionEvent;
if (!loadBalanced) {
expect(newDescription).to.have.nested.property('[0].type', ServerType.Unknown);
} else {
expect(newDescription).to.be.undefined;
expect(server.s.pool.clear).to.have.been.calledOnceWith({
serviceId: connection!.serviceId
});
}
});
it(`should not attach a ResetPool label to the error or mark the server unknown on ${description} if it is stale`, async () => {
// @ts-expect-error because of varied number of args
const error = new errorClass(...errorArgs);
if (errorLabel) {
error.addErrorLabel(errorLabel);
}
error.connectionGeneration = -1;
expect(
server.s.pool.generation,
'expected test server to have a pool of generation 0'
).to.equal(0); // sanity check
const newDescriptionEvent = Promise.race([
once(server, Server.DESCRIPTION_RECEIVED),
sleep(1000)
]);
server.handleError(error, connection);
expect(
error.hasErrorLabel(MongoErrorLabel.ResetPool),
'expected error NOT to have a ResetPool label'
).to.be.false;
const newDescription = await newDescriptionEvent;
expect(newDescription).to.be.undefined;
});
}
for (const { description, errorClass, errorArgs } of unhandledErrors) {
it(`should not attach a ResetPool label to the error or mark the server unknown on ${description}`, async () => {
// @ts-expect-error because of varied number of args
const error = new errorClass(...errorArgs);
const newDescriptionEvent = Promise.race([
once(server, Server.DESCRIPTION_RECEIVED),
sleep(1000)
]);
server.handleError(error, connection);
if (error instanceof MongoError) {
expect(
error.hasErrorLabel(MongoErrorLabel.ResetPool),
'expected error NOT to have a ResetPool label'
).to.be.false;
}
const newDescription = await newDescriptionEvent;
expect(newDescription).to.be.undefined;
});
}
});
}
});
});