-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(NODE-5749): RTTPinger always sends legacy hello
- Loading branch information
Showing
3 changed files
with
122 additions
and
41 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
86 changes: 86 additions & 0 deletions
86
test/integration/connection-monitoring-and-pooling/rtt_pinger.test.ts
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,86 @@ | ||
import { expect } from 'chai'; | ||
import * as semver from 'semver'; | ||
import * as sinon from 'sinon'; | ||
|
||
import { type MongoClient } from '../../mongodb'; | ||
import { sleep } from '../../tools/utils'; | ||
|
||
describe('class RTTPinger', () => { | ||
afterEach(() => sinon.restore()); | ||
|
||
context('when serverApi is enabled', () => { | ||
let serverApiClient: MongoClient; | ||
beforeEach(async function () { | ||
if (semver.gte('5.0.0', this.configuration.version)) { | ||
if (this.currentTest) | ||
this.currentTest.skipReason = 'Test requires serverApi, needs to be on MongoDB 5.0+'; | ||
return this.skip(); | ||
} | ||
|
||
serverApiClient = this.configuration.newClient( | ||
{}, | ||
{ serverApi: { version: '1', strict: true }, heartbeatFrequencyMS: 10 } | ||
); | ||
}); | ||
|
||
afterEach(async () => { | ||
await serverApiClient?.close(); | ||
}); | ||
|
||
it('measures rtt with a hello command', async function () { | ||
await serverApiClient.connect(); | ||
await sleep(1001); // rttPinger creation | ||
|
||
const rttPingers = Array.from(serverApiClient.topology?.s.servers.values() ?? [], s => { | ||
if (s.monitor?.rttPinger) return s.monitor?.rttPinger; | ||
else expect.fail('expected rttPinger to be defined'); | ||
}); | ||
|
||
await sleep(11); // rttPinger connection creation | ||
|
||
const spies = rttPingers.map(rtt => sinon.spy(rtt.connection!, 'command')); | ||
|
||
await sleep(11); // allow for another ping after spies have been made | ||
|
||
expect(spies).to.have.length.above(1); | ||
for (const spy of spies) { | ||
expect(spy).to.have.been.calledWith(sinon.match.any, { hello: 1 }, sinon.match.any); | ||
} | ||
}); | ||
}); | ||
|
||
context('when rtt hello receives an error', () => { | ||
let client: MongoClient; | ||
beforeEach(async function () { | ||
client = this.configuration.newClient({}, { heartbeatFrequencyMS: 10 }); | ||
}); | ||
|
||
afterEach(async () => { | ||
await client?.close(); | ||
}); | ||
|
||
it('destroys the connection', async function () { | ||
await client.connect(); | ||
await sleep(1001); // rttPinger creation | ||
|
||
const rttPingers = Array.from(client.topology?.s.servers.values() ?? [], s => { | ||
if (s.monitor?.rttPinger) return s.monitor?.rttPinger; | ||
else expect.fail('expected rttPinger to be defined'); | ||
}); | ||
|
||
await sleep(11); // rttPinger connection creation | ||
|
||
const spies = rttPingers.map(rtt => | ||
sinon.stub(rtt.connection!, 'command').yieldsRight(new Error('any')) | ||
); | ||
const destroySpies = rttPingers.map(rtt => sinon.spy(rtt.connection!, 'destroy')); | ||
|
||
await sleep(11); // allow for another ping after spies have been made | ||
|
||
expect(destroySpies).to.have.length.above(1); | ||
for (const spy of spies) { | ||
expect(spy).to.have.been.called; | ||
} | ||
}); | ||
}); | ||
}); |