@@ -159,6 +159,67 @@ describe('new Connection()', function () {
159159 } ) ;
160160
161161 describe ( '#onMessage' , function ( ) {
162+ context ( 'when the connection is not a monitoring connection' , function ( ) {
163+ let queue : Map < number , OperationDescription > ;
164+ let driverSocket : FakeSocket ;
165+ let connection : Connection ;
166+
167+ beforeEach ( function ( ) {
168+ driverSocket = sinon . spy ( new FakeSocket ( ) ) ;
169+ } ) ;
170+
171+ context ( 'when more than one operation description is in the queue' , function ( ) {
172+ let spyOne ;
173+ let spyTwo ;
174+ const document = { ok : 1 } ;
175+
176+ beforeEach ( function ( ) {
177+ spyOne = sinon . spy ( ) ;
178+ spyTwo = sinon . spy ( ) ;
179+
180+ // @ts -expect-error: driverSocket does not fully satisfy the stream type, but that's okay
181+ connection = new Connection ( driverSocket , connectionOptionsDefaults ) ;
182+ const queueSymbol = getSymbolFrom ( connection , 'queue' ) ;
183+ queue = connection [ queueSymbol ] ;
184+
185+ // Create the operation descriptions.
186+ const descriptionOne : OperationDescription = {
187+ requestId : 1 ,
188+ cb : spyOne
189+ } ;
190+ const descriptionTwo : OperationDescription = {
191+ requestId : 2 ,
192+ cb : spyTwo
193+ } ;
194+
195+ // Stick an operation description in the queue.
196+ queue . set ( 2 , descriptionOne ) ;
197+ queue . set ( 3 , descriptionTwo ) ;
198+ // Emit a message that matches the existing operation description.
199+ const msg = generateOpMsgBuffer ( document ) ;
200+ const msgHeader : MessageHeader = {
201+ length : msg . readInt32LE ( 0 ) ,
202+ requestId : 2 ,
203+ responseTo : 1 ,
204+ opCode : msg . readInt32LE ( 12 )
205+ } ;
206+ const msgBody = msg . subarray ( 16 ) ;
207+
208+ const message = new BinMsg ( msg , msgHeader , msgBody ) ;
209+ connection . onMessage ( message ) ;
210+ } ) ;
211+
212+ it ( 'calls all operation description callbacks with an error' , function ( ) {
213+ expect ( spyOne ) . to . be . calledOnce ;
214+ expect ( spyTwo ) . to . be . calledOnce ;
215+ const errorOne = spyOne . firstCall . args [ 0 ] ;
216+ const errorTwo = spyTwo . firstCall . args [ 0 ] ;
217+ expect ( errorOne ) . to . be . instanceof ( MongoRuntimeError ) ;
218+ expect ( errorTwo ) . to . be . instanceof ( MongoRuntimeError ) ;
219+ } ) ;
220+ } ) ;
221+ } ) ;
222+
162223 context ( 'when the connection is a monitoring connection' , function ( ) {
163224 let queue : Map < number , OperationDescription > ;
164225 let driverSocket : FakeSocket ;
0 commit comments