|
| 1 | +import { expect } from 'chai'; |
| 2 | +import * as semver from 'semver'; |
| 3 | +import * as sinon from 'sinon'; |
| 4 | + |
| 5 | +import { type Connection, type MongoClient, type RTTPinger } from '../../../src'; |
| 6 | +import { LEGACY_HELLO_COMMAND } from '../../../src/constants'; |
| 7 | +import { sleep } from '../../tools/utils'; |
| 8 | + |
| 9 | +/** |
| 10 | + * RTTPingers are only created after getting a hello from the server that defines topologyVersion |
| 11 | + * Each monitor is reaching out to a different node and rttPinger's are created async as a result. |
| 12 | + * |
| 13 | + * This function checks for rttPingers and sleeps if none are found. |
| 14 | + */ |
| 15 | +async function getRTTPingers(client: MongoClient) { |
| 16 | + type RTTPingerConnection = Omit<RTTPinger, 'connection'> & { connection: Connection }; |
| 17 | + const pingers = (rtt => rtt?.connection != null) as (r?: RTTPinger) => r is RTTPingerConnection; |
| 18 | + |
| 19 | + if (!client.topology) expect.fail('Must provide a connected client'); |
| 20 | + |
| 21 | + // eslint-disable-next-line no-constant-condition |
| 22 | + while (true) { |
| 23 | + const servers = client.topology.s.servers.values(); |
| 24 | + const rttPingers = Array.from(servers, s => s.monitor?.rttPinger).filter(pingers); |
| 25 | + |
| 26 | + if (rttPingers.length !== 0) { |
| 27 | + return rttPingers; |
| 28 | + } |
| 29 | + |
| 30 | + await sleep(5); |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +describe('class RTTPinger', () => { |
| 35 | + afterEach(() => sinon.restore()); |
| 36 | + |
| 37 | + beforeEach(async function () { |
| 38 | + if (!this.currentTest) return; |
| 39 | + if (this.configuration.isLoadBalanced) { |
| 40 | + this.currentTest.skipReason = 'No monitoring in LB mode, test not relevant'; |
| 41 | + return this.skip(); |
| 42 | + } |
| 43 | + if (semver.gte('4.4.0', this.configuration.version)) { |
| 44 | + this.currentTest.skipReason = |
| 45 | + 'Test requires streaming monitoring, needs to be on MongoDB 4.4+'; |
| 46 | + return this.skip(); |
| 47 | + } |
| 48 | + }); |
| 49 | + |
| 50 | + context('when serverApi is enabled', () => { |
| 51 | + let serverApiClient: MongoClient; |
| 52 | + |
| 53 | + beforeEach(async function () { |
| 54 | + if (!this.currentTest) return; |
| 55 | + |
| 56 | + if (semver.gte('5.0.0', this.configuration.version)) { |
| 57 | + this.currentTest.skipReason = 'Test requires serverApi, needs to be on MongoDB 5.0+'; |
| 58 | + return this.skip(); |
| 59 | + } |
| 60 | + |
| 61 | + serverApiClient = this.configuration.newClient( |
| 62 | + {}, |
| 63 | + { serverApi: { version: '1', strict: true }, heartbeatFrequencyMS: 10 } |
| 64 | + ); |
| 65 | + }); |
| 66 | + |
| 67 | + afterEach(async () => { |
| 68 | + await serverApiClient?.close(); |
| 69 | + }); |
| 70 | + |
| 71 | + it('measures rtt with a hello command', async function () { |
| 72 | + await serverApiClient.connect(); |
| 73 | + const rttPingers = await getRTTPingers(serverApiClient); |
| 74 | + |
| 75 | + const spies = rttPingers.map(rtt => sinon.spy(rtt.connection, 'command')); |
| 76 | + |
| 77 | + await sleep(11); // allow for another ping after spies have been made |
| 78 | + |
| 79 | + expect(spies).to.have.lengthOf.at.least(1); |
| 80 | + for (const spy of spies) { |
| 81 | + expect(spy).to.have.been.calledWith(sinon.match.any, { hello: 1 }, sinon.match.any); |
| 82 | + } |
| 83 | + }); |
| 84 | + }); |
| 85 | + |
| 86 | + context('when serverApi is disabled', () => { |
| 87 | + let client: MongoClient; |
| 88 | + |
| 89 | + beforeEach(async function () { |
| 90 | + if (!this.currentTest) return; |
| 91 | + if (this.configuration.serverApi) { |
| 92 | + this.currentTest.skipReason = 'Test requires serverApi to NOT be enabled'; |
| 93 | + return this.skip(); |
| 94 | + } |
| 95 | + |
| 96 | + client = this.configuration.newClient({}, { heartbeatFrequencyMS: 10 }); |
| 97 | + }); |
| 98 | + |
| 99 | + afterEach(async () => { |
| 100 | + await client?.close(); |
| 101 | + }); |
| 102 | + |
| 103 | + context('connected to a pre-hello server', () => { |
| 104 | + it('measures rtt with a LEGACY_HELLO_COMMAND command', async function () { |
| 105 | + await client.connect(); |
| 106 | + const rttPingers = await getRTTPingers(client); |
| 107 | + |
| 108 | + // Fake pre-hello server. |
| 109 | + // Hello was back-ported to feature versions of the server so we would need to pin |
| 110 | + // versions prior to 4.4.2, 4.2.10, 4.0.21, and 3.6.21 to integration test |
| 111 | + for (const rtt of rttPingers) rtt.connection.helloOk = false; |
| 112 | + |
| 113 | + const spies = rttPingers.map(rtt => sinon.spy(rtt.connection, 'command')); |
| 114 | + |
| 115 | + await sleep(11); // allow for another ping after spies have been made |
| 116 | + |
| 117 | + expect(spies).to.have.lengthOf.at.least(1); |
| 118 | + for (const spy of spies) { |
| 119 | + expect(spy).to.have.been.calledWith( |
| 120 | + sinon.match.any, |
| 121 | + { [LEGACY_HELLO_COMMAND]: 1 }, |
| 122 | + sinon.match.any |
| 123 | + ); |
| 124 | + } |
| 125 | + }); |
| 126 | + }); |
| 127 | + |
| 128 | + context('connected to a helloOk server', () => { |
| 129 | + it('measures rtt with a hello command', async function () { |
| 130 | + await client.connect(); |
| 131 | + const rttPingers = await getRTTPingers(client); |
| 132 | + |
| 133 | + const spies = rttPingers.map(rtt => sinon.spy(rtt.connection, 'command')); |
| 134 | + |
| 135 | + // We should always be connected to helloOk servers |
| 136 | + for (const rtt of rttPingers) expect(rtt.connection).to.have.property('helloOk', true); |
| 137 | + |
| 138 | + await sleep(11); // allow for another ping after spies have been made |
| 139 | + |
| 140 | + expect(spies).to.have.lengthOf.at.least(1); |
| 141 | + for (const spy of spies) { |
| 142 | + expect(spy).to.have.been.calledWith(sinon.match.any, { hello: 1 }, sinon.match.any); |
| 143 | + } |
| 144 | + }); |
| 145 | + }); |
| 146 | + }); |
| 147 | + |
| 148 | + context(`when the RTTPinger's hello command receives any error`, () => { |
| 149 | + let client: MongoClient; |
| 150 | + beforeEach(async function () { |
| 151 | + client = this.configuration.newClient({}, { heartbeatFrequencyMS: 10 }); |
| 152 | + }); |
| 153 | + |
| 154 | + afterEach(async () => { |
| 155 | + await client?.close(); |
| 156 | + }); |
| 157 | + |
| 158 | + it('destroys the connection with force=true', async function () { |
| 159 | + await client.connect(); |
| 160 | + const rttPingers = await getRTTPingers(client); |
| 161 | + |
| 162 | + for (const rtt of rttPingers) { |
| 163 | + sinon.stub(rtt.connection, 'command').yieldsRight(new Error('any error')); |
| 164 | + } |
| 165 | + const spies = rttPingers.map(rtt => sinon.spy(rtt.connection, 'destroy')); |
| 166 | + |
| 167 | + await sleep(11); // allow for another ping after spies have been made |
| 168 | + |
| 169 | + expect(spies).to.have.lengthOf.at.least(1); |
| 170 | + for (const spy of spies) { |
| 171 | + expect(spy).to.have.been.calledWithExactly({ force: true }); |
| 172 | + } |
| 173 | + }); |
| 174 | + }); |
| 175 | +}); |
0 commit comments