|
24 | 24 | import java.util.ArrayList;
|
25 | 25 | import java.util.Collections;
|
26 | 26 | import java.util.List;
|
| 27 | +import java.util.concurrent.CountDownLatch; |
| 28 | +import java.util.concurrent.ExecutorService; |
| 29 | +import java.util.concurrent.Executors; |
| 30 | +import java.util.concurrent.TimeUnit; |
| 31 | +import java.util.concurrent.atomic.AtomicInteger; |
27 | 32 | import java.util.stream.IntStream;
|
28 | 33 |
|
29 | 34 | import org.apache.sshd.client.SshClient;
|
|
37 | 42 | import org.apache.sshd.server.SshServer;
|
38 | 43 | import org.apache.sshd.server.keyprovider.SimpleGeneratorHostKeyProvider;
|
39 | 44 | import org.apache.sshd.sftp.client.SftpClient;
|
| 45 | +import org.apache.sshd.sftp.client.SftpErrorDataHandler; |
| 46 | +import org.apache.sshd.sftp.client.SftpVersionSelector; |
40 | 47 | import org.apache.sshd.sftp.client.impl.AbstractSftpClient;
|
41 | 48 | import org.apache.sshd.sftp.server.SftpSubsystemFactory;
|
42 | 49 | import org.junit.jupiter.api.Test;
|
@@ -268,4 +275,64 @@ void clientSessionIsClosedOnSessionClose() throws Exception {
|
268 | 275 | }
|
269 | 276 | }
|
270 | 277 |
|
| 278 | + @Test |
| 279 | + void sharedSessionConcurrentAccess() throws Exception { |
| 280 | + try (SshServer server = SshServer.setUpDefaultServer()) { |
| 281 | + server.setPasswordAuthenticator((arg0, arg1, arg2) -> true); |
| 282 | + server.setPort(0); |
| 283 | + server.setKeyPairProvider(new SimpleGeneratorHostKeyProvider(new File("hostkey.ser").toPath())); |
| 284 | + server.setSubsystemFactories(Collections.singletonList(new SftpSubsystemFactory())); |
| 285 | + server.start(); |
| 286 | + |
| 287 | + AtomicInteger clientInstances = new AtomicInteger(); |
| 288 | + |
| 289 | + DefaultSftpSessionFactory sftpSessionFactory = new DefaultSftpSessionFactory(true) { |
| 290 | + |
| 291 | + @Override |
| 292 | + protected SftpClient createSftpClient(ClientSession clientSession, |
| 293 | + SftpVersionSelector initialVersionSelector, SftpErrorDataHandler errorDataHandler) |
| 294 | + throws IOException { |
| 295 | + |
| 296 | + clientInstances.incrementAndGet(); |
| 297 | + return super.createSftpClient(clientSession, initialVersionSelector, errorDataHandler); |
| 298 | + } |
| 299 | + |
| 300 | + }; |
| 301 | + sftpSessionFactory.setHost("localhost"); |
| 302 | + sftpSessionFactory.setPort(server.getPort()); |
| 303 | + sftpSessionFactory.setUser("user"); |
| 304 | + sftpSessionFactory.setPassword("pass"); |
| 305 | + sftpSessionFactory.setAllowUnknownKeys(true); |
| 306 | + |
| 307 | + ExecutorService executorService = Executors.newFixedThreadPool(10); |
| 308 | + |
| 309 | + CountDownLatch executionLatch = new CountDownLatch(20); |
| 310 | + List<Exception> errors = Collections.synchronizedList(new ArrayList<>()); |
| 311 | + |
| 312 | + for (int i = 0; i < 20; i++) { |
| 313 | + executorService.execute(() -> { |
| 314 | + try (SftpSession session = sftpSessionFactory.getSession()) { |
| 315 | + session.list("."); |
| 316 | + } |
| 317 | + catch (Exception e) { |
| 318 | + errors.add(e); |
| 319 | + } |
| 320 | + executionLatch.countDown(); |
| 321 | + }); |
| 322 | + } |
| 323 | + |
| 324 | + assertThat(executionLatch.await(10, TimeUnit.SECONDS)).isTrue(); |
| 325 | + synchronized (errors) { |
| 326 | + assertThat(errors).isEmpty(); |
| 327 | + } |
| 328 | + |
| 329 | + assertThat(clientInstances).hasValue(1); |
| 330 | + |
| 331 | + executorService.shutdown(); |
| 332 | + assertThat(executorService.awaitTermination(10, TimeUnit.SECONDS)).isTrue(); |
| 333 | + |
| 334 | + sftpSessionFactory.destroy(); |
| 335 | + } |
| 336 | + } |
| 337 | + |
271 | 338 | }
|
0 commit comments