@@ -4,8 +4,13 @@ const Connection = require('../../../lib/cmap/connection').Connection;
4
4
const connect = require ( '../../../lib/core/connection/connect' ) ;
5
5
const expect = require ( 'chai' ) . expect ;
6
6
const BSON = require ( 'bson' ) ;
7
+ const setupDatabase = require ( '../../functional/shared' ) . setupDatabase ;
7
8
8
9
describe ( 'Connection' , function ( ) {
10
+ before ( function ( ) {
11
+ return setupDatabase ( this . configuration ) ;
12
+ } ) ;
13
+
9
14
it ( 'should execute a command against a server' , function ( done ) {
10
15
const connectOptions = Object . assign (
11
16
{ connectionType : Connection , bson : new BSON ( ) } ,
@@ -70,4 +75,56 @@ describe('Connection', function() {
70
75
done ( ) ;
71
76
} ) ;
72
77
} ) ;
78
+
79
+ it ( 'should support calling back multiple times on exhaust commands' , {
80
+ metadata : { requires : { mongodb : '>=4.2.0' } } ,
81
+ test : function ( done ) {
82
+ const ns = `${ this . configuration . db } .$cmd` ;
83
+ const connectOptions = Object . assign (
84
+ { connectionType : Connection , bson : new BSON ( ) } ,
85
+ this . configuration . options
86
+ ) ;
87
+
88
+ connect ( connectOptions , ( err , conn ) => {
89
+ expect ( err ) . to . not . exist ;
90
+ this . defer ( _done => conn . destroy ( _done ) ) ;
91
+
92
+ const documents = Array . from ( Array ( 10000 ) , ( _ , idx ) => ( {
93
+ test : Math . floor ( Math . random ( ) * idx )
94
+ } ) ) ;
95
+
96
+ conn . command ( ns , { insert : 'test' , documents } , ( err , res ) => {
97
+ expect ( err ) . to . not . exist ;
98
+ expect ( res )
99
+ . nested . property ( 'result.n' )
100
+ . to . equal ( documents . length ) ;
101
+
102
+ let totalDocumentsRead = 0 ;
103
+ conn . command ( ns , { find : 'test' , batchSize : 100 } , ( err , result ) => {
104
+ expect ( err ) . to . not . exist ;
105
+ expect ( result ) . nested . property ( 'result.cursor' ) . to . exist ;
106
+ const cursor = result . result . cursor ;
107
+ totalDocumentsRead += cursor . firstBatch . length ;
108
+
109
+ conn . command (
110
+ ns ,
111
+ { getMore : cursor . id , collection : 'test' , batchSize : 100 } ,
112
+ { exhaustAllowed : true } ,
113
+ ( err , result ) => {
114
+ expect ( err ) . to . not . exist ;
115
+ expect ( result ) . nested . property ( 'result.cursor' ) . to . exist ;
116
+ const cursor = result . result . cursor ;
117
+ totalDocumentsRead += cursor . nextBatch . length ;
118
+
119
+ if ( cursor . id === 0 || cursor . id . isZero ( ) ) {
120
+ expect ( totalDocumentsRead ) . to . equal ( documents . length ) ;
121
+ done ( ) ;
122
+ }
123
+ }
124
+ ) ;
125
+ } ) ;
126
+ } ) ;
127
+ } ) ;
128
+ }
129
+ } ) ;
73
130
} ) ;
0 commit comments