@@ -372,16 +372,31 @@ describe('Change Streams', function () {
372
372
373
373
describe ( 'cache the change stream resume token' , ( ) => {
374
374
describe ( 'using iterator form' , ( ) => {
375
- it ( '#next' , {
376
- metadata : { requires : { topology : 'replicaset' } } ,
375
+ context ( '#next' , ( ) => {
376
+ it ( 'caches the resume token on change' , {
377
+ metadata : { requires : { topology : 'replicaset' } } ,
377
378
378
- async test ( ) {
379
- await initIteratorMode ( changeStream ) ;
380
- await collection . insertOne ( { a : 1 } ) ;
379
+ async test ( ) {
380
+ await initIteratorMode ( changeStream ) ;
381
+ await collection . insertOne ( { a : 1 } ) ;
381
382
382
- const change = await changeStream . next ( ) ;
383
- expect ( change ) . to . have . property ( '_id' ) . that . deep . equals ( changeStream . resumeToken ) ;
384
- }
383
+ const change = await changeStream . next ( ) ;
384
+ expect ( change ) . to . have . property ( '_id' ) . that . deep . equals ( changeStream . resumeToken ) ;
385
+ }
386
+ } ) ;
387
+
388
+ it ( 'caches the resume token correctly when preceded by #hasNext' , {
389
+ metadata : { requires : { topology : 'replicaset' } } ,
390
+ async test ( ) {
391
+ await initIteratorMode ( changeStream ) ;
392
+ await collection . insertOne ( { a : 1 } ) ;
393
+
394
+ await changeStream . hasNext ( ) ;
395
+
396
+ const change = await changeStream . next ( ) ;
397
+ expect ( change ) . to . have . property ( '_id' ) . that . deep . equals ( changeStream . resumeToken ) ;
398
+ }
399
+ } ) ;
385
400
} ) ;
386
401
387
402
it ( '#tryNext' , {
@@ -396,7 +411,7 @@ describe('Change Streams', function () {
396
411
}
397
412
} ) ;
398
413
399
- describe ( '#hasNext' , ( ) => {
414
+ context ( '#hasNext' , ( ) => {
400
415
it ( 'does not cache the resume token' , {
401
416
metadata : { requires : { topology : 'replicaset' } } ,
402
417
async test ( ) {
@@ -408,14 +423,7 @@ describe('Change Streams', function () {
408
423
const hasNext = await changeStream . hasNext ( ) ;
409
424
expect ( hasNext ) . to . be . true ;
410
425
411
- // Calling .hasNext() does not allow the ChangeStream to update the token,
412
- // even when changes are present.
413
426
expect ( changeStream . resumeToken ) . to . equal ( resumeToken ) ;
414
-
415
- // Consuming the change causes the ChangeStream to cache the resume token.
416
- const change = await changeStream . next ( ) ;
417
- expect ( change ) . to . have . property ( '_id' ) . that . deep . equals ( changeStream . resumeToken ) ;
418
- expect ( resumeToken ) . to . not . equal ( changeStream . resumeToken ) ;
419
427
}
420
428
} ) ;
421
429
} ) ;
0 commit comments