Skip to content

Commit

Permalink
[646] Added waiting for thread pool shutdown to ConnectSynchronouslyH…
Browse files Browse the repository at this point in the history
…angTest

Bug: eclipse-ee4j#646
Signed-off-by: Vladimir Golyakov <[email protected]>
  • Loading branch information
wgolyakov committed Mar 5, 2021
1 parent c87e7a5 commit e8ea50e
Showing 1 changed file with 22 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -36,14 +40,16 @@ 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 {
clientThread = new Thread() {
@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
Expand All @@ -60,6 +66,7 @@ public void run() {
clientThread.interrupt();
}
server.close();
waitForThreadPoolShutdown(client);
}
}

Expand Down Expand Up @@ -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();
}
}
}

0 comments on commit e8ea50e

Please sign in to comment.