|
| 1 | +import { expect } from 'chai'; |
| 2 | + |
| 3 | +import type { CommandFailedEvent, CommandSucceededEvent } from '../../mongodb'; |
| 4 | + |
| 5 | +const TEST_METADATA = { requires: { mongodb: '>=4.2.9', topology: 'sharded' } }; |
| 6 | +const FAIL_COMMAND = { |
| 7 | + configureFailPoint: 'failCommand', |
| 8 | + mode: { times: 1 }, |
| 9 | + data: { |
| 10 | + failCommands: ['find'], |
| 11 | + errorCode: 6, |
| 12 | + closeConnection: true |
| 13 | + } |
| 14 | +}; |
| 15 | +const DISABLE_FAIL_COMMAND = { |
| 16 | + configureFailPoint: 'failCommand', |
| 17 | + mode: 'off', |
| 18 | + data: { |
| 19 | + failCommands: ['find'], |
| 20 | + errorCode: 6, |
| 21 | + closeConnection: true |
| 22 | + } |
| 23 | +}; |
| 24 | + |
| 25 | +describe('Server Selection Sharded Retryable Reads Prose tests', function () { |
| 26 | + context('Retryable Reads Are Retried on a Different mongos if One is Available', function () { |
| 27 | + const commandFailedEvents: CommandFailedEvent[] = []; |
| 28 | + let client; |
| 29 | + let utilClientOne; |
| 30 | + let utilClientTwo; |
| 31 | + |
| 32 | + // This test MUST be executed against a sharded cluster that has at least two |
| 33 | + // mongos instances. |
| 34 | + // 1. Ensure that a test is run against a sharded cluster that has at least two |
| 35 | + // mongoses. If there are more than two mongoses in the cluster, pick two to |
| 36 | + // test against. |
| 37 | + beforeEach(async function () { |
| 38 | + const uri = this.configuration.url({ |
| 39 | + monitorCommands: true, |
| 40 | + useMultipleMongoses: true |
| 41 | + }); |
| 42 | + |
| 43 | + // 3. Create a client with ``retryReads=true`` that connects to the cluster, |
| 44 | + // providing the two selected mongoses as seeds. |
| 45 | + client = this.configuration.newClient(uri, { |
| 46 | + monitorCommands: true, |
| 47 | + retryReads: true |
| 48 | + }); |
| 49 | + client.on('commandFailed', event => { |
| 50 | + commandFailedEvents.push(event); |
| 51 | + }); |
| 52 | + await client.connect(); |
| 53 | + const seeds = client.topology.s.seedlist.map(address => address.toString()); |
| 54 | + |
| 55 | + // 2. Create a client per mongos using the direct connection, and configure the |
| 56 | + // following fail points on each mongos:: |
| 57 | + // { |
| 58 | + // configureFailPoint: "failCommand", |
| 59 | + // mode: { times: 1 }, |
| 60 | + // data: { |
| 61 | + // failCommands: ["find"], |
| 62 | + // errorCode: 6, |
| 63 | + // closeConnection: true |
| 64 | + // } |
| 65 | + // } |
| 66 | + utilClientOne = this.configuration.newClient(`mongodb://${seeds[0]}`, { |
| 67 | + directConnection: true |
| 68 | + }); |
| 69 | + utilClientTwo = this.configuration.newClient(`mongodb://${seeds[1]}`, { |
| 70 | + directConnection: true |
| 71 | + }); |
| 72 | + await utilClientOne.db('admin').command(FAIL_COMMAND); |
| 73 | + await utilClientTwo.db('admin').command(FAIL_COMMAND); |
| 74 | + }); |
| 75 | + |
| 76 | + afterEach(async function () { |
| 77 | + await client?.close(); |
| 78 | + await utilClientOne.db('admin').command(DISABLE_FAIL_COMMAND); |
| 79 | + await utilClientTwo.db('admin').command(DISABLE_FAIL_COMMAND); |
| 80 | + await utilClientOne?.close(); |
| 81 | + await utilClientTwo?.close(); |
| 82 | + }); |
| 83 | + |
| 84 | + // 4. Enable command monitoring, and execute a ``find`` command that is |
| 85 | + // supposed to fail on both mongoses. |
| 86 | + // 5. Asserts that there were failed command events from each mongos. |
| 87 | + // 6. Disable the fail points. |
| 88 | + it('retries on a different mongos', TEST_METADATA, async function () { |
| 89 | + await client |
| 90 | + .db('test') |
| 91 | + .collection('test') |
| 92 | + .find() |
| 93 | + .toArray() |
| 94 | + .catch(() => null); |
| 95 | + expect(commandFailedEvents[0].address).to.not.equal(commandFailedEvents[1].address); |
| 96 | + }); |
| 97 | + }); |
| 98 | + |
| 99 | + // 1. Ensure that a test is run against a sharded cluster. If there are multiple |
| 100 | + // mongoses in the cluster, pick one to test against. |
| 101 | + context('Retryable Reads Are Retried on the Same mongos if No Others are Available', function () { |
| 102 | + const commandFailedEvents: CommandFailedEvent[] = []; |
| 103 | + const commandSucceededEvents: CommandSucceededEvent[] = []; |
| 104 | + let client; |
| 105 | + let utilClient; |
| 106 | + |
| 107 | + beforeEach(async function () { |
| 108 | + const uri = this.configuration.url({ |
| 109 | + monitorCommands: true |
| 110 | + }); |
| 111 | + |
| 112 | + // 3. Create a client with ``retryReads=true`` that connects to the cluster, |
| 113 | + // providing the selected mongos as the seed. |
| 114 | + client = this.configuration.newClient(uri, { |
| 115 | + monitorCommands: true, |
| 116 | + retryReads: true |
| 117 | + }); |
| 118 | + client.on('commandFailed', event => { |
| 119 | + commandFailedEvents.push(event); |
| 120 | + }); |
| 121 | + client.on('commandSucceeded', event => { |
| 122 | + commandSucceededEvents.push(event); |
| 123 | + }); |
| 124 | + |
| 125 | + // 2. Create a client that connects to the mongos using the direct connection, |
| 126 | + // and configure the following fail point on the mongos:: |
| 127 | + // { |
| 128 | + // configureFailPoint: "failCommand", |
| 129 | + // mode: { times: 1 }, |
| 130 | + // data: { |
| 131 | + // failCommands: ["find"], |
| 132 | + // errorCode: 6, |
| 133 | + // closeConnection: true |
| 134 | + // } |
| 135 | + // } |
| 136 | + utilClient = this.configuration.newClient(uri, { |
| 137 | + directConnection: true |
| 138 | + }); |
| 139 | + await utilClient.db('admin').command(FAIL_COMMAND); |
| 140 | + }); |
| 141 | + |
| 142 | + afterEach(async function () { |
| 143 | + await client?.close(); |
| 144 | + await utilClient?.db('admin').command(DISABLE_FAIL_COMMAND); |
| 145 | + await utilClient?.close(); |
| 146 | + }); |
| 147 | + |
| 148 | + // 4. Enable command monitoring, and execute a ``find`` command. |
| 149 | + // 5. Asserts that there was a failed command and a successful command event. |
| 150 | + // 6. Disable the fail point. |
| 151 | + it('retries on the same mongos', TEST_METADATA, async function () { |
| 152 | + await client |
| 153 | + .db('test') |
| 154 | + .collection('test') |
| 155 | + .find() |
| 156 | + .toArray() |
| 157 | + .catch(() => null); |
| 158 | + expect(commandFailedEvents[0].address).to.equal(commandSucceededEvents[0].address); |
| 159 | + }); |
| 160 | + }); |
| 161 | +}); |
0 commit comments