diff --git a/containers/jdk-client/src/test/java/org/glassfish/tyrus/container/jdk/client/ConnectSynchronouslyHangTest.java b/containers/jdk-client/src/test/java/org/glassfish/tyrus/container/jdk/client/ConnectSynchronouslyHangTest.java index 973a2377..5677b701 100644 --- a/containers/jdk-client/src/test/java/org/glassfish/tyrus/container/jdk/client/ConnectSynchronouslyHangTest.java +++ b/containers/jdk-client/src/test/java/org/glassfish/tyrus/container/jdk/client/ConnectSynchronouslyHangTest.java @@ -19,15 +19,19 @@ import java.io.IOException; import java.net.ServerSocket; import java.net.Socket; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; import javax.websocket.ClientEndpoint; import org.glassfish.tyrus.client.ClientManager; +import org.glassfish.tyrus.client.ClientProperties; import org.glassfish.tyrus.test.tools.TestContainer; import org.junit.Test; import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; /** * @author Vladimir Golyakov @@ -36,6 +40,7 @@ public class ConnectSynchronouslyHangTest extends TestContainer { @Test public void testConnectSynchronouslyHang() throws IOException, InterruptedException { + final ClientManager client = ClientManager.createClient(JdkClientContainer.class.getName()); Thread clientThread = null; ServerSocket server = startBadServer(); try { @@ -43,7 +48,8 @@ public void testConnectSynchronouslyHang() throws IOException, InterruptedExcept @Override public void run() { try { - ClientManager client = ClientManager.createClient(JdkClientContainer.class.getName()); + client.getProperties().put(ClientProperties.HANDSHAKE_TIMEOUT, 1); + client.getProperties().put(ClientProperties.SHARED_CONTAINER_IDLE_TIMEOUT, 1); client.connectToServer(AnnotatedClientEndpoint.class, getURI("/any-endpoint", "wss")); } catch (Exception e) { // ignore @@ -60,6 +66,7 @@ public void run() { clientThread.interrupt(); } server.close(); + waitForThreadPoolShutdown(client); } } @@ -88,4 +95,18 @@ public void run() { @ClientEndpoint public static class AnnotatedClientEndpoint { } + + private void waitForThreadPoolShutdown(ClientManager client) { + try { + /* Tests in the package are sensitive to freeing resources. Unclosed sessions might hinder the next test + (if the next test requires a fresh client thread pool) */ + ExecutorService executorService = client.getExecutorService(); + ScheduledExecutorService scheduledExecutorService = client.getScheduledExecutorService(); + client.shutdown(); + assertTrue(executorService.awaitTermination(5, TimeUnit.SECONDS)); + assertTrue(scheduledExecutorService.awaitTermination(5, TimeUnit.SECONDS)); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } }