-
Notifications
You must be signed in to change notification settings - Fork 1.8k
fix(NODE-3705): ReadPreference.fromOptions omitting hedge and maxStalenessSeconds when readPreference is a string #3060
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
nbbeeken
merged 12 commits into
main
from
NODE-3705/enable-hedge-options-when-readPreference-is-string
Dec 1, 2021
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
9fa15a7
fix(NODE-3705): propagate hedge settings through ReadPreference.fromO…
f4b6460
Move ::constructor tests and new fromOptions tests to unit testing di…
baileympearson f4670bc
Add maxStalenessSeconds option to read preference construction
baileympearson f03d1fe
Clean up unit tests for read preference
baileympearson 4af66df
Update unit tests
baileympearson 136de75
Remove .only from spec file
baileympearson a215994
Merge branch 'main' of https://github.com/mongodb/node-mongodb-native…
baileympearson 369f450
Move test file to typescript
baileympearson 0b2cdfd
Dummy commit to test authorship
2307258
Empty commit - test authorship
b0b54d1
Add meself as contributor
baileympearson 01cbc7a
Test commit signature verify
baileympearson File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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'); | ||
}); | ||
}); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.