@@ -69,6 +69,14 @@ function checkHttpRouteAttribute(metricsData, expectedRoutes) {
6969 return foundRoutes ;
7070}
7171
72+ function createMetricsTimeout ( ms ) {
73+ return new Promise ( ( _ , reject ) => {
74+ setTimeout ( ( ) => {
75+ reject ( new Error ( `Timed out waiting for metrics after ${ ms } ms` ) ) ;
76+ } , ms ) ;
77+ } ) ;
78+ }
79+
7280async function runTest ( ) {
7381 // Start gRPC server as child process
7482 const grpcServer = new GRPCServer ( ) ;
@@ -109,6 +117,7 @@ async function runTest() {
109117 // Track which routes we've seen
110118 const expectedRoutes = new Set ( [ '/users/:id' , '/posts/:postId' , '/health' ] ) ;
111119 const foundRoutes = new Set ( ) ;
120+ const metricsTimeoutMs = 10_000 ;
112121
113122 // Listen for metrics
114123 const metricsPromise = new Promise ( ( resolve , reject ) => {
@@ -127,6 +136,10 @@ async function runTest() {
127136 }
128137 } ) ;
129138 } ) ;
139+ const waitForMetrics = Promise . race ( [
140+ metricsPromise ,
141+ createMetricsTimeout ( metricsTimeoutMs ) ,
142+ ] ) ;
130143
131144 // Make HTTP requests to trigger routes
132145 console . log ( 'Making HTTP requests...' ) ;
@@ -146,13 +159,20 @@ async function runTest() {
146159 assert . strictEqual ( response . status , 200 ) ;
147160 console . log ( 'Requested /health' ) ;
148161
149- // Wait for all metrics to be reported
150- await metricsPromise ;
151-
152- // Cleanup
153- console . log ( 'Cleaning up...' ) ;
154- await fastify . close ( ) ;
155- grpcServer . close ( ) ;
162+ try {
163+ // Wait for all metrics to be reported
164+ await waitForMetrics ;
165+ } finally {
166+ console . log ( 'Cleaning up...' ) ;
167+ await fastify . close ( ) . catch ( ( err ) => {
168+ console . error ( 'Failed to close Fastify server' , err ) ;
169+ } ) ;
170+ try {
171+ grpcServer . close ( ) ;
172+ } catch ( err ) {
173+ console . error ( 'Failed to close gRPC server' , err ) ;
174+ }
175+ }
156176}
157177
158178await runTest ( ) ;
0 commit comments