gnodet-bot commented on code in PR #30505:
URL: https://github.com/apache/camel/pull/30505#discussion_r4664160615


##########
components/camel-http/src/test/java/org/apache/camel/component/http/HttpOAuth2SameHostPoolShutdownTest.java:
##########
@@ -0,0 +1,155 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.http;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.camel.Exchange;
+import org.apache.camel.component.http.handler.HeaderValidationHandler;
+import org.apache.camel.component.http.handler.OAuth2TokenRequestHandler;
+import org.apache.hc.client5.http.HttpRoute;
+import org.apache.hc.client5.http.impl.classic.HttpClientBuilder;
+import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManager;
+import org.apache.hc.core5.http.HttpHost;
+import org.apache.hc.core5.http.HttpStatus;
+import org.apache.hc.core5.http.impl.bootstrap.HttpServer;
+import org.apache.hc.core5.http.impl.bootstrap.ServerBootstrap;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNull;
+
+/**
+ * Regression test for CAMEL-27233: when the OAuth2 token endpoint is on the 
same host and port as the API endpoint, the
+ * token client's independent connection manager must NOT affect the API 
client's connection pool.
+ *
+ * <p>
+ * Before the fix, {@link OAuth2ClientConfigurer#configureHttpClient} called 
{@code clientBuilder.build()}, which built
+ * the token client inheriting the endpoint's {@code 
HttpClientConnectionManager}. Because {@code connManagerShared}
+ * defaults to {@code false}, both clients considered themselves the 
connection-manager owner. When the token client was
+ * closed (e.g. on a stop/restart cycle or when {@code doStop()} ran), it shut 
down the shared pool, leaving the API
+ * client's pool in a shut-down state and causing subsequent requests to throw
+ * {@code IllegalStateException: Connection pool shut down}.
+ * </p>
+ */
+class HttpOAuth2SameHostPoolShutdownTest extends BaseHttpTest {
+
+    private static final String FAKE_TOKEN = "same-host-test-token";
+    private static final String CLIENT_ID = "test-client";
+    private static final String CLIENT_SECRET = "test-secret";
+
+    private HttpServer localServer;
+
+    @Override
+    public void setupResources() throws Exception {
+        Map<String, String> expectedHeaders = new HashMap<>();
+        expectedHeaders.put("Authorization", "Bearer " + FAKE_TOKEN);
+
+        // A single server handles both /token and /items on the same host and 
port,
+        // reproducing the case described in CAMEL-27233.
+        localServer = ServerBootstrap.bootstrap()
+                .setCanonicalHostName("localhost")
+                .setHttpProcessor(getBasicHttpProcessor())
+                .setConnectionReuseStrategy(getConnectionReuseStrategy())
+                .setResponseFactory(getHttpResponseFactory())
+                .setSslContext(getSSLContext())
+                .register("/token", new OAuth2TokenRequestHandler(FAKE_TOKEN, 
CLIENT_ID, CLIENT_SECRET))
+                .register("/items",
+                        new HeaderValidationHandler("GET", null, null, null, 
expectedHeaders))
+                .create();
+
+        localServer.start();
+    }

Review Comment:
   `setupResources()` starts `localServer` but there is no matching 
`cleanupResources()` override to stop it after each test. Without cleanup, the 
server leaks between tests and may cause port conflicts in subsequent runs.
   
   Add this override after `setupResources()`:
   
   ```suggestion
       @Override
       public void cleanupResources() throws Exception {
           closeImmediately(localServer).close();
       }
   
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to