Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(NODE-3705): ReadPreference.fromOptions omitting hedge and maxStal…
…enessSeconds when readPreference is a string (#3060)
- Loading branch information
Bailey Pearson
committed
Dec 1, 2021
1 parent
7b00d0f
commit b9fbac5
Showing
4 changed files
with
139 additions
and
65 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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'); | ||
}); | ||
}); | ||
}); |