|
1 | 1 | /*
|
2 |
| - * Copyright 2014-2024 the original author or authors. |
| 2 | + * Copyright 2014-2025 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
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;
|
|
35 | 40 | import org.apache.sshd.server.SshServer;
|
36 | 41 | import org.apache.sshd.server.keyprovider.SimpleGeneratorHostKeyProvider;
|
37 | 42 | import org.apache.sshd.sftp.client.SftpClient;
|
| 43 | +import org.apache.sshd.sftp.client.SftpErrorDataHandler; |
| 44 | +import org.apache.sshd.sftp.client.SftpVersionSelector; |
38 | 45 | import org.apache.sshd.sftp.client.impl.AbstractSftpClient;
|
39 | 46 | import org.apache.sshd.sftp.server.SftpSubsystemFactory;
|
40 | 47 | import org.junit.jupiter.api.Test;
|
@@ -260,4 +267,65 @@ void clientSessionIsClosedOnSessionClose() throws Exception {
|
260 | 267 | sftpSessionFactory.destroy();
|
261 | 268 | }
|
262 | 269 | }
|
| 270 | + |
| 271 | + @Test |
| 272 | + void sharedSessionConcurrentAccess() throws Exception { |
| 273 | + try (SshServer server = SshServer.setUpDefaultServer()) { |
| 274 | + server.setPasswordAuthenticator((arg0, arg1, arg2) -> true); |
| 275 | + server.setPort(0); |
| 276 | + server.setKeyPairProvider(new SimpleGeneratorHostKeyProvider(new File("hostkey.ser").toPath())); |
| 277 | + server.setSubsystemFactories(Collections.singletonList(new SftpSubsystemFactory())); |
| 278 | + server.start(); |
| 279 | + |
| 280 | + AtomicInteger clientInstances = new AtomicInteger(); |
| 281 | + |
| 282 | + DefaultSftpSessionFactory sftpSessionFactory = new DefaultSftpSessionFactory(true) { |
| 283 | + |
| 284 | + @Override |
| 285 | + protected SftpClient createSftpClient(ClientSession clientSession, |
| 286 | + SftpVersionSelector initialVersionSelector, SftpErrorDataHandler errorDataHandler) |
| 287 | + throws IOException { |
| 288 | + |
| 289 | + clientInstances.incrementAndGet(); |
| 290 | + return super.createSftpClient(clientSession, initialVersionSelector, errorDataHandler); |
| 291 | + } |
| 292 | + |
| 293 | + }; |
| 294 | + sftpSessionFactory.setHost("localhost"); |
| 295 | + sftpSessionFactory.setPort(server.getPort()); |
| 296 | + sftpSessionFactory.setUser("user"); |
| 297 | + sftpSessionFactory.setPassword("pass"); |
| 298 | + sftpSessionFactory.setAllowUnknownKeys(true); |
| 299 | + |
| 300 | + ExecutorService executorService = Executors.newFixedThreadPool(10); |
| 301 | + |
| 302 | + CountDownLatch executionLatch = new CountDownLatch(20); |
| 303 | + List<Exception> errors = Collections.synchronizedList(new ArrayList<>()); |
| 304 | + |
| 305 | + for (int i = 0; i < 20; i++) { |
| 306 | + executorService.execute(() -> { |
| 307 | + try (SftpSession session = sftpSessionFactory.getSession()) { |
| 308 | + session.list("."); |
| 309 | + } |
| 310 | + catch (Exception e) { |
| 311 | + errors.add(e); |
| 312 | + } |
| 313 | + executionLatch.countDown(); |
| 314 | + }); |
| 315 | + } |
| 316 | + |
| 317 | + assertThat(executionLatch.await(10, TimeUnit.SECONDS)).isTrue(); |
| 318 | + synchronized (errors) { |
| 319 | + assertThat(errors).isEmpty(); |
| 320 | + } |
| 321 | + |
| 322 | + assertThat(clientInstances).hasValue(1); |
| 323 | + |
| 324 | + executorService.shutdown(); |
| 325 | + assertThat(executorService.awaitTermination(10, TimeUnit.SECONDS)).isTrue(); |
| 326 | + |
| 327 | + sftpSessionFactory.destroy(); |
| 328 | + } |
| 329 | + } |
| 330 | + |
263 | 331 | }
|
0 commit comments