gnodet commented on code in PR #25157: URL: https://github.com/apache/camel/pull/25157#discussion_r3665414166
########## components/camel-mina-sftp/src/test/java/org/apache/camel/component/file/remote/mina/sftp/SftpDisconnectThreadLeakIT.java: ########## @@ -0,0 +1,93 @@ +/* + * 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.file.remote.mina.sftp; + +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.Set; +import java.util.concurrent.TimeUnit; +import java.util.stream.Collectors; + +import org.apache.camel.Exchange; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.condition.EnabledIf; + +import static org.awaitility.Awaitility.await; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +/** + * Test that verifies SshClient daemon threads are cleaned up after disconnect (CAMEL-24273). + * <p/> + * Before the fix, each SFTP connect/disconnect cycle left behind NIO2 and timer daemon threads from the SshClient, + * causing the thread count to grow continuously until the system's thread limit was exhausted. + */ +@EnabledIf(value = "org.apache.camel.test.infra.ftp.services.embedded.SftpUtil#hasRequiredAlgorithms('src/test/resources/sftp/hostkey.pem')") +class SftpDisconnectThreadLeakIT extends SftpServerTestSupport { + + @Test + void testDisconnectCleansUpSshClientThreads() throws Exception { + // Capture the set of SshClient thread names before SFTP operations + Set<String> threadsBefore = getSshClientThreadNames(); + + // Perform multiple SFTP transfers — each creates a new SshClient with NIO2/timer threads + int transfers = 5; + for (int i = 0; i < transfers; i++) { + template.sendBodyAndHeader( + "mina-sftp://localhost:{{ftp.server.port}}/{{ftp.root.dir}}" + + "?username=admin&password=admin&disconnect=true&knownHostsFile=" + + service.getKnownHostsFile(), + "Hello World " + i, Exchange.FILE_NAME, "hello" + i + ".txt"); + + Path file = ftpFile("hello" + i + ".txt"); + assertTrue(Files.exists(file), "File should exist: " + file); + } + + // Verify files were written correctly + for (int i = 0; i < transfers; i++) { + Path file = ftpFile("hello" + i + ".txt"); + assertTrue(Files.exists(file), "File should exist: " + file); + assertEquals("Hello World " + i, Files.readString(file)); + } + + // After all transfers with disconnect=true, there should be no SshClient threads remaining. + // Before the fix, each transfer would leave behind ~5 daemon threads (nio2, timer, resume), + // resulting in ~25 leaked threads after 5 transfers. + // Use Awaitility to wait for thread cleanup rather than Thread.sleep(). Review Comment: Good catch — removed the redundant comment block. Kept only a single-line summary of the assertion intent. _Claude Code on behalf of gnodet_ -- 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]
