11import { expect } from 'chai' ;
2- import { EventEmitter } from 'events' ;
2+ import { EventEmitter , on } from 'events' ;
33import { Socket } from 'net' ;
44import * as sinon from 'sinon' ;
5+ import { Readable } from 'stream' ;
56import { setTimeout } from 'timers' ;
67
78import { BinMsg } from '../../../src/cmap/commands' ;
@@ -165,11 +166,44 @@ describe('new Connection()', function () {
165166
166167 beforeEach ( function ( ) {
167168 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+ } ) ;
173207 } ) ;
174208
175209 context ( 'when requestId/responseTo do not match' , function ( ) {
@@ -178,6 +212,13 @@ describe('new Connection()', function () {
178212
179213 beforeEach ( function ( ) {
180214 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+
181222 // Create the operation description.
182223 const operationDescription : OperationDescription = {
183224 requestId : 1 ,
@@ -201,7 +242,7 @@ describe('new Connection()', function () {
201242 } ) ;
202243
203244 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 ) ;
205246 } ) ;
206247 } ) ;
207248
@@ -211,6 +252,13 @@ describe('new Connection()', function () {
211252
212253 beforeEach ( function ( ) {
213254 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+
214262 // Create the operation description.
215263 const operationDescription : OperationDescription = {
216264 requestId : 1 ,
@@ -234,7 +282,7 @@ describe('new Connection()', function () {
234282 } ) ;
235283
236284 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 ) ;
238286 } ) ;
239287 } ) ;
240288 } ) ;
0 commit comments