@@ -7,6 +7,7 @@ import { type Collection, type Document, type MongoClient, MongoNetworkError } f
77import { Connection } from '../../../src/cmap/connection' ;
88import { ns } from '../../../src/utils' ;
99import { clearFailPoint , configureFailPoint } from '../../tools/utils' ;
10+ import { filterForCommands } from '../shared' ;
1011
1112describe ( 'Socket Errors' , ( ) => {
1213 describe ( 'when a socket emits an error' , ( ) => {
@@ -81,7 +82,6 @@ describe('Socket Errors', () => {
8182 describe ( 'when an error is thrown writing data to a socket' , ( ) => {
8283 let client : MongoClient ;
8384 let collection : Collection < Document > ;
84- let errorCount = 0 ;
8585
8686 beforeEach ( async function ( ) {
8787 client = this . configuration . newClient ( { monitorCommands : true } ) ;
@@ -96,7 +96,6 @@ describe('Socket Errors', () => {
9696 //@ts -expect-error: private property
9797 const socket = connection . socket ;
9898 const stub = sinon . stub ( socket , 'write' ) . callsFake ( function ( ) {
99- errorCount ++ ;
10099 stub . restore ( ) ;
101100 throw new Error ( 'This socket has been ended by the other party' ) ;
102101 } ) ;
@@ -110,26 +109,18 @@ describe('Socket Errors', () => {
110109 } ) ;
111110
112111 it ( 'retries and succeeds' , async ( ) => {
113- const initialErrorCount = errorCount ;
114112 const commandSucceededEvents : string [ ] = [ ] ;
115113 const commandFailedEvents : string [ ] = [ ] ;
116114 const commandStartedEvents : string [ ] = [ ] ;
117115
118- client . on ( 'commandStarted' , event => {
119- if ( event . commandName === 'find' ) commandStartedEvents . push ( event . commandName ) ;
120- } ) ;
121- client . on ( 'commandSucceeded' , event => {
122- if ( event . commandName === 'find' ) commandSucceededEvents . push ( event . commandName ) ;
123- } ) ;
124- client . on ( 'commandFailed' , event => {
125- if ( event . commandName === 'find' ) commandFailedEvents . push ( event . commandName ) ;
126- } ) ;
116+ client . on ( 'commandStarted' , filterForCommands ( 'find' , commandStartedEvents ) ) ;
117+ client . on ( 'commandSucceeded' , filterForCommands ( 'find' , commandSucceededEvents ) ) ;
118+ client . on ( 'commandFailed' , filterForCommands ( 'find' , commandFailedEvents ) ) ;
127119
128120 // call find, fail once, succeed on retry
129121 const item = await collection . findOne ( { } ) ;
130- // check that an object was returned
122+ // check that we didn't find anything, as expected
131123 expect ( item ) . to . be . null ;
132- expect ( errorCount ) . to . be . equal ( initialErrorCount + 1 ) ;
133124 // check that we have the expected command monitoring events
134125 expect ( commandStartedEvents ) . to . have . length ( 2 ) ;
135126 expect ( commandFailedEvents ) . to . have . length ( 1 ) ;
0 commit comments