|
| 1 | +/* eslint-disable @typescript-eslint/no-non-null-assertion */ |
| 2 | +import { expect } from 'chai'; |
| 3 | +import { once } from 'events'; |
| 4 | +import * as sinon from 'sinon'; |
| 5 | + |
| 6 | +import { |
| 7 | + Connection, |
| 8 | + MongoError, |
| 9 | + MongoErrorLabel, |
| 10 | + MongoNetworkError, |
| 11 | + MongoNetworkTimeoutError, |
| 12 | + ObjectId, |
| 13 | + ServerType, |
| 14 | + TopologyType |
| 15 | +} from '../../../src'; |
| 16 | +import { Server } from '../../../src/sdam/server'; |
| 17 | +import { ServerDescription } from '../../../src/sdam/server_description'; |
| 18 | +import { Topology } from '../../../src/sdam/topology'; |
| 19 | +import { sleep } from '../../tools/utils'; |
| 20 | + |
| 21 | +const handledErrors = [ |
| 22 | + { |
| 23 | + description: 'any non-timeout network error', |
| 24 | + errorClass: MongoNetworkError, |
| 25 | + errorArgs: ['TestError'] |
| 26 | + }, |
| 27 | + { |
| 28 | + description: 'a network timeout error before handshake', |
| 29 | + errorClass: MongoNetworkTimeoutError, |
| 30 | + errorArgs: ['TestError', { beforeHandshake: true }] |
| 31 | + }, |
| 32 | + { |
| 33 | + description: 'an auth handshake error', |
| 34 | + errorClass: MongoError, |
| 35 | + errorArgs: ['TestError'], |
| 36 | + errorLabel: MongoErrorLabel.HandshakeError |
| 37 | + } |
| 38 | +]; |
| 39 | + |
| 40 | +const unhandledErrors = [ |
| 41 | + { |
| 42 | + description: 'a non-MongoError', |
| 43 | + errorClass: Error, |
| 44 | + errorArgs: ['TestError'] |
| 45 | + }, |
| 46 | + { |
| 47 | + description: 'a network timeout error after handshake', |
| 48 | + errorClass: MongoNetworkTimeoutError, |
| 49 | + errorArgs: ['TestError', { beforeHandshake: false }] |
| 50 | + }, |
| 51 | + { |
| 52 | + description: 'a non-network non-handshake MongoError', |
| 53 | + errorClass: MongoError, |
| 54 | + errorArgs: ['TestError'] |
| 55 | + } |
| 56 | +]; |
| 57 | + |
| 58 | +describe('Server', () => { |
| 59 | + describe('#handleError', () => { |
| 60 | + let server: Server, connection: Connection | undefined; |
| 61 | + beforeEach(() => { |
| 62 | + server = new Server(new Topology([], {} as any), new ServerDescription('a:1'), {} as any); |
| 63 | + }); |
| 64 | + for (const loadBalanced of [true, false]) { |
| 65 | + const mode = loadBalanced ? 'loadBalanced' : 'non-loadBalanced'; |
| 66 | + const contextSuffix = loadBalanced ? ' with connection provided' : ''; |
| 67 | + context(`in ${mode} mode${contextSuffix}`, () => { |
| 68 | + beforeEach(() => { |
| 69 | + if (loadBalanced) { |
| 70 | + server.s.topology.description.type = TopologyType.LoadBalanced; |
| 71 | + connection = { serviceId: new ObjectId() } as Connection; |
| 72 | + server.s.pool.clear = sinon.stub(); |
| 73 | + } else { |
| 74 | + connection = undefined; |
| 75 | + } |
| 76 | + }); |
| 77 | + for (const { description, errorClass, errorArgs, errorLabel } of handledErrors) { |
| 78 | + const handledDescription = loadBalanced |
| 79 | + ? `should reset the pool but not attach a ResetPool label to the error or mark the server unknown on ${description}` |
| 80 | + : `should attach a ResetPool label to the error and mark the server unknown on ${description}`; |
| 81 | + it(`${handledDescription}`, async () => { |
| 82 | + // @ts-expect-error because of varied number of args |
| 83 | + const error = new errorClass(...errorArgs); |
| 84 | + if (errorLabel) { |
| 85 | + error.addErrorLabel(errorLabel); |
| 86 | + } |
| 87 | + const newDescriptionEvent = Promise.race([ |
| 88 | + once(server, Server.DESCRIPTION_RECEIVED), |
| 89 | + sleep(1000) |
| 90 | + ]); |
| 91 | + server.handleError(error, connection); |
| 92 | + if (!loadBalanced) { |
| 93 | + expect( |
| 94 | + error.hasErrorLabel(MongoErrorLabel.ResetPool), |
| 95 | + 'expected error to have a ResetPool label' |
| 96 | + ).to.be.true; |
| 97 | + } else { |
| 98 | + expect( |
| 99 | + error.hasErrorLabel(MongoErrorLabel.ResetPool), |
| 100 | + 'expected error NOT to have a ResetPool label' |
| 101 | + ).to.be.false; |
| 102 | + } |
| 103 | + const newDescription = await newDescriptionEvent; |
| 104 | + if (!loadBalanced) { |
| 105 | + expect(newDescription).to.have.nested.property('[0].type', ServerType.Unknown); |
| 106 | + } else { |
| 107 | + expect(newDescription).to.be.undefined; |
| 108 | + expect(server.s.pool.clear).to.have.been.calledOnceWith(connection!.serviceId); |
| 109 | + } |
| 110 | + }); |
| 111 | + |
| 112 | + it(`should not attach a ResetPool label to the error or mark the server unknown on ${description} if it is stale`, async () => { |
| 113 | + // @ts-expect-error because of varied number of args |
| 114 | + const error = new errorClass(...errorArgs); |
| 115 | + if (errorLabel) { |
| 116 | + error.addErrorLabel(errorLabel); |
| 117 | + } |
| 118 | + |
| 119 | + error.connectionGeneration = -1; |
| 120 | + expect( |
| 121 | + server.s.pool.generation, |
| 122 | + 'expected test server to have a pool of generation 0' |
| 123 | + ).to.equal(0); // sanity check |
| 124 | + |
| 125 | + const newDescriptionEvent = Promise.race([ |
| 126 | + once(server, Server.DESCRIPTION_RECEIVED), |
| 127 | + sleep(1000) |
| 128 | + ]); |
| 129 | + server.handleError(error, connection); |
| 130 | + expect( |
| 131 | + error.hasErrorLabel(MongoErrorLabel.ResetPool), |
| 132 | + 'expected error NOT to have a ResetPool label' |
| 133 | + ).to.be.false; |
| 134 | + const newDescription = await newDescriptionEvent; |
| 135 | + expect(newDescription).to.be.undefined; |
| 136 | + }); |
| 137 | + } |
| 138 | + |
| 139 | + for (const { description, errorClass, errorArgs } of unhandledErrors) { |
| 140 | + it(`should not attach a ResetPool label to the error or mark the server unknown on ${description}`, async () => { |
| 141 | + // @ts-expect-error because of varied number of args |
| 142 | + const error = new errorClass(...errorArgs); |
| 143 | + |
| 144 | + const newDescriptionEvent = Promise.race([ |
| 145 | + once(server, Server.DESCRIPTION_RECEIVED), |
| 146 | + sleep(1000) |
| 147 | + ]); |
| 148 | + server.handleError(error, connection); |
| 149 | + if (error instanceof MongoError) { |
| 150 | + expect( |
| 151 | + error.hasErrorLabel(MongoErrorLabel.ResetPool), |
| 152 | + 'expected error NOT to have a ResetPool label' |
| 153 | + ).to.be.false; |
| 154 | + } |
| 155 | + const newDescription = await newDescriptionEvent; |
| 156 | + expect(newDescription).to.be.undefined; |
| 157 | + }); |
| 158 | + } |
| 159 | + }); |
| 160 | + } |
| 161 | + }); |
| 162 | +}); |
0 commit comments