From 0918bc626fa641772a8a969a26093d657ee3f163 Mon Sep 17 00:00:00 2001 From: Jeroen van Erp Date: Thu, 24 Aug 2017 13:59:58 +0200 Subject: [PATCH] Improved test stability --- .../connection/channel/direct/LocalPortForwarder.java | 9 +++++++++ .../channel/direct/LocalPortForwarderSpec.groovy | 6 ++++++ 2 files changed, 15 insertions(+) diff --git a/src/main/java/net/schmizz/sshj/connection/channel/direct/LocalPortForwarder.java b/src/main/java/net/schmizz/sshj/connection/channel/direct/LocalPortForwarder.java index 3407e368..0b318097 100644 --- a/src/main/java/net/schmizz/sshj/connection/channel/direct/LocalPortForwarder.java +++ b/src/main/java/net/schmizz/sshj/connection/channel/direct/LocalPortForwarder.java @@ -137,6 +137,15 @@ public class LocalPortForwarder { listen(Thread.currentThread()); } + /** + * Returns whether this listener is running (ie. whether a thread is attached to it). + * + * @return + */ + public boolean isRunning() { + return this.runningThread != null && !serverSocket.isClosed(); + } + /** * Start listening for incoming connections and forward to remote host as a channel and ensure that the thread is registered. * This is useful if for instance {@link #close() is called from another thread} diff --git a/src/test/groovy/com/hierynomus/sshj/connection/channel/direct/LocalPortForwarderSpec.groovy b/src/test/groovy/com/hierynomus/sshj/connection/channel/direct/LocalPortForwarderSpec.groovy index 4c6ac9d6..e7f8f593 100644 --- a/src/test/groovy/com/hierynomus/sshj/connection/channel/direct/LocalPortForwarderSpec.groovy +++ b/src/test/groovy/com/hierynomus/sshj/connection/channel/direct/LocalPortForwarderSpec.groovy @@ -19,6 +19,9 @@ import com.hierynomus.sshj.test.SshFixture import net.schmizz.sshj.connection.channel.direct.LocalPortForwarder import org.junit.Rule import spock.lang.Specification +import spock.util.concurrent.PollingConditions + +import java.util.concurrent.CountDownLatch class LocalPortForwarderSpec extends Specification { @Rule @@ -44,6 +47,9 @@ class LocalPortForwarderSpec extends Specification { thread.start() then: + new PollingConditions().eventually { + lpf.isRunning() + } thread.isAlive() when: