/
read_preference.test.ts
134 lines (117 loc) · 5.39 KB
/
read_preference.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
import { ReadPreference } from '../../src';
import { expect } from 'chai';
describe('class ReadPreference', function () {
const maxStalenessSeconds = 1234;
const { PRIMARY, SECONDARY, NEAREST } = ReadPreference;
const TAGS = [{ loc: 'dc' }];
describe('::constructor', function () {
it('should accept (mode)', function () {
expect(new ReadPreference(PRIMARY)).to.be.an.instanceOf(ReadPreference);
});
it('should accept valid (mode, tags)', function () {
expect(new ReadPreference(PRIMARY, [])).to.be.an.instanceOf(ReadPreference);
const p0 = new ReadPreference(NEAREST, TAGS);
expect(p0).to.have.property('mode', NEAREST);
});
it('should not accept invalid tags', function () {
expect(() => new ReadPreference(PRIMARY, 'invalid' as any)).to.throw(
'ReadPreference tags must be an array'
);
expect(
() => new ReadPreference(PRIMARY, { loc: 'dc' } as any, { maxStalenessSeconds })
).to.throw('ReadPreference tags must be an array');
});
it('should accept (mode, options)', function () {
const p1 = new ReadPreference(SECONDARY, { maxStalenessSeconds } as any);
expect(p1.mode).to.equal(SECONDARY);
expect(p1).to.have.property('maxStalenessSeconds', maxStalenessSeconds);
});
it('should not accept mode=primary + tags', function () {
expect(() => new ReadPreference(PRIMARY, TAGS)).to.throw(
'Primary read preference cannot be combined with tags'
);
});
it('should not accept mode=primary + options.maxStalenessSeconds', function () {
expect(() => new ReadPreference(PRIMARY, null, { maxStalenessSeconds })).to.throw(
'Primary read preference cannot be combined with maxStalenessSeconds'
);
});
it('should not accept mode=primary + options.hedge enabled', function () {
expect(() => new ReadPreference(PRIMARY, null, { hedge: { enabled: true } })).to.throw(
'Primary read preference cannot be combined with hedge'
);
});
it('should accept (mode=secondary, tags=null, options)', function () {
const p2 = new ReadPreference(SECONDARY, null, { maxStalenessSeconds });
expect(p2).to.be.an.instanceOf(ReadPreference);
expect(p2).to.have.property('mode', SECONDARY);
expect(p2).to.have.property('maxStalenessSeconds', maxStalenessSeconds);
});
it('should accept (mode=secondary, tags, options)', function () {
const p3 = new ReadPreference(SECONDARY, TAGS, { maxStalenessSeconds });
expect(p3).to.be.an.instanceOf(ReadPreference);
expect(p3).to.have.property('mode', SECONDARY);
expect(p3.tags).to.deep.equal(TAGS);
expect(p3).to.have.property('maxStalenessSeconds', maxStalenessSeconds);
});
it('should not accept (mode, options, tags)', function () {
expect(
() => new ReadPreference(PRIMARY, { maxStalenessSeconds } as any, TAGS as any)
).to.throw('ReadPreference tags must be an array');
});
});
describe('fromOptions factory method', () => {
it('should return undefined if no options are passed', () => {
const readPreference = ReadPreference.fromOptions();
expect(readPreference).to.be.undefined;
});
context('readPreference is string', () => {
it('should accept { readPreference }', function () {
const readPreference = ReadPreference.fromOptions({
readPreference: PRIMARY
});
expect(readPreference).to.be.an.instanceOf(ReadPreference);
expect(readPreference).to.have.property('mode', PRIMARY);
});
it('should accept { readPreference, readPreferenceTags }', function () {
const readPreference = ReadPreference.fromOptions({
readPreference: SECONDARY,
readPreferenceTags: TAGS
});
expect(readPreference).to.be.an.instanceOf(ReadPreference);
expect(readPreference).to.have.property('mode', SECONDARY);
expect(readPreference.tags).to.deep.equal(TAGS);
});
it('should accept { readPreference, maxStalenessSeconds }', function () {
const readPreference = ReadPreference.fromOptions({
readPreference: SECONDARY,
maxStalenessSeconds: maxStalenessSeconds
});
expect(readPreference).to.be.an.instanceOf(ReadPreference);
expect(readPreference).to.have.property('mode', SECONDARY);
expect(readPreference).to.have.property('maxStalenessSeconds', maxStalenessSeconds);
});
it('should accept { readPreference, hedge }', function () {
const readPreference = ReadPreference.fromOptions({
readPreference: SECONDARY,
hedge: {
enabled: true
}
});
expect(readPreference).to.be.an.instanceOf(ReadPreference);
expect(readPreference).to.have.property('mode', SECONDARY);
expect(readPreference.hedge).to.deep.equal({ enabled: true });
});
});
it('should not accept mode=primary + options.hedge', function () {
expect(() =>
ReadPreference.fromOptions({ readPreference: PRIMARY, hedge: { enabled: true } })
).to.throw('Primary read preference cannot be combined with hedge');
});
it('should not accept mode=primary + options.maxStalenessSeconds', function () {
expect(() =>
ReadPreference.fromOptions({ readPreference: PRIMARY, maxStalenessSeconds })
).to.throw('Primary read preference cannot be combined with maxStalenessSeconds');
});
});
});