|
1 | 1 | import { expect } from 'chai'; |
2 | 2 | import { test } from 'mocha'; |
| 3 | +import * as sinon from 'sinon'; |
3 | 4 |
|
4 | | -import { type CommandFailedEvent, type MongoClient } from '../../mongodb'; |
5 | | -import { configureFailPoint } from '../../tools/utils'; |
6 | | -import { filterForCommands } from '../shared'; |
| 5 | +import { type MongoClient } from '../../../src'; |
| 6 | +import { configureFailPoint, type FailCommandFailPoint, measureDuration } from '../../tools/utils'; |
7 | 7 |
|
8 | | -const COMMIT_FAIL_TIMES = 35; |
| 8 | +const failCommand: FailCommandFailPoint = { |
| 9 | + configureFailPoint: 'failCommand', |
| 10 | + mode: { |
| 11 | + times: 13 |
| 12 | + }, |
| 13 | + data: { |
| 14 | + failCommands: ['commitTransaction'], |
| 15 | + errorCode: 251 |
| 16 | + } |
| 17 | +}; |
9 | 18 |
|
10 | 19 | describe('Retry Backoff is Enforced', function () { |
11 | | - // Drivers should test that retries within `withTransaction` do not occur immediately. Optionally, set BACKOFF_INITIAL to a |
12 | | - // higher value to decrease flakiness of this test. Configure a fail point that forces 30 retries. Check that the total |
13 | | - // time for all retries exceeded 1.25 seconds. |
14 | | - |
15 | 20 | let client: MongoClient; |
16 | | - let failures: Array<CommandFailedEvent>; |
17 | 21 |
|
18 | 22 | beforeEach(async function () { |
19 | | - client = this.configuration.newClient({}, { monitorCommands: true }); |
20 | | - |
21 | | - failures = []; |
22 | | - client.on('commandFailed', filterForCommands('commitTransaction', failures)); |
23 | | - |
24 | | - await client.connect(); |
25 | | - |
26 | | - await configureFailPoint(this.configuration, { |
27 | | - configureFailPoint: 'failCommand', |
28 | | - mode: { |
29 | | - times: COMMIT_FAIL_TIMES |
30 | | - }, |
31 | | - data: { |
32 | | - failCommands: ['commitTransaction'], |
33 | | - errorCode: 24 |
34 | | - } |
35 | | - }); |
| 23 | + client = this.configuration.newClient(); |
36 | 24 | }); |
37 | 25 |
|
38 | 26 | afterEach(async function () { |
| 27 | + sinon.restore(); |
39 | 28 | await client?.close(); |
40 | 29 | }); |
41 | 30 |
|
42 | | - for (let i = 0; i < 250; ++i) { |
43 | | - test.only( |
44 | | - 'works' + i, |
45 | | - { |
46 | | - requires: { |
47 | | - mongodb: '>=4.4', // failCommand |
48 | | - topology: '!single' // transactions can't run on standalone servers |
49 | | - } |
50 | | - }, |
51 | | - async function () { |
52 | | - const start = performance.now(); |
| 31 | + test( |
| 32 | + 'works', |
| 33 | + { |
| 34 | + requires: { |
| 35 | + mongodb: '>=4.4', // failCommand |
| 36 | + topology: '!single' // transactions can't run on standalone servers |
| 37 | + } |
| 38 | + }, |
| 39 | + async function () { |
| 40 | + const randomStub = sinon.stub(Math, 'random'); |
| 41 | + |
| 42 | + randomStub.returns(0); |
53 | 43 |
|
54 | | - await client.withSession(async s => { |
| 44 | + await configureFailPoint(this.configuration, failCommand); |
| 45 | + |
| 46 | + const { duration: noBackoffTime } = await measureDuration(() => { |
| 47 | + return client.withSession(async s => { |
55 | 48 | await s.withTransaction(async s => { |
56 | 49 | await client.db('foo').collection('bar').insertOne({ name: 'bailey' }, { session: s }); |
57 | 50 | }); |
58 | 51 | }); |
| 52 | + }); |
59 | 53 |
|
60 | | - const end = performance.now(); |
| 54 | + randomStub.returns(1); |
61 | 55 |
|
62 | | - expect(failures).to.have.lengthOf(COMMIT_FAIL_TIMES); |
| 56 | + await configureFailPoint(this.configuration, failCommand); |
63 | 57 |
|
64 | | - expect(end - start).to.be.greaterThan(1250); |
65 | | - } |
66 | | - ); |
67 | | - } |
| 58 | + const { duration: fullBackoffDuration } = await measureDuration(() => { |
| 59 | + return client.withSession(async s => { |
| 60 | + await s.withTransaction(async s => { |
| 61 | + await client.db('foo').collection('bar').insertOne({ name: 'bailey' }, { session: s }); |
| 62 | + }); |
| 63 | + }); |
| 64 | + }); |
| 65 | + |
| 66 | + expect(fullBackoffDuration).to.be.within( |
| 67 | + noBackoffTime + 2200 - 1000, |
| 68 | + noBackoffTime + 2200 + 1000 |
| 69 | + ); |
| 70 | + } |
| 71 | + ); |
68 | 72 | }); |
0 commit comments