@@ -28,9 +28,30 @@ describe('Client Backpressure (Prose)', function () {
2828 }
2929 } ,
3030 async function ( ) {
31+ // 1. Let `client` be a `MongoClient`
3132 client = this . configuration . newClient ( ) ;
3233 await client . connect ( ) ;
3334
35+ // 2. Let `collection` be a collection
36+ const collection = client . db ( 'foo' ) . collection ( 'bar' ) ;
37+
38+ // 3. Now, run transactions without backoff:
39+ // i. Configure the random number generator used for jitter to always return `0` -- this effectively disables backoff.
40+ const stub = sinon . stub ( Math , 'random' ) ;
41+ stub . returns ( 0 ) ;
42+
43+ // ii. Configure the following failPoint:
44+ // ```javascript
45+ // {
46+ // configureFailPoint: 'failCommand',
47+ // mode: 'alwaysOn',
48+ // data: {
49+ // failCommands: ['insert'],
50+ // errorCode: 2,
51+ // errorLabels: ['SystemOverloadedError', 'RetryableError']
52+ // }
53+ // }
54+ // ```
3455 await configureFailPoint ( this . configuration , {
3556 configureFailPoint : 'failCommand' ,
3657 mode : 'alwaysOn' ,
@@ -41,24 +62,23 @@ describe('Client Backpressure (Prose)', function () {
4162 }
4263 } ) ;
4364
44- const stub = sinon . stub ( Math , 'random' ) ;
45-
46- stub . returns ( 0 ) ;
47-
48- const collection = client . db ( 'foo' ) . collection ( 'bar' ) ;
49-
65+ // iii. Insert the document `{ a: 1 }`. Expect that the command errors. Measure the duration of the command execution.
5066 const { duration : durationNoBackoff } = await measureDuration ( async ( ) => {
5167 const error = await collection . insertOne ( { a : 1 } ) . catch ( e => e ) ;
5268 expect ( error ) . to . be . instanceof ( MongoServerError ) ;
5369 } ) ;
5470
71+ // iv. Configure the random number generator used for jitter to always return a number as close as possible to `1`.
5572 stub . returns ( 0.99 ) ;
5673
74+ // v. Execute step iii again.
5775 const { duration : durationBackoff } = await measureDuration ( async ( ) => {
5876 const error = await collection . insertOne ( { a : 1 } ) . catch ( e => e ) ;
5977 expect ( error ) . to . be . instanceof ( MongoServerError ) ;
6078 } ) ;
6179
80+ // vi. Compare the two time between the two runs.
81+ // The sum of 5 backoffs is 3.1 seconds. There is a 1-second window to account for potential variance between the two runs.
6282 expect ( durationBackoff - durationNoBackoff ) . to . be . within ( 3100 - 1000 , 3100 + 1000 ) ;
6383 }
6484 ) ;
0 commit comments