From 01a7aedf35cbd4fc45a205f7e68c52d533f788ca Mon Sep 17 00:00:00 2001 From: eemhu <125959687+eemhu@users.noreply.github.com> Date: Thu, 7 Mar 2024 12:00:06 +0200 Subject: [PATCH] add 'executorService.shutdown()' call to 'Server.stop()' (#73) * add 'executorService.shutdown()' call to 'Server.stop()' and assertTrue in ServerShutdownTest * add assertFalse(server.executorService.isShutdown()) before shutdown to make sure it stops on server.stop() and not before. * move executorService.shutdown() to after socketPoll while loop. remove from SocketPoll.java. * move executorService.shutdown() to finally block --- src/main/java/com/teragrep/rlp_03/Server.java | 5 +++++ src/main/java/com/teragrep/rlp_03/SocketPoll.java | 1 - .../com/teragrep/rlp_03/ServerShutdownTest.java | 14 ++++++++++++-- 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/teragrep/rlp_03/Server.java b/src/main/java/com/teragrep/rlp_03/Server.java index fbb1ef9d..7cf39c7b 100644 --- a/src/main/java/com/teragrep/rlp_03/Server.java +++ b/src/main/java/com/teragrep/rlp_03/Server.java @@ -115,10 +115,15 @@ public void run() { while (!stop.get()) { socketPoll.poll(); } + } catch (IOException ioException) { throw new UncheckedIOException(ioException); } + finally { + // shutdown executorService when outside of poll() loop + executorService.shutdown(); + } LOGGER.debug("Stopped"); } } \ No newline at end of file diff --git a/src/main/java/com/teragrep/rlp_03/SocketPoll.java b/src/main/java/com/teragrep/rlp_03/SocketPoll.java index be3687d0..acc0cfb5 100644 --- a/src/main/java/com/teragrep/rlp_03/SocketPoll.java +++ b/src/main/java/com/teragrep/rlp_03/SocketPoll.java @@ -140,7 +140,6 @@ public void poll() throws IOException { public void close() throws IOException { serverSocketChannel.close(); selector.close(); - executorService.shutdown(); } private void processAccept(ServerSocketChannel serverSocketChannel, SelectionKey selectionKey) throws IOException { diff --git a/src/test/java/com/teragrep/rlp_03/ServerShutdownTest.java b/src/test/java/com/teragrep/rlp_03/ServerShutdownTest.java index eceba920..c01c6034 100644 --- a/src/test/java/com/teragrep/rlp_03/ServerShutdownTest.java +++ b/src/test/java/com/teragrep/rlp_03/ServerShutdownTest.java @@ -49,6 +49,13 @@ import com.teragrep.rlp_03.config.Config; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.condition.DisabledOnJre; +import org.junit.jupiter.api.condition.JRE; + +import java.io.IOException; + +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; public class ServerShutdownTest { @Test @@ -63,10 +70,12 @@ public void testServerShutdownSingleThread() { server.startup.waitForCompletion(); + Assertions.assertFalse(server.executorService.isShutdown()); server.stop(); - serverThread.join(); + Assertions.assertTrue(server.executorService.isShutdown()); }); + } @Test @@ -81,9 +90,10 @@ public void testServerShutdownMultiThread() { server.startup.waitForCompletion(); + Assertions.assertFalse(server.executorService.isShutdown()); server.stop(); - serverThread.join(); + Assertions.assertTrue(server.executorService.isShutdown()); }); } }