1
1
import { expect } from 'chai' ;
2
- import * as sinon from 'sinon' ;
3
2
import { setTimeout } from 'timers' ;
4
3
import { promisify } from 'util' ;
5
4
6
5
import {
7
- BinMsg ,
8
6
CancellationToken ,
9
7
ClientMetadata ,
10
8
connect ,
@@ -15,12 +13,10 @@ import {
15
13
LEGACY_HELLO_COMMAND ,
16
14
MongoCredentials ,
17
15
MongoNetworkError ,
18
- ns ,
19
16
prepareHandshakeDocument as prepareHandshakeDocumentCb
20
17
} from '../../mongodb' ;
21
18
import { genClusterTime } from '../../tools/common' ;
22
19
import * as mock from '../../tools/mongodb-mock/index' ;
23
- import { generateOpMsgBuffer } from '../../tools/utils' ;
24
20
25
21
const CONNECT_DEFAULTS = {
26
22
id : 1 ,
@@ -158,93 +154,6 @@ describe('Connect Tests', function () {
158
154
} ) ;
159
155
} ) ;
160
156
161
- context ( 'when sending commands on a connection' , ( ) => {
162
- let server ;
163
- let connectOptions ;
164
- let connection : Connection ;
165
- let streamSetTimeoutSpy ;
166
-
167
- beforeEach ( async ( ) => {
168
- server = await mock . createServer ( ) ;
169
- server . setMessageHandler ( request => {
170
- if ( isHello ( request . document ) ) {
171
- request . reply ( mock . HELLO ) ;
172
- }
173
- } ) ;
174
- connectOptions = {
175
- ...CONNECT_DEFAULTS ,
176
- hostAddress : server . hostAddress ( ) as HostAddress ,
177
- socketTimeoutMS : 15000
178
- } ;
179
-
180
- connection = await promisify < Connection > ( callback =>
181
- //@ts -expect-error: Callbacks do not have mutual exclusion for error/result existence
182
- connect ( connectOptions , callback )
183
- ) ( ) ;
184
-
185
- streamSetTimeoutSpy = sinon . spy ( connection . stream , 'setTimeout' ) ;
186
- } ) ;
187
-
188
- afterEach ( async ( ) => {
189
- connection . destroy ( { force : true } ) ;
190
- sinon . restore ( ) ;
191
- await mock . cleanup ( ) ;
192
- } ) ;
193
-
194
- it ( 'sets timeout specified on class before writing to the socket' , async ( ) => {
195
- await promisify ( callback =>
196
- connection . command ( ns ( 'admin.$cmd' ) , { hello : 1 } , { } , callback )
197
- ) ( ) ;
198
- expect ( streamSetTimeoutSpy ) . to . have . been . calledWith ( 15000 ) ;
199
- } ) ;
200
-
201
- it ( 'sets timeout specified on options before writing to the socket' , async ( ) => {
202
- await promisify ( callback =>
203
- connection . command ( ns ( 'admin.$cmd' ) , { hello : 1 } , { socketTimeoutMS : 2000 } , callback )
204
- ) ( ) ;
205
- expect ( streamSetTimeoutSpy ) . to . have . been . calledWith ( 2000 ) ;
206
- } ) ;
207
-
208
- it ( 'clears timeout after getting a message if moreToCome=false' , async ( ) => {
209
- connection . stream . setTimeout ( 1 ) ;
210
- const msg = generateOpMsgBuffer ( { hello : 1 } ) ;
211
- const msgHeader = {
212
- length : msg . readInt32LE ( 0 ) ,
213
- requestId : 1 ,
214
- responseTo : 0 ,
215
- opCode : msg . readInt32LE ( 12 )
216
- } ;
217
- const msgBody = msg . subarray ( 16 ) ;
218
- try {
219
- connection . onMessage ( new BinMsg ( msg , msgHeader , msgBody ) ) ;
220
- } catch {
221
- // regardless of outcome
222
- }
223
- // timeout is still reset
224
- expect ( connection . stream ) . to . have . property ( 'timeout' , 0 ) ;
225
- } ) ;
226
-
227
- it ( 'does not clear timeout after getting a message if moreToCome=true' , async ( ) => {
228
- connection . stream . setTimeout ( 1 ) ;
229
- const msg = generateOpMsgBuffer ( { hello : 1 } ) ;
230
- const msgHeader = {
231
- length : msg . readInt32LE ( 0 ) ,
232
- requestId : 1 ,
233
- responseTo : 0 ,
234
- opCode : msg . readInt32LE ( 12 )
235
- } ;
236
- const msgBody = msg . subarray ( 16 ) ;
237
- msgBody . writeInt32LE ( 2 ) ; // OPTS_MORE_TO_COME
238
- try {
239
- connection . onMessage ( new BinMsg ( msg , msgHeader , msgBody ) ) ;
240
- } catch {
241
- // regardless of outcome
242
- }
243
- // timeout is still set
244
- expect ( connection . stream ) . to . have . property ( 'timeout' , 1 ) ;
245
- } ) ;
246
- } ) ;
247
-
248
157
it ( 'should emit `MongoNetworkError` for network errors' , function ( done ) {
249
158
connect ( { hostAddress : new HostAddress ( 'non-existent:27018' ) } , err => {
250
159
expect ( err ) . to . be . instanceOf ( MongoNetworkError ) ;
0 commit comments