@@ -161,37 +161,47 @@ describe("LambdaDogStatsD", () => {
161161 } ) ;
162162
163163 it ( "logs callback errors and completes the send" , async ( ) => {
164+ jest . useFakeTimers ( ) ;
164165 const error = new Error ( "callback failure" ) ;
165- mockSend . mockImplementation ( ( _message , _port , _host , callback ) => callback ( error ) ) ;
166+ const callbacks = useControlledSocket ( ) ;
166167 const client = new LambdaDogStatsD ( ) ;
167168
168169 client . distribution ( "metric" , 1 ) ;
169170
170- await expect ( client . flush ( ) ) . resolves . toBeUndefined ( ) ;
171+ const flush = client . flush ( ) ;
172+ callbacks [ 0 ] ( error ) ;
173+ expect ( jest . getTimerCount ( ) ) . toBe ( 0 ) ;
174+ await expect ( flush ) . resolves . toBeUndefined ( ) ;
171175 expect ( logDebug ) . toHaveBeenCalledWith ( "Unable to send metric packet: callback failure" ) ;
172176 } ) ;
173177
174178 it ( "logs synchronous socket errors without throwing" , async ( ) => {
179+ jest . useFakeTimers ( ) ;
175180 mockSend . mockImplementation ( ( ) => {
176181 throw new Error ( "synchronous failure" ) ;
177182 } ) ;
178183 const client = new LambdaDogStatsD ( ) ;
179184
180185 client . distribution ( "metric" , 1 ) ;
181186
182- await expect ( client . flush ( ) ) . resolves . toBeUndefined ( ) ;
187+ const flush = client . flush ( ) ;
188+ expect ( jest . getTimerCount ( ) ) . toBe ( 0 ) ;
189+ await expect ( flush ) . resolves . toBeUndefined ( ) ;
183190 expect ( logDebug ) . toHaveBeenCalledWith ( "Unable to send metric packet: synchronous failure" ) ;
184191 } ) ;
185192
186193 it ( "normalizes non-Error values thrown synchronously by the socket" , async ( ) => {
194+ jest . useFakeTimers ( ) ;
187195 mockSend . mockImplementation ( ( ) => {
188196 throw "non-error failure" ;
189197 } ) ;
190198 const client = new LambdaDogStatsD ( ) ;
191199
192200 client . distribution ( "metric" , 1 ) ;
193201
194- await expect ( client . flush ( ) ) . resolves . toBeUndefined ( ) ;
202+ const flush = client . flush ( ) ;
203+ expect ( jest . getTimerCount ( ) ) . toBe ( 0 ) ;
204+ await expect ( flush ) . resolves . toBeUndefined ( ) ;
195205 expect ( logDebug ) . toHaveBeenCalledWith ( "Unable to send metric packet: Unknown socket send failure" ) ;
196206 } ) ;
197207
0 commit comments