|
| 1 | +/* |
| 2 | + * Copyright (C)2009 - SSHJ Contributors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package com.hierynomus.sshj |
| 17 | + |
| 18 | +import net.schmizz.sshj.SSHClient |
| 19 | +import net.schmizz.sshj.common.IOUtils |
| 20 | +import net.schmizz.sshj.connection.channel.direct.Session |
| 21 | +import spock.lang.Specification |
| 22 | + |
| 23 | +import java.util.concurrent.* |
| 24 | + |
| 25 | +import static org.codehaus.groovy.runtime.IOGroovyMethods.withCloseable |
| 26 | + |
| 27 | +class ManyChannelsSpec extends Specification { |
| 28 | + |
| 29 | + def "should work with many channels without nonexistent channel error (GH issue #805)"() { |
| 30 | + given: |
| 31 | + SshdContainer sshd = new SshdContainer.Builder() |
| 32 | + .withSshdConfig("""${SshdContainer.Builder.DEFAULT_SSHD_CONFIG} |
| 33 | + MaxSessions 200 |
| 34 | + """.stripMargin()) |
| 35 | + .build() |
| 36 | + sshd.start() |
| 37 | + SSHClient client = sshd.getConnectedClient() |
| 38 | + client.authPublickey("sshj", "src/test/resources/id_rsa") |
| 39 | +
|
| 40 | + when: |
| 41 | + List<Future<Exception>> futures = [] |
| 42 | + ExecutorService executorService = Executors.newCachedThreadPool() |
| 43 | +
|
| 44 | + for (int i in 0..20) { |
| 45 | + futures.add(executorService.submit((Callable<Exception>) { |
| 46 | + return execute(client) |
| 47 | + })) |
| 48 | + } |
| 49 | + executorService.shutdown() |
| 50 | + executorService.awaitTermination(1, TimeUnit.DAYS) |
| 51 | +
|
| 52 | + then: |
| 53 | + futures*.get().findAll { it != null }.empty |
| 54 | +
|
| 55 | + cleanup: |
| 56 | + client.close() |
| 57 | + } |
| 58 | +
|
| 59 | +
|
| 60 | + private static Exception execute(SSHClient sshClient) { |
| 61 | + try { |
| 62 | + for (def i in 0..100) { |
| 63 | + withCloseable (sshClient.startSession()) {sshSession -> |
| 64 | + Session.Command sshCommand = sshSession.exec("ls -la") |
| 65 | + IOUtils.readFully(sshCommand.getInputStream()).toString() |
| 66 | + sshCommand.close() |
| 67 | + } |
| 68 | + } |
| 69 | + } catch (Exception e) { |
| 70 | + return e |
| 71 | + } |
| 72 | + return null |
| 73 | + } |
| 74 | +} |
0 commit comments