|
15 | 15 | */ |
16 | 16 | package com.hierynomus.sshj.keepalive; |
17 | 17 |
|
18 | | -import com.hierynomus.sshj.test.KnownFailingTests; |
19 | | -import com.hierynomus.sshj.test.SlowTests; |
20 | 18 | import com.hierynomus.sshj.test.SshFixture; |
| 19 | +import net.schmizz.keepalive.KeepAlive; |
21 | 20 | import net.schmizz.keepalive.KeepAliveProvider; |
22 | 21 | import net.schmizz.sshj.DefaultConfig; |
23 | 22 | import net.schmizz.sshj.SSHClient; |
24 | 23 | import net.schmizz.sshj.userauth.UserAuthException; |
25 | 24 | import org.junit.Rule; |
26 | 25 | import org.junit.Test; |
27 | | -import org.junit.experimental.categories.Category; |
28 | 26 |
|
29 | 27 | import java.io.IOException; |
30 | | -import java.lang.management.ManagementFactory; |
31 | | -import java.lang.management.ThreadInfo; |
32 | | -import java.lang.management.ThreadMXBean; |
33 | 28 |
|
34 | | -import static org.junit.Assert.fail; |
| 29 | +import static org.junit.Assert.assertEquals; |
| 30 | +import static org.junit.Assert.assertFalse; |
| 31 | +import static org.junit.Assert.assertThrows; |
| 32 | +import static org.junit.Assert.assertTrue; |
35 | 33 |
|
36 | 34 | public class KeepAliveThreadTerminationTest { |
37 | 35 |
|
| 36 | + private static final int KEEP_ALIVE_SECONDS = 1; |
| 37 | + |
| 38 | + private static final long STOP_SLEEP = 1500; |
| 39 | + |
38 | 40 | @Rule |
39 | 41 | public SshFixture fixture = new SshFixture(); |
40 | 42 |
|
41 | 43 | @Test |
42 | | - @Category({SlowTests.class, KnownFailingTests.class}) |
43 | | - public void shouldCorrectlyTerminateThreadOnDisconnect() throws IOException, InterruptedException { |
44 | | - DefaultConfig defaultConfig = new DefaultConfig(); |
| 44 | + public void shouldNotStartThreadOnSetKeepAliveInterval() { |
| 45 | + final SSHClient sshClient = setupClient(); |
| 46 | + |
| 47 | + final KeepAlive keepAlive = sshClient.getConnection().getKeepAlive(); |
| 48 | + assertTrue(keepAlive.isDaemon()); |
| 49 | + assertFalse(keepAlive.isAlive()); |
| 50 | + assertEquals(Thread.State.NEW, keepAlive.getState()); |
| 51 | + } |
| 52 | + |
| 53 | + @Test |
| 54 | + public void shouldStartThreadOnConnectAndInterruptOnDisconnect() throws IOException, InterruptedException { |
| 55 | + final SSHClient sshClient = setupClient(); |
| 56 | + |
| 57 | + final KeepAlive keepAlive = sshClient.getConnection().getKeepAlive(); |
| 58 | + assertTrue(keepAlive.isDaemon()); |
| 59 | + assertEquals(Thread.State.NEW, keepAlive.getState()); |
| 60 | + |
| 61 | + fixture.connectClient(sshClient); |
| 62 | + assertEquals(Thread.State.TIMED_WAITING, keepAlive.getState()); |
| 63 | + |
| 64 | + assertThrows(UserAuthException.class, () -> sshClient.authPassword("bad", "credentials")); |
| 65 | + |
| 66 | + fixture.stopClient(); |
| 67 | + Thread.sleep(STOP_SLEEP); |
| 68 | + |
| 69 | + assertFalse(keepAlive.isAlive()); |
| 70 | + assertEquals(Thread.State.TERMINATED, keepAlive.getState()); |
| 71 | + } |
| 72 | + |
| 73 | + private SSHClient setupClient() { |
| 74 | + final DefaultConfig defaultConfig = new DefaultConfig(); |
45 | 75 | defaultConfig.setKeepAliveProvider(KeepAliveProvider.KEEP_ALIVE); |
46 | | - for (int i = 0; i < 10; i++) { |
47 | | - SSHClient sshClient = fixture.setupClient(defaultConfig); |
48 | | - fixture.connectClient(sshClient); |
49 | | - sshClient.getConnection().getKeepAlive().setKeepAliveInterval(1); |
50 | | - try { |
51 | | - sshClient.authPassword("bad", "credentials"); |
52 | | - fail("Should not auth."); |
53 | | - } catch (UserAuthException e) { |
54 | | - // OK |
55 | | - } |
56 | | - fixture.stopClient(); |
57 | | - Thread.sleep(2000); |
58 | | - } |
59 | | - |
60 | | - ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean(); |
61 | | - for (long l : threadMXBean.getAllThreadIds()) { |
62 | | - ThreadInfo threadInfo = threadMXBean.getThreadInfo(l); |
63 | | - if (threadInfo.getThreadName().equals("keep-alive") && threadInfo.getThreadState() != Thread.State.TERMINATED) { |
64 | | - fail("Found alive keep-alive thread in state " + threadInfo.getThreadState()); |
65 | | - } |
66 | | - } |
| 76 | + final SSHClient sshClient = fixture.setupClient(defaultConfig); |
| 77 | + sshClient.getConnection().getKeepAlive().setKeepAliveInterval(KEEP_ALIVE_SECONDS); |
| 78 | + return sshClient; |
67 | 79 | } |
68 | 80 | } |
0 commit comments