3
3
import { expect } from 'chai' ;
4
4
import * as sinon from 'sinon' ;
5
5
6
+ import { type CommandStartedEvent } from '../../../mongodb' ;
6
7
import {
8
+ type CommandSucceededEvent ,
7
9
MongoClient ,
8
10
MongoOperationTimeoutError ,
9
11
MongoServerSelectionError ,
10
12
now
11
13
} from '../../mongodb' ;
14
+ import { type FailPoint } from '../../tools/utils' ;
12
15
13
16
// TODO(NODE-5824): Implement CSOT prose tests
14
17
describe ( 'CSOT spec prose tests' , function ( ) {
@@ -220,6 +223,42 @@ describe('CSOT spec prose tests', function () {
220
223
* blocking method for cursor iteration that executes `getMore` commands in a loop until a document is available or an
221
224
* error occurs.
222
225
*/
226
+ const failpoint : FailPoint = {
227
+ configureFailPoint : 'failCommand' ,
228
+ mode : 'alwaysOn' ,
229
+ data : {
230
+ failCommands : [ 'getMore' ] ,
231
+ blockConnection : true ,
232
+ blockTimeMS : 15
233
+ }
234
+ } ;
235
+ let internalClient : MongoClient ;
236
+ let client : MongoClient ;
237
+ let commandStarted : CommandStartedEvent [ ] ;
238
+ let commandSucceeded : CommandSucceededEvent [ ] ;
239
+
240
+ beforeEach ( async function ( ) {
241
+ internalClient = this . configuration . newClient ( ) ;
242
+ await internalClient . db ( 'db' ) . dropCollection ( 'coll' ) ;
243
+ await internalClient . db ( 'db' ) . collection ( 'coll' ) . insertOne ( { x : 1 } ) ;
244
+ await internalClient . db ( ) . admin ( ) . command ( failpoint ) ;
245
+
246
+ client = this . configuration . newClient ( undefined , { timeoutMS : 20 } ) ;
247
+ commandStarted = [ ] ;
248
+ commandSucceeded = [ ] ;
249
+
250
+ client . on ( 'commandStarted' , ev => commandStarted . push ( ev ) ) ;
251
+ client . on ( 'commandSucceeded' , ev => commandSucceeded . push ( ev ) ) ;
252
+ } ) ;
253
+
254
+ afterEach ( async function ( ) {
255
+ await internalClient
256
+ . db ( )
257
+ . admin ( )
258
+ . command ( { ...failpoint , mode : 'off' } ) ;
259
+ await internalClient . close ( ) ;
260
+ await client . close ( ) ;
261
+ } ) ;
223
262
224
263
context ( 'Tailable cursors' , ( ) => {
225
264
/**
@@ -246,6 +285,27 @@ describe('CSOT spec prose tests', function () {
246
285
* - Expect this to fail with a timeout error.
247
286
* 1. Verify that a `find` command and two `getMore` commands were executed against the `db.coll` collection during the test.
248
287
*/
288
+
289
+ it ( 'send correct number of finds and getMores' , async function ( ) {
290
+ const cursor = client . db ( 'db' ) . collection ( 'coll' ) . find ( { } , { tailable : true } ) ;
291
+ const doc = await cursor . next ( ) ;
292
+ // FIXME: Account for object id
293
+ expect ( doc ) . to . deep . equal ( { x : 1 } ) ;
294
+ // Check that there are no getMores sent
295
+ expect ( commandStarted . filter ( e => Object . hasOwn ( e . command , 'getMore' ) ) ) . to . have . lengthOf ( 0 ) ;
296
+
297
+ const maybeError = await cursor . next ( ) . then (
298
+ ( ) => null ,
299
+ e => e
300
+ ) ;
301
+
302
+ expect ( maybeError ) . to . be . instanceof ( MongoOperationTimeoutError ) ;
303
+ expect (
304
+ commandStarted . filter (
305
+ e => Object . hasOwn ( e . command , 'find' ) || Object . hasOwn ( e . command , 'getMore' )
306
+ )
307
+ ) . to . have . lengthOf ( 3 ) ;
308
+ } ) ;
249
309
} ) ;
250
310
251
311
context ( 'Change Streams' , ( ) => {
@@ -270,6 +330,20 @@ describe('CSOT spec prose tests', function () {
270
330
* - Expect this to fail with a timeout error.
271
331
* 1. Verify that an `aggregate` command and two `getMore` commands were executed against the `db.coll` collection during the test.
272
332
*/
333
+ it ( 'sends correct number of aggregate and getMores' , async function ( ) {
334
+ const changeStream = client . db ( 'db' ) . collection ( 'coll' ) . watch ( ) ;
335
+ const maybeError = await changeStream . next ( ) . then (
336
+ ( ) => null ,
337
+ e => e
338
+ ) ;
339
+
340
+ expect ( maybeError ) . to . be . instanceof ( MongoOperationTimeoutError ) ;
341
+ expect (
342
+ commandStarted . filter (
343
+ e => Object . hasOwn ( e . command , 'aggregate' ) || Object . hasOwn ( e . command , 'getMore' )
344
+ )
345
+ ) . to . have . lengthOf ( 3 ) ;
346
+ } ) ;
273
347
} ) ;
274
348
} ) ;
275
349
0 commit comments