Skip to content

Commit

Permalink
add 'executorService.shutdown()' call to 'Server.stop()' (#73)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
eemhu authored Mar 7, 2024
1 parent 7d5c9ba commit 01a7aed
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 deletions.
5 changes: 5 additions & 0 deletions src/main/java/com/teragrep/rlp_03/Server.java
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
}
1 change: 0 additions & 1 deletion src/main/java/com/teragrep/rlp_03/SocketPoll.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
14 changes: 12 additions & 2 deletions src/test/java/com/teragrep/rlp_03/ServerShutdownTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -81,9 +90,10 @@ public void testServerShutdownMultiThread() {

server.startup.waitForCompletion();

Assertions.assertFalse(server.executorService.isShutdown());
server.stop();

serverThread.join();
Assertions.assertTrue(server.executorService.isShutdown());
});
}
}

0 comments on commit 01a7aed

Please sign in to comment.