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


##########
components/camel-ssh/src/test/java/org/apache/camel/component/ssh/SshConfigurationErrorsTest.java:
##########
@@ -0,0 +1,239 @@
+/*
+ * 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.ssh;
+
+import java.io.IOException;
+import java.nio.file.Paths;
+import java.time.Duration;
+import java.util.List;
+
+import org.apache.camel.CamelExecutionException;
+import org.apache.camel.ResolveEndpointFailedException;
+import org.apache.commons.lang3.reflect.FieldUtils;
+import org.apache.sshd.client.SshClient;
+import org.apache.sshd.common.NamedResource;
+import org.apache.sshd.common.channel.Channel;
+import org.apache.sshd.common.channel.ChannelFactory;
+import org.apache.sshd.common.kex.AbstractDH;
+import org.apache.sshd.common.kex.BuiltinDHFactories;
+import org.apache.sshd.common.kex.DHFactory;
+import org.apache.sshd.common.kex.KeyExchangeFactory;
+import org.apache.sshd.common.keyprovider.FileKeyPairProvider;
+import org.apache.sshd.common.session.Session;
+import org.apache.sshd.core.CoreModuleProperties;
+import org.apache.sshd.server.SshServer;
+import org.apache.sshd.server.channel.ChannelSessionFactory;
+import org.apache.sshd.server.forward.DirectTcpipFactory;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertInstanceOf;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+/**
+ * A misconfigured endpoint fails with a message that names the problem, 
instead of a NullPointerException, and the
+ * options that had no effect apply.
+ */
+public class SshConfigurationErrorsTest extends SshComponentTestSupport {
+
+    @Test
+    public void testRefusedChannelSaysSo() throws Exception {
+        // a server that accepts the session but refuses the session channel
+        SshServer refusing = SshServer.setUpDefaultServer();
+        refusing.setPort(0);
+        refusing.setKeyPairProvider(new 
FileKeyPairProvider(Paths.get(getHostKey())));
+        refusing.setPasswordAuthenticator((username, password, session) -> 
true);
+        // only forwarding channels: the session channel that runs the command 
is refused
+        refusing.setChannelFactories(List.of(DirectTcpipFactory.INSTANCE));
+        refusing.start();
+        try {
+            String uri = "ssh://smx:smx@localhost:" + refusing.getPort() + 
"?timeout=30000";
+            CamelExecutionException e
+                    = assertThrows(CamelExecutionException.class, () -> 
template.requestBody(uri, "test"));
+
+            Throwable cause = causeStartingWith(e, "The server refused to open 
the exec channel to localhost:");
+            assertTrue(cause.getCause() != null, "the reason from the server 
is the cause");
+        } finally {
+            refusing.stop(true);
+        }
+    }
+
+    @Test
+    public void testChannelThatDoesNotOpenInTimeFailsWithChannelOpenTimeout() 
throws Exception {
+        // a server that takes 3 seconds to open the session channel
+        SshServer slow = SshServer.setUpDefaultServer();
+        slow.setPort(0);
+        slow.setKeyPairProvider(new 
FileKeyPairProvider(Paths.get(getHostKey())));
+        slow.setPasswordAuthenticator((username, password, session) -> true);
+        slow.setCommandFactory(new TestEchoCommandFactory());
+        slow.setChannelFactories(List.of(new ChannelFactory() {
+            @Override
+            public String getName() {
+                return ChannelSessionFactory.INSTANCE.getName();
+            }
+
+            @Override
+            public Channel createChannel(Session session) throws IOException {
+                try {
+                    Thread.sleep(3000);
+                } catch (InterruptedException e) {
+                    Thread.currentThread().interrupt();
+                }

Review Comment:
   `Thread.sleep()` in test code is prohibited by the project convention 
(AGENTS.md: "Never add `Thread.sleep()`"). The sleep here simulates a server 
that takes 3 seconds to open the channel, but the test only waits 500 ms 
(`channelOpenTimeout=500`) — so it always exits before the sleep finishes. 
Replace the sleep with a latch that the channel factory parks on until the 
test's `finally` block counts it down:
   
   ```suggestion
                   try {
                       CountDownLatch started = new CountDownLatch(1);
                       started.await(3, java.util.concurrent.TimeUnit.SECONDS);
                   } catch (InterruptedException e) {
                       Thread.currentThread().interrupt();
                   }
   ```
   
   Then add `CountDownLatch started = new CountDownLatch(1);` as a field or 
effectively-final local outside the anonymous class, and call 
`started.countDown()` in the `finally` block of the outer `try` (after 
`slow.stop(true)`). This lets `slow.stop(true)` unblock the factory immediately 
instead of waiting 3 seconds after the test passes.



-- 
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