diff --git a/core-java/src/main/java/com/baeldung/java/nio/selector/EchoClient.java b/core-java/src/main/java/com/baeldung/java/nio/selector/EchoClient.java index 1c034051aa..61f339db58 100644 --- a/core-java/src/main/java/com/baeldung/java/nio/selector/EchoClient.java +++ b/core-java/src/main/java/com/baeldung/java/nio/selector/EchoClient.java @@ -17,6 +17,11 @@ public class EchoClient { return instance; } + public static void stop() throws IOException { + client.close(); + buffer = null; + } + private EchoClient() { try { client = SocketChannel.open(new InetSocketAddress("localhost", 5454)); @@ -42,5 +47,4 @@ public class EchoClient { return response; } - } diff --git a/core-java/src/main/java/com/baeldung/java/nio/selector/EchoServer.java b/core-java/src/main/java/com/baeldung/java/nio/selector/EchoServer.java index 1d4e01bbc3..2ed9a27c4c 100644 --- a/core-java/src/main/java/com/baeldung/java/nio/selector/EchoServer.java +++ b/core-java/src/main/java/com/baeldung/java/nio/selector/EchoServer.java @@ -1,21 +1,19 @@ package com.baeldung.java.nio.selector; +import java.io.File; +import java.io.IOException; +import java.net.InetSocketAddress; +import java.nio.ByteBuffer; +import java.nio.channels.SelectionKey; +import java.nio.channels.Selector; import java.nio.channels.ServerSocketChannel; import java.nio.channels.SocketChannel; -import java.nio.channels.Selector; -import java.nio.channels.SelectionKey; -import java.nio.ByteBuffer; -import java.io.IOException; -import java.util.Set; import java.util.Iterator; -import java.net.InetSocketAddress; -import java.io.File; +import java.util.Set; public class EchoServer { - public static void main(String[] args) - - throws IOException { + public static void main(String[] args) throws IOException { Selector selector = Selector.open(); ServerSocketChannel serverSocket = ServerSocketChannel.open(); serverSocket.bind(new InetSocketAddress("localhost", 5454)); @@ -49,7 +47,6 @@ public class EchoServer { } } - public static Process start() throws IOException, InterruptedException { String javaHome = System.getProperty("java.home"); String javaBin = javaHome + File.separator + "bin" + File.separator + "java"; diff --git a/core-java/src/test/java/com/baeldung/java/nio/selector/NioEchoIntegrationTest.java b/core-java/src/test/java/com/baeldung/java/nio/selector/NioEchoIntegrationTest.java index 748dc5b9f4..fc64799578 100644 --- a/core-java/src/test/java/com/baeldung/java/nio/selector/NioEchoIntegrationTest.java +++ b/core-java/src/test/java/com/baeldung/java/nio/selector/NioEchoIntegrationTest.java @@ -4,19 +4,32 @@ import static org.junit.Assert.assertEquals; import java.io.IOException; +import org.junit.After; +import org.junit.Before; import org.junit.Test; public class NioEchoIntegrationTest { + Process server; + EchoClient client; + + @Before + public void setup() throws IOException, InterruptedException { + server = EchoServer.start(); + client = EchoClient.start(); + } + @Test - public void givenClient_whenServerEchosMessage_thenCorrect() throws IOException, InterruptedException { - Process process = EchoServer.start(); - EchoClient client = EchoClient.start(); + public void givenServerClient_whenServerEchosMessage_thenCorrect() { String resp1 = client.sendMessage("hello"); String resp2 = client.sendMessage("world"); assertEquals("hello", resp1); assertEquals("world", resp2); + } - process.destroy(); + @After + public void teardown() throws IOException { + server.destroy(); + EchoClient.stop(); } }