-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
fix(NODE-3272)!: emit correct event type when SRV Polling #2825
Merged
Merged
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
34cd935
fix(NODE-3272)!: emit correct event type when SRV Polling
nbbeeken f57f2f0
fix: clean up conditional logic and simplify test
nbbeeken b198ac4
test: more modular testing
nbbeeken 41813d9
test: pass current topology to helper
nbbeeken 35cb3ef
test: dedupe beforeEach
nbbeeken 9f90bb1
fix: hanging test issue
nbbeeken 8cc4d77
test: add afterEach clean up
nbbeeken 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 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
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 |
---|---|---|
|
@@ -7,6 +7,11 @@ const { Topology } = require('../../../src/sdam/topology'); | |
const { Server } = require('../../../src/sdam/server'); | ||
const { ServerDescription } = require('../../../src/sdam/server_description'); | ||
const { ns, makeClientMetadata } = require('../../../src/utils'); | ||
const { TopologyDescriptionChangedEvent } = require('../../../src/sdam/events'); | ||
const { TopologyDescription } = require('../../../src/sdam/topology_description'); | ||
const { TopologyType } = require('../../../src/sdam/common'); | ||
const { SrvPoller, SrvPollingEvent } = require('../../../src/sdam/srv_polling'); | ||
const { getSymbolFrom } = require('../../tools/utils'); | ||
|
||
describe('Topology (unit)', function () { | ||
describe('client metadata', function () { | ||
|
@@ -314,5 +319,108 @@ describe('Topology (unit)', function () { | |
}); | ||
return p; | ||
}); | ||
|
||
describe('srv event listeners', function () { | ||
/** @type {Topology} */ | ||
let topology; | ||
|
||
beforeEach(() => { | ||
topology = new Topology('', { srvHost: 'fakeHost' }); | ||
|
||
expect(topology.s.detectSrvRecords).to.be.a('function'); | ||
expect(topology.s.detectShardedTopology).to.be.a('function'); | ||
}); | ||
|
||
function transitionTopology(topology, from, to) { | ||
topology.emit( | ||
Topology.TOPOLOGY_DESCRIPTION_CHANGED, | ||
new TopologyDescriptionChangedEvent( | ||
2, | ||
new TopologyDescription(from), | ||
new TopologyDescription(to) | ||
) | ||
); | ||
// We don't want the SrvPoller to actually run | ||
clearTimeout(topology.s.srvPoller._timeout); | ||
} | ||
|
||
describe('srvRecordDiscovery event listener', function () { | ||
beforeEach(() => { | ||
// fake a transition to Sharded | ||
transitionTopology(topology, TopologyType.Unknown, TopologyType.Sharded); | ||
expect(topology.s.srvPoller).to.be.instanceOf(SrvPoller); | ||
|
||
const srvPollerListeners = topology.s.srvPoller.listeners(SrvPoller.SRV_RECORD_DISCOVERY); | ||
expect(srvPollerListeners).to.have.lengthOf(1); | ||
expect(srvPollerListeners[0]).to.equal(topology.s.detectSrvRecords); | ||
const topologyChangeListeners = topology.listeners(Topology.TOPOLOGY_DESCRIPTION_CHANGED); | ||
expect(topologyChangeListeners).to.have.lengthOf(1); | ||
expect(topologyChangeListeners[0]).to.equal(topology.s.detectShardedTopology); | ||
}); | ||
|
||
it('should emit topologyDescriptionChange event', function () { | ||
topology.once(Topology.TOPOLOGY_DESCRIPTION_CHANGED, ev => { | ||
// The first event we get here is caused by the srv record discovery event below | ||
expect(ev).to.have.nested.property('newDescription.servers'); | ||
expect(ev.newDescription.servers.get('fake:2')) | ||
.to.be.a('object') | ||
.with.property('address', 'fake:2'); | ||
}); | ||
|
||
topology.s.srvPoller.emit( | ||
SrvPoller.SRV_RECORD_DISCOVERY, | ||
new SrvPollingEvent([{ priority: 1, weight: 1, port: 2, name: 'fake' }]) | ||
); | ||
|
||
// The srv event starts a monitor that we don't actually want running | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. let's move the cleanup into an afterEach and iterate over all servers There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, good idea 🚀 |
||
const server = topology.s.servers.get('fake:2'); | ||
const kMonitor = getSymbolFrom(server, 'monitor'); | ||
const kMonitorId = getSymbolFrom(server[kMonitor], 'monitorId'); | ||
server[kMonitor][kMonitorId].stop(); | ||
}); | ||
|
||
it('should clean up listeners on close', function (done) { | ||
topology.s.state = 'connected'; // fake state to test clean up logic | ||
topology.close(e => { | ||
const srvPollerListeners = topology.s.srvPoller.listeners( | ||
SrvPoller.SRV_RECORD_DISCOVERY | ||
); | ||
expect(srvPollerListeners).to.have.lengthOf(0); | ||
const topologyChangeListeners = topology.listeners( | ||
Topology.TOPOLOGY_DESCRIPTION_CHANGED | ||
); | ||
expect(topologyChangeListeners).to.have.lengthOf(0); | ||
done(e); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('topologyDescriptionChange event listener', function () { | ||
it('should not add more than one srvRecordDiscovery listener', function () { | ||
// fake a transition to Sharded | ||
transitionTopology(topology, TopologyType.Unknown, TopologyType.Sharded); // Transition 1 | ||
|
||
const srvListenersFirstTransition = topology.s.srvPoller.listeners( | ||
SrvPoller.SRV_RECORD_DISCOVERY | ||
); | ||
expect(srvListenersFirstTransition).to.have.lengthOf(1); | ||
|
||
transitionTopology(topology, TopologyType.Unknown, TopologyType.Sharded); // Transition 2 | ||
|
||
const srvListenersSecondTransition = topology.s.srvPoller.listeners( | ||
SrvPoller.SRV_RECORD_DISCOVERY | ||
); | ||
expect(srvListenersSecondTransition).to.have.lengthOf(1); | ||
}); | ||
|
||
it('should not add srvRecordDiscovery listener if transition is not to Sharded topology', function () { | ||
// fake a transition to **NOT** Sharded | ||
transitionTopology(topology, TopologyType.Unknown, TopologyType.ReplicaSetWithPrimary); | ||
|
||
const srvListeners = topology.s.srvPoller.listeners(SrvPoller.SRV_RECORD_DISCOVERY); | ||
expect(srvListeners).to.have.lengthOf(0); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cool util! 🎉
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, and good catch on the other ticket I'll be sure to note it in the merge