gnodet-bot commented on code in PR #26734: URL: https://github.com/apache/camel/pull/26734#discussion_r4080226305
########## core/camel-core/src/test/java/org/apache/camel/util/HttpResourceTimeoutTest.java: ########## @@ -0,0 +1,197 @@ +/* + * 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.util; + +import java.io.IOException; +import java.io.OutputStream; +import java.net.ServerSocket; +import java.net.Socket; +import java.net.SocketTimeoutException; +import java.nio.charset.StandardCharsets; +import java.util.Properties; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; + +import org.apache.camel.CamelContext; +import org.apache.camel.impl.DefaultCamelContext; +import org.apache.camel.impl.engine.DefaultResourceResolvers; +import org.apache.camel.spi.Resource; +import org.apache.camel.support.ResourceHelper; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.Timeout; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; + +/** + * An {@code http:} resource must not be able to stall the caller forever. + * <p/> + * The server here accepts the connection and then never answers, which is the only shape that reproduces the problem: a + * refused connection fails fast on its own, so nothing is proven by pointing at a closed port. + * <p/> + * The timeouts below are deliberately {@code SEPARATE_THREAD}: the default mode only measures elapsed time once the + * test method returns, so a regression that restores the indefinite wait would hang the build instead of failing it. + */ +public class HttpResourceTimeoutTest { + + private ServerSocket server; + private Thread acceptor; + private CountDownLatch accepted; + private volatile boolean stopped; + + @BeforeEach + void startMuteServer() throws Exception { + server = new ServerSocket(0); + accepted = new CountDownLatch(1); + acceptor = new Thread(() -> { + while (!stopped) { + try (Socket socket = server.accept()) { + accepted.countDown(); + // hold the connection open and write nothing at all, so the client is left waiting on a read; + // draining the request blocks until the client gives up and closes + socket.getInputStream().transferTo(OutputStream.nullOutputStream()); + } catch (IOException e) { + return; + } + } + }, "mute-http-server"); + acceptor.setDaemon(true); + acceptor.start(); + stopped = false; Review Comment: ⚠️ **Race condition — `stopped` reset happens after `acceptor.start()`.** After the *first* test, `@AfterEach` sets `stopped = true`. The next `@BeforeEach` starts a fresh thread before resetting `stopped = false`, so the thread reads `!stopped` → `!true` → exits immediately without ever calling `server.accept()`. Any test that relies on `accepted.await(…)` (i.e. `getInputStreamGivesUpOnAServerThatNeverAnswers`) then races against a dead acceptor and can fail intermittently when JUnit runs tests in a sequence. Fix: reset `stopped` before starting the thread. ```suggestion stopped = false; acceptor.start(); ``` ########## core/camel-core/src/test/java/org/apache/camel/util/HttpResourceTimeoutTest.java: ########## @@ -0,0 +1,197 @@ +/* + * 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.util; + +import java.io.IOException; +import java.io.OutputStream; +import java.net.ServerSocket; +import java.net.Socket; +import java.net.SocketTimeoutException; +import java.nio.charset.StandardCharsets; +import java.util.Properties; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; + +import org.apache.camel.CamelContext; +import org.apache.camel.impl.DefaultCamelContext; +import org.apache.camel.impl.engine.DefaultResourceResolvers; +import org.apache.camel.spi.Resource; +import org.apache.camel.support.ResourceHelper; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.Timeout; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; + +/** + * An {@code http:} resource must not be able to stall the caller forever. + * <p/> + * The server here accepts the connection and then never answers, which is the only shape that reproduces the problem: a + * refused connection fails fast on its own, so nothing is proven by pointing at a closed port. + * <p/> + * The timeouts below are deliberately {@code SEPARATE_THREAD}: the default mode only measures elapsed time once the + * test method returns, so a regression that restores the indefinite wait would hang the build instead of failing it. + */ +public class HttpResourceTimeoutTest { Review Comment: ❌ **CLAUDE.md test-visibility rule — `public` is not permitted on new test classes.** The prior review raised this and the author replied that the rule is not in CLAUDE.md. It is. From `CLAUDE.md` in the repo root: > **Test Visibility: Drop `public` From Test Classes and Methods** > New test classes and test methods MUST NOT use the `public` modifier. The author's argument (2725 public test classes already exist) is about *pre-existing* code, not new code. The rule explicitly distinguishes: existing classes are grandfathered, new ones are not. The suggested changes on the four `@Test` methods below stand for the same reason. ```suggestion class HttpResourceTimeoutTest { ``` ########## core/camel-core/src/test/java/org/apache/camel/util/HttpResourceTimeoutTest.java: ########## @@ -0,0 +1,197 @@ +/* + * 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.util; + +import java.io.IOException; +import java.io.OutputStream; +import java.net.ServerSocket; +import java.net.Socket; +import java.net.SocketTimeoutException; +import java.nio.charset.StandardCharsets; +import java.util.Properties; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; + +import org.apache.camel.CamelContext; +import org.apache.camel.impl.DefaultCamelContext; +import org.apache.camel.impl.engine.DefaultResourceResolvers; +import org.apache.camel.spi.Resource; +import org.apache.camel.support.ResourceHelper; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.Timeout; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; + +/** + * An {@code http:} resource must not be able to stall the caller forever. + * <p/> + * The server here accepts the connection and then never answers, which is the only shape that reproduces the problem: a + * refused connection fails fast on its own, so nothing is proven by pointing at a closed port. + * <p/> + * The timeouts below are deliberately {@code SEPARATE_THREAD}: the default mode only measures elapsed time once the + * test method returns, so a regression that restores the indefinite wait would hang the build instead of failing it. + */ +public class HttpResourceTimeoutTest { + + private ServerSocket server; + private Thread acceptor; + private CountDownLatch accepted; + private volatile boolean stopped; + + @BeforeEach + void startMuteServer() throws Exception { + server = new ServerSocket(0); + accepted = new CountDownLatch(1); + acceptor = new Thread(() -> { + while (!stopped) { + try (Socket socket = server.accept()) { + accepted.countDown(); + // hold the connection open and write nothing at all, so the client is left waiting on a read; + // draining the request blocks until the client gives up and closes + socket.getInputStream().transferTo(OutputStream.nullOutputStream()); + } catch (IOException e) { + return; + } + } + }, "mute-http-server"); + acceptor.setDaemon(true); + acceptor.start(); + stopped = false; + } + + @AfterEach + void stopMuteServer() throws Exception { + stopped = true; + server.close(); + acceptor.join(TimeUnit.SECONDS.toMillis(5)); + } + + private CamelContext contextWithReadTimeout(String millis) { + CamelContext context = new DefaultCamelContext(); + Properties properties = new Properties(); + properties.setProperty(DefaultResourceResolvers.HTTP_READ_TIMEOUT_PROPERTY, millis); + context.getPropertiesComponent().setInitialProperties(properties); + context.start(); + return context; + } + + private String muteUrl() { + return "http://localhost:" + server.getLocalPort() + "/policy.rego"; + } + + @Test + @Timeout(value = 30, threadMode = Timeout.ThreadMode.SEPARATE_THREAD) + public void getInputStreamGivesUpOnAServerThatNeverAnswers() throws Exception { Review Comment: ```suggestion void getInputStreamGivesUpOnAServerThatNeverAnswers() throws Exception { ``` ########## core/camel-core/src/test/java/org/apache/camel/util/HttpResourceTimeoutTest.java: ########## @@ -0,0 +1,197 @@ +/* + * 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.util; + +import java.io.IOException; +import java.io.OutputStream; +import java.net.ServerSocket; +import java.net.Socket; +import java.net.SocketTimeoutException; +import java.nio.charset.StandardCharsets; +import java.util.Properties; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; + +import org.apache.camel.CamelContext; +import org.apache.camel.impl.DefaultCamelContext; +import org.apache.camel.impl.engine.DefaultResourceResolvers; +import org.apache.camel.spi.Resource; +import org.apache.camel.support.ResourceHelper; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.Timeout; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; + +/** + * An {@code http:} resource must not be able to stall the caller forever. + * <p/> + * The server here accepts the connection and then never answers, which is the only shape that reproduces the problem: a + * refused connection fails fast on its own, so nothing is proven by pointing at a closed port. + * <p/> + * The timeouts below are deliberately {@code SEPARATE_THREAD}: the default mode only measures elapsed time once the + * test method returns, so a regression that restores the indefinite wait would hang the build instead of failing it. + */ +public class HttpResourceTimeoutTest { + + private ServerSocket server; + private Thread acceptor; + private CountDownLatch accepted; + private volatile boolean stopped; + + @BeforeEach + void startMuteServer() throws Exception { + server = new ServerSocket(0); + accepted = new CountDownLatch(1); + acceptor = new Thread(() -> { + while (!stopped) { + try (Socket socket = server.accept()) { + accepted.countDown(); + // hold the connection open and write nothing at all, so the client is left waiting on a read; + // draining the request blocks until the client gives up and closes + socket.getInputStream().transferTo(OutputStream.nullOutputStream()); + } catch (IOException e) { + return; + } + } + }, "mute-http-server"); + acceptor.setDaemon(true); + acceptor.start(); + stopped = false; + } + + @AfterEach + void stopMuteServer() throws Exception { + stopped = true; + server.close(); + acceptor.join(TimeUnit.SECONDS.toMillis(5)); + } + + private CamelContext contextWithReadTimeout(String millis) { + CamelContext context = new DefaultCamelContext(); + Properties properties = new Properties(); + properties.setProperty(DefaultResourceResolvers.HTTP_READ_TIMEOUT_PROPERTY, millis); + context.getPropertiesComponent().setInitialProperties(properties); + context.start(); + return context; + } + + private String muteUrl() { + return "http://localhost:" + server.getLocalPort() + "/policy.rego"; + } + + @Test + @Timeout(value = 30, threadMode = Timeout.ThreadMode.SEPARATE_THREAD) + public void getInputStreamGivesUpOnAServerThatNeverAnswers() throws Exception { + CamelContext context = contextWithReadTimeout("500"); + try { + Resource resource = ResourceHelper.resolveResource(context, muteUrl()); + + assertThrows(SocketTimeoutException.class, resource::getInputStream); + assertTrue(accepted.await(5, TimeUnit.SECONDS), + "the server should have accepted the connection - a refused connect proves nothing"); + } finally { + context.stop(); + } + } + + @Test + @Timeout(value = 30, threadMode = Timeout.ThreadMode.SEPARATE_THREAD) + public void existsGivesUpOnAServerThatNeverAnswers() throws Exception { Review Comment: ```suggestion void existsGivesUpOnAServerThatNeverAnswers() throws Exception { ``` ########## core/camel-core/src/test/java/org/apache/camel/util/HttpResourceTimeoutTest.java: ########## @@ -0,0 +1,197 @@ +/* + * 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.util; + +import java.io.IOException; +import java.io.OutputStream; +import java.net.ServerSocket; +import java.net.Socket; +import java.net.SocketTimeoutException; +import java.nio.charset.StandardCharsets; +import java.util.Properties; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; + +import org.apache.camel.CamelContext; +import org.apache.camel.impl.DefaultCamelContext; +import org.apache.camel.impl.engine.DefaultResourceResolvers; +import org.apache.camel.spi.Resource; +import org.apache.camel.support.ResourceHelper; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.Timeout; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; + +/** + * An {@code http:} resource must not be able to stall the caller forever. + * <p/> + * The server here accepts the connection and then never answers, which is the only shape that reproduces the problem: a + * refused connection fails fast on its own, so nothing is proven by pointing at a closed port. + * <p/> + * The timeouts below are deliberately {@code SEPARATE_THREAD}: the default mode only measures elapsed time once the + * test method returns, so a regression that restores the indefinite wait would hang the build instead of failing it. + */ +public class HttpResourceTimeoutTest { + + private ServerSocket server; + private Thread acceptor; + private CountDownLatch accepted; + private volatile boolean stopped; + + @BeforeEach + void startMuteServer() throws Exception { + server = new ServerSocket(0); + accepted = new CountDownLatch(1); + acceptor = new Thread(() -> { + while (!stopped) { + try (Socket socket = server.accept()) { + accepted.countDown(); + // hold the connection open and write nothing at all, so the client is left waiting on a read; + // draining the request blocks until the client gives up and closes + socket.getInputStream().transferTo(OutputStream.nullOutputStream()); + } catch (IOException e) { + return; + } + } + }, "mute-http-server"); + acceptor.setDaemon(true); + acceptor.start(); + stopped = false; + } + + @AfterEach + void stopMuteServer() throws Exception { + stopped = true; + server.close(); + acceptor.join(TimeUnit.SECONDS.toMillis(5)); + } + + private CamelContext contextWithReadTimeout(String millis) { + CamelContext context = new DefaultCamelContext(); + Properties properties = new Properties(); + properties.setProperty(DefaultResourceResolvers.HTTP_READ_TIMEOUT_PROPERTY, millis); + context.getPropertiesComponent().setInitialProperties(properties); + context.start(); + return context; + } + + private String muteUrl() { + return "http://localhost:" + server.getLocalPort() + "/policy.rego"; + } + + @Test + @Timeout(value = 30, threadMode = Timeout.ThreadMode.SEPARATE_THREAD) + public void getInputStreamGivesUpOnAServerThatNeverAnswers() throws Exception { + CamelContext context = contextWithReadTimeout("500"); + try { + Resource resource = ResourceHelper.resolveResource(context, muteUrl()); + + assertThrows(SocketTimeoutException.class, resource::getInputStream); + assertTrue(accepted.await(5, TimeUnit.SECONDS), + "the server should have accepted the connection - a refused connect proves nothing"); + } finally { + context.stop(); + } + } + + @Test + @Timeout(value = 30, threadMode = Timeout.ThreadMode.SEPARATE_THREAD) + public void existsGivesUpOnAServerThatNeverAnswers() throws Exception { + CamelContext context = contextWithReadTimeout("500"); + try { + Resource resource = ResourceHelper.resolveResource(context, muteUrl()); + + // exists() wraps the IOException rather than declaring it, so the timeout surfaces as the cause + IllegalArgumentException e = assertThrows(IllegalArgumentException.class, resource::exists); + assertEquals(SocketTimeoutException.class, e.getCause().getClass()); + } finally { + context.stop(); + } + } + + @Test + @Timeout(value = 30, threadMode = Timeout.ThreadMode.SEPARATE_THREAD) + public void httpsResourcesAreBoundedToo() throws Exception { + CamelContext context = contextWithReadTimeout("500"); + try { + // the https resolver hands back the same HttpResource, so it must inherit the same bound; the handshake + // against a mute plain-text server is what fails here, and it must fail rather than hang + Resource resource + = ResourceHelper.resolveResource(context, "https://localhost:" + server.getLocalPort() + "/x"); + + assertThrows(IOException.class, resource::getInputStream); + } finally { + context.stop(); + } + } + + @Test + @Timeout(value = 30, threadMode = Timeout.ThreadMode.SEPARATE_THREAD) + public void aTimeoutThatIsNotANumberSaysWhichPropertyIsWrong() throws Exception { Review Comment: ```suggestion void aTimeoutThatIsNotANumberSaysWhichPropertyIsWrong() throws Exception { ``` ########## core/camel-core/src/test/java/org/apache/camel/util/HttpResourceTimeoutTest.java: ########## @@ -0,0 +1,197 @@ +/* + * 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.util; + +import java.io.IOException; +import java.io.OutputStream; +import java.net.ServerSocket; +import java.net.Socket; +import java.net.SocketTimeoutException; +import java.nio.charset.StandardCharsets; +import java.util.Properties; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; + +import org.apache.camel.CamelContext; +import org.apache.camel.impl.DefaultCamelContext; +import org.apache.camel.impl.engine.DefaultResourceResolvers; +import org.apache.camel.spi.Resource; +import org.apache.camel.support.ResourceHelper; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.Timeout; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; + +/** + * An {@code http:} resource must not be able to stall the caller forever. + * <p/> + * The server here accepts the connection and then never answers, which is the only shape that reproduces the problem: a + * refused connection fails fast on its own, so nothing is proven by pointing at a closed port. + * <p/> + * The timeouts below are deliberately {@code SEPARATE_THREAD}: the default mode only measures elapsed time once the + * test method returns, so a regression that restores the indefinite wait would hang the build instead of failing it. + */ +public class HttpResourceTimeoutTest { + + private ServerSocket server; + private Thread acceptor; + private CountDownLatch accepted; + private volatile boolean stopped; + + @BeforeEach + void startMuteServer() throws Exception { + server = new ServerSocket(0); + accepted = new CountDownLatch(1); + acceptor = new Thread(() -> { + while (!stopped) { + try (Socket socket = server.accept()) { + accepted.countDown(); + // hold the connection open and write nothing at all, so the client is left waiting on a read; + // draining the request blocks until the client gives up and closes + socket.getInputStream().transferTo(OutputStream.nullOutputStream()); + } catch (IOException e) { + return; + } + } + }, "mute-http-server"); + acceptor.setDaemon(true); + acceptor.start(); + stopped = false; + } + + @AfterEach + void stopMuteServer() throws Exception { + stopped = true; + server.close(); + acceptor.join(TimeUnit.SECONDS.toMillis(5)); + } + + private CamelContext contextWithReadTimeout(String millis) { + CamelContext context = new DefaultCamelContext(); + Properties properties = new Properties(); + properties.setProperty(DefaultResourceResolvers.HTTP_READ_TIMEOUT_PROPERTY, millis); + context.getPropertiesComponent().setInitialProperties(properties); + context.start(); + return context; + } + + private String muteUrl() { + return "http://localhost:" + server.getLocalPort() + "/policy.rego"; + } + + @Test + @Timeout(value = 30, threadMode = Timeout.ThreadMode.SEPARATE_THREAD) + public void getInputStreamGivesUpOnAServerThatNeverAnswers() throws Exception { + CamelContext context = contextWithReadTimeout("500"); + try { + Resource resource = ResourceHelper.resolveResource(context, muteUrl()); + + assertThrows(SocketTimeoutException.class, resource::getInputStream); + assertTrue(accepted.await(5, TimeUnit.SECONDS), + "the server should have accepted the connection - a refused connect proves nothing"); + } finally { + context.stop(); + } + } + + @Test + @Timeout(value = 30, threadMode = Timeout.ThreadMode.SEPARATE_THREAD) + public void existsGivesUpOnAServerThatNeverAnswers() throws Exception { + CamelContext context = contextWithReadTimeout("500"); + try { + Resource resource = ResourceHelper.resolveResource(context, muteUrl()); + + // exists() wraps the IOException rather than declaring it, so the timeout surfaces as the cause + IllegalArgumentException e = assertThrows(IllegalArgumentException.class, resource::exists); + assertEquals(SocketTimeoutException.class, e.getCause().getClass()); + } finally { + context.stop(); + } + } + + @Test + @Timeout(value = 30, threadMode = Timeout.ThreadMode.SEPARATE_THREAD) + public void httpsResourcesAreBoundedToo() throws Exception { Review Comment: ```suggestion void httpsResourcesAreBoundedToo() throws Exception { ``` ########## core/camel-core/src/test/java/org/apache/camel/util/HttpResourceTimeoutTest.java: ########## @@ -0,0 +1,197 @@ +/* + * 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.util; + +import java.io.IOException; +import java.io.OutputStream; +import java.net.ServerSocket; +import java.net.Socket; +import java.net.SocketTimeoutException; +import java.nio.charset.StandardCharsets; +import java.util.Properties; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; + +import org.apache.camel.CamelContext; +import org.apache.camel.impl.DefaultCamelContext; +import org.apache.camel.impl.engine.DefaultResourceResolvers; +import org.apache.camel.spi.Resource; +import org.apache.camel.support.ResourceHelper; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.Timeout; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; + +/** + * An {@code http:} resource must not be able to stall the caller forever. + * <p/> + * The server here accepts the connection and then never answers, which is the only shape that reproduces the problem: a + * refused connection fails fast on its own, so nothing is proven by pointing at a closed port. + * <p/> + * The timeouts below are deliberately {@code SEPARATE_THREAD}: the default mode only measures elapsed time once the + * test method returns, so a regression that restores the indefinite wait would hang the build instead of failing it. + */ +public class HttpResourceTimeoutTest { + + private ServerSocket server; + private Thread acceptor; + private CountDownLatch accepted; + private volatile boolean stopped; + + @BeforeEach + void startMuteServer() throws Exception { + server = new ServerSocket(0); + accepted = new CountDownLatch(1); + acceptor = new Thread(() -> { + while (!stopped) { + try (Socket socket = server.accept()) { + accepted.countDown(); + // hold the connection open and write nothing at all, so the client is left waiting on a read; + // draining the request blocks until the client gives up and closes + socket.getInputStream().transferTo(OutputStream.nullOutputStream()); + } catch (IOException e) { + return; + } + } + }, "mute-http-server"); + acceptor.setDaemon(true); + acceptor.start(); + stopped = false; + } + + @AfterEach + void stopMuteServer() throws Exception { + stopped = true; + server.close(); + acceptor.join(TimeUnit.SECONDS.toMillis(5)); + } + + private CamelContext contextWithReadTimeout(String millis) { + CamelContext context = new DefaultCamelContext(); + Properties properties = new Properties(); + properties.setProperty(DefaultResourceResolvers.HTTP_READ_TIMEOUT_PROPERTY, millis); + context.getPropertiesComponent().setInitialProperties(properties); + context.start(); + return context; + } + + private String muteUrl() { + return "http://localhost:" + server.getLocalPort() + "/policy.rego"; + } + + @Test + @Timeout(value = 30, threadMode = Timeout.ThreadMode.SEPARATE_THREAD) + public void getInputStreamGivesUpOnAServerThatNeverAnswers() throws Exception { + CamelContext context = contextWithReadTimeout("500"); + try { + Resource resource = ResourceHelper.resolveResource(context, muteUrl()); + + assertThrows(SocketTimeoutException.class, resource::getInputStream); + assertTrue(accepted.await(5, TimeUnit.SECONDS), + "the server should have accepted the connection - a refused connect proves nothing"); + } finally { + context.stop(); + } + } + + @Test + @Timeout(value = 30, threadMode = Timeout.ThreadMode.SEPARATE_THREAD) + public void existsGivesUpOnAServerThatNeverAnswers() throws Exception { + CamelContext context = contextWithReadTimeout("500"); + try { + Resource resource = ResourceHelper.resolveResource(context, muteUrl()); + + // exists() wraps the IOException rather than declaring it, so the timeout surfaces as the cause + IllegalArgumentException e = assertThrows(IllegalArgumentException.class, resource::exists); + assertEquals(SocketTimeoutException.class, e.getCause().getClass()); + } finally { + context.stop(); + } + } + + @Test + @Timeout(value = 30, threadMode = Timeout.ThreadMode.SEPARATE_THREAD) + public void httpsResourcesAreBoundedToo() throws Exception { + CamelContext context = contextWithReadTimeout("500"); + try { + // the https resolver hands back the same HttpResource, so it must inherit the same bound; the handshake + // against a mute plain-text server is what fails here, and it must fail rather than hang + Resource resource + = ResourceHelper.resolveResource(context, "https://localhost:" + server.getLocalPort() + "/x"); + + assertThrows(IOException.class, resource::getInputStream); + } finally { + context.stop(); + } + } + + @Test + @Timeout(value = 30, threadMode = Timeout.ThreadMode.SEPARATE_THREAD) + public void aTimeoutThatIsNotANumberSaysWhichPropertyIsWrong() throws Exception { + CamelContext context = new DefaultCamelContext(); + Properties properties = new Properties(); + properties.setProperty(DefaultResourceResolvers.HTTP_CONNECT_TIMEOUT_PROPERTY, "10s"); + context.getPropertiesComponent().setInitialProperties(properties); + context.start(); + try { + IllegalArgumentException e + = assertThrows(IllegalArgumentException.class, () -> ResourceHelper.resolveResource(context, muteUrl())); + + // the failure happens during startup, so the message has to carry the key; "For input string" alone + // leaves an operator grepping their whole configuration + assertTrue(e.getMessage().contains(DefaultResourceResolvers.HTTP_CONNECT_TIMEOUT_PROPERTY), + "message should name the property, was: " + e.getMessage()); + assertTrue(e.getMessage().contains("10s"), + "message should quote the bad value, was: " + e.getMessage()); + } finally { + context.stop(); + } + } + + @Test + @Timeout(value = 30, threadMode = Timeout.ThreadMode.SEPARATE_THREAD) + public void aResourceThatIsNotThereStillReportsAbsent() throws Exception { Review Comment: ```suggestion void aResourceThatIsNotThereStillReportsAbsent() throws Exception { ``` -- 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]
