@@ -12,6 +12,7 @@ const { ReadPreference } = require('../../src/read_preference');
12
12
const { ServerType } = require ( '../../src/sdam/common' ) ;
13
13
const { formatSort } = require ( '../../src/sort' ) ;
14
14
const { FindCursor } = require ( '../../src/cursor/find_cursor' ) ;
15
+ const kSession = Symbol ( 'session' ) ;
15
16
16
17
describe ( 'Cursor' , function ( ) {
17
18
before ( function ( ) {
@@ -3755,6 +3756,65 @@ describe('Cursor', function () {
3755
3756
}
3756
3757
) ;
3757
3758
3759
+ describe ( '#clone' , function ( ) {
3760
+ let client ;
3761
+ let db ;
3762
+ let collection ;
3763
+
3764
+ beforeEach ( function ( ) {
3765
+ client = this . configuration . newClient ( { w : 1 } ) ;
3766
+
3767
+ return client . connect ( ) . then ( client => {
3768
+ db = client . db ( this . configuration . db ) ;
3769
+ collection = db . collection ( 'test_coll' ) ;
3770
+ } ) ;
3771
+ } ) ;
3772
+
3773
+ afterEach ( function ( ) {
3774
+ return client . close ( ) ;
3775
+ } ) ;
3776
+
3777
+ context ( 'when executing on a find cursor' , function ( ) {
3778
+ it ( 'removes the existing session from the cloned cursor' , function ( ) {
3779
+ const docs = [ { name : 'test1' } , { name : 'test2' } ] ;
3780
+ return collection . insertMany ( docs ) . then ( ( ) => {
3781
+ const cursor = collection . find ( { } , { batchSize : 1 } ) ;
3782
+ return cursor
3783
+ . next ( )
3784
+ . then ( doc => {
3785
+ expect ( doc ) . to . exist ;
3786
+ const clonedCursor = cursor . clone ( ) ;
3787
+ expect ( clonedCursor . cursorOptions . session ) . to . not . exist ;
3788
+ expect ( clonedCursor [ kSession ] ) . to . not . exist ;
3789
+ } )
3790
+ . finally ( ( ) => {
3791
+ return cursor . close ( ) ;
3792
+ } ) ;
3793
+ } ) ;
3794
+ } ) ;
3795
+ } ) ;
3796
+
3797
+ context ( 'when executing on an aggregation cursor' , function ( ) {
3798
+ it ( 'removes the existing session from the cloned cursor' , function ( ) {
3799
+ const docs = [ { name : 'test1' } , { name : 'test2' } ] ;
3800
+ return collection . insertMany ( docs ) . then ( ( ) => {
3801
+ const cursor = collection . aggregate ( [ { $match : { } } ] , { batchSize : 1 } ) ;
3802
+ return cursor
3803
+ . next ( )
3804
+ . then ( doc => {
3805
+ expect ( doc ) . to . exist ;
3806
+ const clonedCursor = cursor . clone ( ) ;
3807
+ expect ( clonedCursor . cursorOptions . session ) . to . not . exist ;
3808
+ expect ( clonedCursor [ kSession ] ) . to . not . exist ;
3809
+ } )
3810
+ . finally ( ( ) => {
3811
+ return cursor . close ( ) ;
3812
+ } ) ;
3813
+ } ) ;
3814
+ } ) ;
3815
+ } ) ;
3816
+ } ) ;
3817
+
3758
3818
it ( 'should return a promise when no callback supplied to forEach method' , function ( ) {
3759
3819
const configuration = this . configuration ;
3760
3820
const client = configuration . newClient ( { w : 1 } , { maxPoolSize : 1 } ) ;
0 commit comments