@@ -2,7 +2,7 @@ import { expect } from 'chai';
2
2
import * as sinon from 'sinon' ;
3
3
4
4
import { AbstractCursor , Collection , ConnectionPool , MongoClient } from '../../mongodb' ;
5
- import { FailPoint , sleep } from '../../tools/utils' ;
5
+ import { FailPoint } from '../../tools/utils' ;
6
6
7
7
const testMetadata : MongoDBMetadataUI = {
8
8
requires : {
@@ -118,20 +118,21 @@ describe('Server Operation Count Tests', function () {
118
118
const server = Array . from ( client . topology . s . servers . values ( ) ) [ 0 ] ;
119
119
expect ( server . s . operationCount ) . to . equal ( 0 ) ;
120
120
const commandSpy = sinon . spy ( server , 'command' ) ;
121
+ const incrementSpy = sinon . spy ( server , 'incrementOperationCount' ) ;
122
+ const decrementSpy = sinon . spy ( server , 'decrementOperationCount' ) ;
121
123
122
124
const operationPromises = Array . from ( { length : 10 } , ( ) =>
123
125
collection . insertOne ( { count : 1 } )
124
126
) ;
125
127
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 ) ;
133
129
134
130
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 ) ;
135
136
expect ( server . s . operationCount ) . to . equal ( 0 ) ;
136
137
} ) ;
137
138
} ) ;
0 commit comments