@@ -2,7 +2,7 @@ import { expect } from 'chai';
22import * as sinon from 'sinon' ;
33
44import { AbstractCursor , Collection , ConnectionPool , MongoClient } from '../../mongodb' ;
5- import { FailPoint , sleep } from '../../tools/utils' ;
5+ import { FailPoint } from '../../tools/utils' ;
66
77const testMetadata : MongoDBMetadataUI = {
88 requires : {
@@ -118,20 +118,21 @@ describe('Server Operation Count Tests', function () {
118118 const server = Array . from ( client . topology . s . servers . values ( ) ) [ 0 ] ;
119119 expect ( server . s . operationCount ) . to . equal ( 0 ) ;
120120 const commandSpy = sinon . spy ( server , 'command' ) ;
121+ const incrementSpy = sinon . spy ( server , 'incrementOperationCount' ) ;
122+ const decrementSpy = sinon . spy ( server , 'decrementOperationCount' ) ;
121123
122124 const operationPromises = Array . from ( { length : 10 } , ( ) =>
123125 collection . insertOne ( { count : 1 } )
124126 ) ;
125127
126- // operation count is incremented after connection checkout, which happens asynchronously (even though there are plenty of connections in the pool).
127- // we sleep to give the event loop a turn so that all the commands check out a connection before asserting the operation count
128- await sleep ( 1 ) ;
129-
130- expect ( server . s . operationCount ) . to . equal ( 10 ) ;
131-
132- await Promise . all ( operationPromises ) ;
128+ await Promise . allSettled ( operationPromises ) ;
133129
134130 expect ( commandSpy . called ) . to . be . true ;
131+ // This test is flaky when sleeping and asserting the operation count after the sleep but before the
132+ // promise execution, so we assert instead that the count was incremented 10 times and decremented 10
133+ // times - the total number of operations.
134+ expect ( incrementSpy . callCount ) . to . equal ( 10 ) ;
135+ expect ( decrementSpy . callCount ) . to . equal ( 10 ) ;
135136 expect ( server . s . operationCount ) . to . equal ( 0 ) ;
136137 } ) ;
137138 } ) ;
0 commit comments