-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
server_description.test.ts
200 lines (190 loc) · 7.78 KB
/
server_description.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
import { expect } from 'chai';
import { MongoRuntimeError } from '../../../src';
import { Long, ObjectId } from '../../../src/bson';
import {
compareTopologyVersion,
ServerDescription,
TopologyVersion
} from '../../../src/sdam/server_description';
describe('ServerDescription', function () {
describe('constructor()', () => {
it('should throw if given a null address', () => {
// @ts-expect-error: Passing nullish value to prove error will be thrown
expect(() => new ServerDescription(null)).to.throw(MongoRuntimeError);
// @ts-expect-error: Passing nullish value to prove error will be thrown
expect(() => new ServerDescription()).to.throw(MongoRuntimeError);
});
it('should throw if given an empty string for an address', () => {
expect(() => new ServerDescription('')).to.throw(MongoRuntimeError);
});
});
describe('error equality', function () {
const errorEqualityTests = [
{
description: 'equal error types and messages',
lhs: new ServerDescription('127.0.0.1:27017', undefined, { error: new Error('test') }),
rhs: new ServerDescription('127.0.0.1:27017', undefined, { error: new Error('test') }),
equal: true
},
{
description: 'equal error types and unequal messages',
lhs: new ServerDescription('127.0.0.1:27017', undefined, { error: new Error('test') }),
rhs: new ServerDescription('127.0.0.1:27017', undefined, { error: new Error('blah') }),
equal: false
},
{
description: 'unequal error types and equal messages',
lhs: new ServerDescription('127.0.0.1:27017', undefined, { error: new TypeError('test') }),
rhs: new ServerDescription('127.0.0.1:27017', undefined, { error: new Error('test') }),
equal: false
},
{
description: 'null lhs',
lhs: new ServerDescription('127.0.0.1:27017', undefined, { error: null }),
rhs: new ServerDescription('127.0.0.1:27017', undefined, { error: new Error('test') }),
equal: false
},
{
description: 'null rhs',
lhs: new ServerDescription('127.0.0.1:27017', undefined, { error: new TypeError('test') }),
rhs: new ServerDescription('127.0.0.1:27017', undefined, { error: undefined }),
equal: false
}
];
for (const test of errorEqualityTests) {
it(test.description, function () {
expect(test.lhs.equals(test.rhs)).to.equal(test.equal);
});
}
});
it('should normalize an IPv6 address with brackets and toLowered characters', function () {
const description = new ServerDescription('[ABCD:f::abcd:abcd:abcd:abcd]:1234');
expect(description.host).to.equal('[abcd:f::abcd:abcd:abcd:abcd]'); // IPv6 Addresses must always be bracketed if there is a port
expect(description.port).to.equal(1234);
});
it('should normalize an IPv6 address with brackets and toLowered characters even when the port is omitted', function () {
const description = new ServerDescription('[ABCD:f::abcd:abcd:abcd:abcd]');
expect(description.host).to.equal('[abcd:f::abcd:abcd:abcd:abcd]');
expect(description.port).to.equal(27017);
});
describe('compareTopologyVersion()', () => {
const processIdZero = new ObjectId('00'.repeat(12));
const processIdOne = new ObjectId('00'.repeat(11) + '01');
type CompareTopologyVersionTest = {
title: string;
currentTv?: TopologyVersion | null;
newTv?: TopologyVersion | null;
out: 0 | -1 | 1;
};
const compareTopologyVersionEqualTests: CompareTopologyVersionTest[] = [
{
title: 'when process ids are equal and both counter values are Long.ZERO',
currentTv: { processId: processIdZero, counter: Long.ZERO },
newTv: { processId: processIdZero, counter: Long.ZERO },
out: 0
},
{
title: 'when process ids are equal and both counter values are non-zero numbers',
// @ts-expect-error: Testing that the function handles numbers
currentTv: { processId: processIdZero, counter: 2 },
// @ts-expect-error: Testing that the function handles numbers
newTv: { processId: processIdZero, counter: 2 },
out: 0
},
{
title: 'when process ids are equal and both counter values are zero numbers',
// @ts-expect-error: Testing that the function handles numbers
currentTv: { processId: processIdZero, counter: 0 },
// @ts-expect-error: Testing that the function handles numbers
newTv: { processId: processIdZero, counter: 0 },
out: 0
},
{
title:
'when process ids are equal and counter values are equal but current has a different type',
currentTv: { processId: processIdZero, counter: Long.fromNumber(2) },
// @ts-expect-error: Testing that the function handles numbers
newTv: { processId: processIdZero, counter: 2 },
out: 0
},
{
title:
'when process ids are equal and counter values are equal but new has a different type',
// @ts-expect-error: Testing that the function handles numbers
currentTv: { processId: processIdZero, counter: 2 },
newTv: { processId: processIdZero, counter: Long.fromNumber(2) },
out: 0
},
{
title: 'when process ids are equal and counter values are equal and both are Long',
currentTv: { processId: processIdZero, counter: Long.fromNumber(2) },
newTv: { processId: processIdZero, counter: Long.fromNumber(2) },
out: 0
}
];
const compareTopologyVersionLessThanTests: CompareTopologyVersionTest[] = [
{
title: 'when both versions are null',
currentTv: null,
newTv: null,
out: -1
},
{
title: 'when both versions are undefined',
currentTv: undefined,
newTv: undefined,
out: -1
},
{
title: 'when current is null and new is undefined',
currentTv: null,
newTv: undefined,
out: -1
},
{
title: 'when current is undefined and new is null',
currentTv: undefined,
newTv: null,
out: -1
},
{
title:
'when new processId is greater the topologyVersion is too regardless of counter being less',
// Even if processId of current is greater, it is not an ordered value
currentTv: { processId: processIdOne, counter: Long.fromNumber(2) },
newTv: { processId: processIdZero, counter: Long.fromNumber(1) },
out: -1
},
{
title: 'when processIds are equal but new counter is greater',
currentTv: { processId: processIdZero, counter: Long.fromNumber(2) },
newTv: { processId: processIdZero, counter: Long.fromNumber(3) },
out: -1
}
];
const compareTopologyVersionGreaterThanTests: CompareTopologyVersionTest[] = [
{
title: 'when processIds are equal but new counter is less than current',
currentTv: { processId: processIdZero, counter: Long.fromNumber(3) },
newTv: { processId: processIdZero, counter: Long.fromNumber(2) },
out: 1
}
];
const makeTopologyVersionComparisonTests = tests => {
for (const { title, currentTv, newTv, out } of tests) {
it(title, () => {
expect(compareTopologyVersion(currentTv, newTv)).to.equal(out);
});
}
};
context('should return that versions are equal', () => {
makeTopologyVersionComparisonTests(compareTopologyVersionEqualTests);
});
context('should return that current version is less than', () => {
makeTopologyVersionComparisonTests(compareTopologyVersionLessThanTests);
});
context('should return that current version is greater than', () => {
makeTopologyVersionComparisonTests(compareTopologyVersionGreaterThanTests);
});
});
});