Testing: adjust WolfSSLEngineTest to properly close sockets and close ExecutorService
parent
ca24a84dca
commit
12f7654b5c
|
@ -1232,8 +1232,10 @@ public class WolfSSLEngineTest {
|
||||||
|
|
||||||
/* Start up simple TLS test server */
|
/* Start up simple TLS test server */
|
||||||
CountDownLatch serverOpenLatch = new CountDownLatch(1);
|
CountDownLatch serverOpenLatch = new CountDownLatch(1);
|
||||||
InternalMultiThreadedSSLSocketServer server =
|
InternalMultiThreadedSSLSocketServer server = null;
|
||||||
new InternalMultiThreadedSSLSocketServer(svrPort, serverOpenLatch,
|
|
||||||
|
try {
|
||||||
|
server = new InternalMultiThreadedSSLSocketServer(svrPort, serverOpenLatch,
|
||||||
numThreads);
|
numThreads);
|
||||||
server.start();
|
server.start();
|
||||||
|
|
||||||
|
@ -1261,7 +1263,6 @@ public class WolfSSLEngineTest {
|
||||||
|
|
||||||
/* Wait for all client threads to finish, else time out */
|
/* Wait for all client threads to finish, else time out */
|
||||||
returnWithoutTimeout = latch.await(10, TimeUnit.SECONDS);
|
returnWithoutTimeout = latch.await(10, TimeUnit.SECONDS);
|
||||||
server.join(1000);
|
|
||||||
|
|
||||||
/* check failure count and success count against thread count */
|
/* check failure count and success count against thread count */
|
||||||
if (failures.get(0) == 0 && success.get(0) == numThreads) {
|
if (failures.get(0) == 0 && success.get(0) == numThreads) {
|
||||||
|
@ -1278,6 +1279,29 @@ public class WolfSSLEngineTest {
|
||||||
fail("SSLEngine threading error, threads timed out");
|
fail("SSLEngine threading error, threads timed out");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} finally {
|
||||||
|
/* Ensure proper cleanup in all cases */
|
||||||
|
|
||||||
|
/* Close server socket to ensure it's released */
|
||||||
|
if (server != null) {
|
||||||
|
server.closeSocket();
|
||||||
|
try {
|
||||||
|
server.join(1000);
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
/* Ignore */
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Shutdown executor service and wait for it to terminate */
|
||||||
|
service.shutdown();
|
||||||
|
try {
|
||||||
|
if (!service.awaitTermination(5, TimeUnit.SECONDS)) {
|
||||||
|
service.shutdownNow();
|
||||||
|
}
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
service.shutdownNow();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1548,6 +1572,7 @@ public class WolfSSLEngineTest {
|
||||||
private int serverPort;
|
private int serverPort;
|
||||||
private CountDownLatch serverOpenLatch = null;
|
private CountDownLatch serverOpenLatch = null;
|
||||||
private int clientConnections = 1;
|
private int clientConnections = 1;
|
||||||
|
private SSLServerSocket ss = null;
|
||||||
|
|
||||||
public InternalMultiThreadedSSLSocketServer(
|
public InternalMultiThreadedSSLSocketServer(
|
||||||
int port, CountDownLatch openLatch, int clientConnections) {
|
int port, CountDownLatch openLatch, int clientConnections) {
|
||||||
|
@ -1556,11 +1581,24 @@ public class WolfSSLEngineTest {
|
||||||
this.clientConnections = clientConnections;
|
this.clientConnections = clientConnections;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Explicitly closes the server socket if still open
|
||||||
|
*/
|
||||||
|
public void closeSocket() {
|
||||||
|
if (ss != null) {
|
||||||
|
try {
|
||||||
|
ss.close();
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
try {
|
try {
|
||||||
SSLContext ctx = tf.createSSLContext("TLS", engineProvider);
|
SSLContext ctx = tf.createSSLContext("TLS", engineProvider);
|
||||||
SSLServerSocket ss = (SSLServerSocket)ctx
|
ss = (SSLServerSocket)ctx
|
||||||
.getServerSocketFactory().createServerSocket(serverPort);
|
.getServerSocketFactory().createServerSocket(serverPort);
|
||||||
|
|
||||||
while (clientConnections > 0) {
|
while (clientConnections > 0) {
|
||||||
|
@ -1573,6 +1611,15 @@ public class WolfSSLEngineTest {
|
||||||
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
|
} finally {
|
||||||
|
/* Ensure server socket is closed */
|
||||||
|
if (ss != null) {
|
||||||
|
try {
|
||||||
|
ss.close();
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1592,9 +1639,17 @@ public class WolfSSLEngineTest {
|
||||||
sock.startHandshake();
|
sock.startHandshake();
|
||||||
sock.getInputStream().read(response);
|
sock.getInputStream().read(response);
|
||||||
sock.getOutputStream().write(msg.getBytes());
|
sock.getOutputStream().write(msg.getBytes());
|
||||||
sock.close();
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
|
} finally {
|
||||||
|
/* Ensure socket is closed */
|
||||||
|
try {
|
||||||
|
if (sock != null && !sock.isClosed()) {
|
||||||
|
sock.close();
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue