1
1
import { expect } from 'chai' ;
2
- import { EventEmitter } from 'events' ;
2
+ import { EventEmitter , on } from 'events' ;
3
3
import { Socket } from 'net' ;
4
4
import * as sinon from 'sinon' ;
5
+ import { Readable } from 'stream' ;
5
6
import { setTimeout } from 'timers' ;
6
7
7
8
import { BinMsg } from '../../../src/cmap/commands' ;
@@ -165,11 +166,44 @@ describe('new Connection()', function () {
165
166
166
167
beforeEach ( function ( ) {
167
168
driverSocket = sinon . spy ( new FakeSocket ( ) ) ;
168
- // @ts -expect-error: driverSocket does not fully satisfy the stream type, but that's okay
169
- connection = sinon . spy ( new Connection ( driverSocket , connectionOptionsDefaults ) ) ;
170
- connection . isMonitoringConnection = true ;
171
- const queueSymbol = getSymbolFrom ( connection , 'queue' ) ;
172
- queue = connection [ queueSymbol ] ;
169
+ } ) ;
170
+
171
+ context ( 'when multiple hellos exist on the stream' , function ( ) {
172
+ let callbackSpy ;
173
+ const inputStream = new Readable ( ) ;
174
+ const document = { ok : 1 } ;
175
+
176
+ beforeEach ( function ( ) {
177
+ callbackSpy = sinon . spy ( ) ;
178
+ const firstHello = generateOpMsgBuffer ( document ) ;
179
+ const secondHello = generateOpMsgBuffer ( document ) ;
180
+ const thirdHello = generateOpMsgBuffer ( document ) ;
181
+ const buffer = Buffer . concat ( [ firstHello , secondHello , thirdHello ] ) ;
182
+
183
+ // @ts -expect-error: driverSocket does not fully satisfy the stream type, but that's okay
184
+ connection = sinon . spy ( new Connection ( inputStream , connectionOptionsDefaults ) ) ;
185
+ connection . isMonitoringConnection = true ;
186
+ const queueSymbol = getSymbolFrom ( connection , 'queue' ) ;
187
+ queue = connection [ queueSymbol ] ;
188
+
189
+ // Create the operation description.
190
+ const operationDescription : OperationDescription = {
191
+ requestId : 1 ,
192
+ cb : callbackSpy
193
+ } ;
194
+
195
+ // Stick an operation description in the queue.
196
+ queue . set ( 1 , operationDescription ) ;
197
+
198
+ // Push the buffer of 3 hellos to the input stream
199
+ inputStream . push ( buffer ) ;
200
+ inputStream . push ( null ) ;
201
+ } ) ;
202
+
203
+ it ( 'calls the operation description callback with the document' , async function ( ) {
204
+ await on ( inputStream , 'message' ) ;
205
+ expect ( callbackSpy ) . to . be . calledOnceWith ( undefined , document ) ;
206
+ } ) ;
173
207
} ) ;
174
208
175
209
context ( 'when requestId/responseTo do not match' , function ( ) {
@@ -178,6 +212,13 @@ describe('new Connection()', function () {
178
212
179
213
beforeEach ( function ( ) {
180
214
callbackSpy = sinon . spy ( ) ;
215
+
216
+ // @ts -expect-error: driverSocket does not fully satisfy the stream type, but that's okay
217
+ connection = sinon . spy ( new Connection ( driverSocket , connectionOptionsDefaults ) ) ;
218
+ connection . isMonitoringConnection = true ;
219
+ const queueSymbol = getSymbolFrom ( connection , 'queue' ) ;
220
+ queue = connection [ queueSymbol ] ;
221
+
181
222
// Create the operation description.
182
223
const operationDescription : OperationDescription = {
183
224
requestId : 1 ,
@@ -201,7 +242,7 @@ describe('new Connection()', function () {
201
242
} ) ;
202
243
203
244
it ( 'calls the operation description callback with the document' , function ( ) {
204
- expect ( callbackSpy ) . to . be . calledExactlyOnceWith ( undefined , document ) ;
245
+ expect ( callbackSpy ) . to . be . calledOnceWith ( undefined , document ) ;
205
246
} ) ;
206
247
} ) ;
207
248
@@ -211,6 +252,13 @@ describe('new Connection()', function () {
211
252
212
253
beforeEach ( function ( ) {
213
254
callbackSpy = sinon . spy ( ) ;
255
+
256
+ // @ts -expect-error: driverSocket does not fully satisfy the stream type, but that's okay
257
+ connection = sinon . spy ( new Connection ( driverSocket , connectionOptionsDefaults ) ) ;
258
+ connection . isMonitoringConnection = true ;
259
+ const queueSymbol = getSymbolFrom ( connection , 'queue' ) ;
260
+ queue = connection [ queueSymbol ] ;
261
+
214
262
// Create the operation description.
215
263
const operationDescription : OperationDescription = {
216
264
requestId : 1 ,
@@ -234,7 +282,7 @@ describe('new Connection()', function () {
234
282
} ) ;
235
283
236
284
it ( 'calls the operation description callback with the document' , function ( ) {
237
- expect ( callbackSpy ) . to . be . calledExactlyOnceWith ( undefined , document ) ;
285
+ expect ( callbackSpy ) . to . be . calledOnceWith ( undefined , document ) ;
238
286
} ) ;
239
287
} ) ;
240
288
} ) ;
0 commit comments