|
| 1 | +import { expect } from 'chai'; |
| 2 | +import { test } from 'mocha'; |
| 3 | + |
| 4 | +import { type CommandFailedEvent, type MongoClient } from '../../mongodb'; |
| 5 | +import { configureFailPoint } from '../../tools/utils'; |
| 6 | +import { filterForCommands } from '../shared'; |
| 7 | + |
| 8 | +describe('Retry Backoff is Enforced', function () { |
| 9 | + // Drivers should test that retries within `withTransaction` do not occur immediately. Optionally, set BACKOFF_INITIAL to a |
| 10 | + // higher value to decrease flakiness of this test. Configure a fail point that forces 30 retries. Check that the total |
| 11 | + // time for all retries exceeded 1.25 seconds. |
| 12 | + |
| 13 | + let client: MongoClient; |
| 14 | + let failures: Array<CommandFailedEvent>; |
| 15 | + |
| 16 | + beforeEach(async function () { |
| 17 | + client = this.configuration.newClient({}, { monitorCommands: true }); |
| 18 | + |
| 19 | + failures = []; |
| 20 | + client.on('commandFailed', filterForCommands('commitTransaction', failures)); |
| 21 | + |
| 22 | + await client.connect(); |
| 23 | + |
| 24 | + await configureFailPoint(this.configuration, { |
| 25 | + configureFailPoint: 'failCommand', |
| 26 | + mode: { |
| 27 | + times: 30 |
| 28 | + }, |
| 29 | + data: { |
| 30 | + failCommands: ['commitTransaction'], |
| 31 | + errorCode: 24, |
| 32 | + errorLabels: ['UnknownTransactionCommitResult'] |
| 33 | + } |
| 34 | + }); |
| 35 | + }); |
| 36 | + |
| 37 | + afterEach(async function () { |
| 38 | + await client?.close(); |
| 39 | + }); |
| 40 | + |
| 41 | + for (let i = 0; i < 250; ++i) { |
| 42 | + test.only('works' + i, async function () { |
| 43 | + const start = performance.now(); |
| 44 | + |
| 45 | + await client.withSession(async s => { |
| 46 | + await s.withTransaction(async s => { |
| 47 | + await client.db('foo').collection('bar').insertOne({ name: 'bailey' }, { session: s }); |
| 48 | + }); |
| 49 | + }); |
| 50 | + |
| 51 | + const end = performance.now(); |
| 52 | + |
| 53 | + expect(failures).to.have.lengthOf(30); |
| 54 | + |
| 55 | + expect(end - start).to.be.greaterThan(1250); |
| 56 | + }); |
| 57 | + } |
| 58 | +}); |
0 commit comments