1313
1414import io .vertx .core .Context ;
1515import io .vertx .core .Future ;
16+ import io .vertx .core .ThreadingModel ;
1617import io .vertx .core .VertxOptions ;
1718import io .vertx .core .buffer .Buffer ;
1819import io .vertx .core .http .impl .HttpClientImpl ;
1920import io .vertx .core .http .impl .HttpServerRequestInternal ;
21+ import io .vertx .core .impl .VertxInternal ;
2022import io .vertx .core .metrics .MetricsOptions ;
2123import io .vertx .core .net .NetClient ;
2224import io .vertx .core .net .SocketAddress ;
4648public abstract class HttpMetricsTestBase extends HttpTestBase {
4749
4850 private final HttpVersion protocol ;
51+ private final ThreadingModel threadingModel ;
4952
50- public HttpMetricsTestBase (HttpVersion protocol ) {
53+ public HttpMetricsTestBase (HttpVersion protocol , ThreadingModel threadingModel ) {
5154 this .protocol = protocol ;
55+ this .threadingModel = threadingModel ;
56+ }
57+
58+ @ Override
59+ protected void startServer (SocketAddress bindAddress , Context context , HttpServer server ) throws Exception {
60+ if (threadingModel == ThreadingModel .WORKER ) {
61+ context = ((VertxInternal ) vertx ).createWorkerContext ();
62+ }
63+ super .startServer (bindAddress , context , server );
5264 }
5365
5466 @ Override
@@ -83,6 +95,8 @@ public void testHttpMetricsLifecycle() throws Exception {
8395 assertTrue (serverMetric .get ().socket .connected .get ());
8496 assertNull (serverMetric .get ().route .get ());
8597 req .routed ("/route/:param" );
98+ // Worker can wait
99+ assertWaitUntil (() -> serverMetric .get ().route .get () != null );
86100 assertEquals ("/route/:param" , serverMetric .get ().route .get ());
87101 req .bodyHandler (buff -> {
88102 assertEquals (contentLength , buff .length ());
@@ -93,14 +107,21 @@ public void testHttpMetricsLifecycle() throws Exception {
93107 vertx .setPeriodic (1 , timerID -> {
94108 Buffer chunk = TestUtils .randomBuffer (chunkSize );
95109 if (numBuffer .decrementAndGet () == 0 ) {
96- resp .end (chunk );
97- assertTrue (serverMetric .get ().responseEnded .get ());
98- assertEquals (contentLength , serverMetric .get ().bytesWritten .get ());
99- assertNull (serverMetrics .getRequestMetric (req ));
110+ resp
111+ .end (chunk )
112+ .onComplete (onSuccess (v -> {
113+ assertTrue (serverMetric .get ().responseEnded .get ());
114+ assertFalse (serverMetric .get ().failed .get ());
115+ assertEquals (contentLength , serverMetric .get ().bytesWritten .get ());
116+ assertNull (serverMetrics .getRequestMetric (req ));
117+ }));
100118 vertx .cancelTimer (timerID );
101119 } else {
102- resp .write (chunk );
103- assertSame (serverMetric .get ().response .get (), resp );
120+ resp
121+ .write (chunk )
122+ .onComplete (onSuccess (v -> {
123+ assertSame (serverMetric .get ().response .get (), resp );
124+ }));
104125 }
105126 });
106127 });
@@ -205,9 +226,7 @@ public void testHttpClientLifecycle() throws Exception {
205226 });
206227 });
207228 });
208- CountDownLatch listenLatch = new CountDownLatch (1 );
209- server .listen (HttpTestBase .DEFAULT_HTTP_PORT , "localhost" , onSuccess (s -> { listenLatch .countDown (); }));
210- awaitLatch (listenLatch );
229+ startServer (testAddress );
211230 FakeHttpClientMetrics clientMetrics = FakeMetricsBase .getMetrics (client );
212231 CountDownLatch responseBeginLatch = new CountDownLatch (1 );
213232 CountDownLatch responseEndLatch = new CountDownLatch (1 );
@@ -301,8 +320,13 @@ public void testRouteMetrics() throws Exception {
301320 HttpServerMetric metric = metrics .getRequestMetric (req );
302321 assertNull (metric .route .get ());
303322 req .routed ("MyRoute" );
323+ // Worker can wait
324+ assertWaitUntil (() -> metric .route .get () != null );
304325 assertEquals ("MyRoute" , metric .route .get ());
326+ metric .route .set (null );
305327 req .routed ("MyRoute - rerouted" );
328+ // Worker can wait
329+ assertWaitUntil (() -> metric .route .get () != null );
306330 assertEquals ("MyRoute - rerouted" , metric .route .get ());
307331 req .response ().end ();
308332 testComplete ();
0 commit comments