@@ -2,7 +2,7 @@ import { expect } from 'chai';
22import { test } from 'mocha' ;
33import * as sinon from 'sinon' ;
44
5- import { type MongoClient } from '../../../src' ;
5+ import { type ClientSession , type Collection , type MongoClient } from '../../../src' ;
66import { configureFailPoint , type FailCommandFailPoint , measureDuration } from '../../tools/utils' ;
77
88const failCommand : FailCommandFailPoint = {
@@ -12,15 +12,20 @@ const failCommand: FailCommandFailPoint = {
1212 } ,
1313 data : {
1414 failCommands : [ 'commitTransaction' ] ,
15- errorCode : 251
15+ errorCode : 251 // no such transaction
1616 }
1717} ;
1818
1919describe ( 'Retry Backoff is Enforced' , function ( ) {
20+ // 1. let client be a MongoClient
2021 let client : MongoClient ;
2122
23+ // 2. let coll be a collection
24+ let collection : Collection ;
25+
2226 beforeEach ( async function ( ) {
2327 client = this . configuration . newClient ( ) ;
28+ collection = client . db ( 'foo' ) . collection ( 'bar' ) ;
2429 } ) ;
2530
2631 afterEach ( async function ( ) {
@@ -39,30 +44,40 @@ describe('Retry Backoff is Enforced', function () {
3944 async function ( ) {
4045 const randomStub = sinon . stub ( Math , 'random' ) ;
4146
47+ // 3.i Configure the random number generator used for jitter to always return 0
4248 randomStub . returns ( 0 ) ;
4349
50+ // 3.ii Configure a fail point that forces 13 retries
4451 await configureFailPoint ( this . configuration , failCommand ) ;
4552
53+ // 3.iii
54+ const callback = async ( s : ClientSession ) => {
55+ await collection . insertOne ( { } , { session : s } ) ;
56+ } ;
57+
58+ // 3.iv Let no_backoff_time be the duration of the withTransaction API call
4659 const { duration : noBackoffTime } = await measureDuration ( ( ) => {
4760 return client . withSession ( async s => {
48- await s . withTransaction ( async s => {
49- await client . db ( 'foo' ) . collection ( 'bar' ) . insertOne ( { name : 'bailey' } , { session : s } ) ;
50- } ) ;
61+ await s . withTransaction ( callback ) ;
5162 } ) ;
5263 } ) ;
5364
65+ // 4.i Configure the random number generator used for jitter to always return 1.
5466 randomStub . returns ( 1 ) ;
5567
68+ // 4.ii Configure a fail point that forces 13 retries like in step 3.2.
5669 await configureFailPoint ( this . configuration , failCommand ) ;
5770
71+ // 4.iii Use the same callback defined in 3.3.
72+ // 4.iv Let with_backoff_time be the duration of the withTransaction API call
5873 const { duration : fullBackoffDuration } = await measureDuration ( ( ) => {
5974 return client . withSession ( async s => {
60- await s . withTransaction ( async s => {
61- await client . db ( 'foo' ) . collection ( 'bar' ) . insertOne ( { name : 'bailey' } , { session : s } ) ;
62- } ) ;
75+ await s . withTransaction ( callback ) ;
6376 } ) ;
6477 } ) ;
6578
79+ // 5. Compare the two time between the two runs.
80+ // The sum of 13 backoffs is roughly 2.2 seconds. There is a 1-second window to account for potential variance between the two runs.
6681 expect ( fullBackoffDuration ) . to . be . within (
6782 noBackoffTime + 2200 - 1000 ,
6883 noBackoffTime + 2200 + 1000
0 commit comments