@@ -78,18 +78,13 @@ describe('Socket Errors', () => {
7878 } ) ;
7979 } ) ;
8080
81- describe ( 'when encountering connection error' , ( ) => {
81+ describe ( 'when an error is thrown writing data to a socket ' , ( ) => {
8282 let client : MongoClient ;
8383 let collection : Collection < Document > ;
84-
85- const metadata : MongoDBMetadataUI = { requires : { mongodb : '>=4.4' } } ;
84+ let errorCount = 0 ;
8685
8786 beforeEach ( async function ( ) {
88- if ( ! this . configuration . filters . NodeVersionFilter . filter ( { metadata } ) ) {
89- return ;
90- }
91-
92- client = this . configuration . newClient ( { } ) ;
87+ client = this . configuration . newClient ( { monitorCommands : true } ) ;
9388 await client . connect ( ) ;
9489 const db = client . db ( 'closeConn' ) ;
9590 collection = db . collection ( 'closeConn' ) ;
@@ -102,7 +97,9 @@ describe('Socket Errors', () => {
10297 for ( const connection of server . pool . connections ) {
10398 //@ts -expect-error: private property
10499 const socket = connection . socket ;
105- sinon . stub ( socket , 'write' ) . callsFake ( function ( ) {
100+ const stub = sinon . stub ( socket , 'write' ) . callsFake ( function ( ) {
101+ errorCount ++ ;
102+ stub . restore ( ) ;
106103 throw new Error ( 'This socket has been ended by the other party' ) ;
107104 } ) ;
108105 }
@@ -114,10 +111,31 @@ describe('Socket Errors', () => {
114111 await client . close ( ) ;
115112 } ) ;
116113
117- it ( 'throws a MongoNetworkError and retries' , metadata , async ( ) => {
114+ it ( 'retries and succeeds' , async ( ) => {
115+ const initialErrorCount = errorCount ;
116+ const commandSucceededEvents : string [ ] = [ ] ;
117+ const commandFailedEvents : string [ ] = [ ] ;
118+ const commandStartedEvents : string [ ] = [ ] ;
119+
120+ client . on ( 'commandStarted' , event => {
121+ if ( event . commandName === 'find' ) commandStartedEvents . push ( event . commandName ) ;
122+ } ) ;
123+ client . on ( 'commandSucceeded' , event => {
124+ if ( event . commandName === 'find' ) commandSucceededEvents . push ( event . commandName ) ;
125+ } ) ;
126+ client . on ( 'commandFailed' , event => {
127+ if ( event . commandName === 'find' ) commandFailedEvents . push ( event . commandName ) ;
128+ } ) ;
129+
130+ // call find, fail once, succeed on retry
118131 const item = await collection . findOne ( { } ) ;
132+ // check that an object was returned
119133 expect ( item ) . to . exist ;
120- console . log ( item ) ;
134+ expect ( errorCount ) . to . be . equal ( initialErrorCount + 1 ) ;
135+ // check that we have the expected command monitoring events
136+ expect ( commandStartedEvents ) . to . have . length ( 2 ) ;
137+ expect ( commandFailedEvents ) . to . have . length ( 1 ) ;
138+ expect ( commandSucceededEvents ) . to . have . length ( 1 ) ;
121139 } ) ;
122140 } ) ;
123141} ) ;
0 commit comments