Skip to content

Commit c48a5c2

Browse files
khatwaniNikhilpivovarit
authored andcommitted
Fixed Failing EchoTest (eugenp#782)
1 parent 33a183b commit c48a5c2

3 files changed

Lines changed: 30 additions & 16 deletions

File tree

‎core-java/src/main/java/com/baeldung/java/nio/selector/EchoClient.java‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ public static EchoClient start() {
1717
return instance;
1818
}
1919

20+
public static void stop() throws IOException {
21+
client.close();
22+
buffer = null;
23+
}
24+
2025
private EchoClient() {
2126
try {
2227
client = SocketChannel.open(new InetSocketAddress("localhost", 5454));
@@ -42,5 +47,4 @@ public String sendMessage(String msg) {
4247
return response;
4348

4449
}
45-
4650
}

‎core-java/src/main/java/com/baeldung/java/nio/selector/EchoServer.java‎

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,19 @@
11
package com.baeldung.java.nio.selector;
22

3+
import java.io.File;
4+
import java.io.IOException;
5+
import java.net.InetSocketAddress;
6+
import java.nio.ByteBuffer;
7+
import java.nio.channels.SelectionKey;
8+
import java.nio.channels.Selector;
39
import java.nio.channels.ServerSocketChannel;
410
import java.nio.channels.SocketChannel;
5-
import java.nio.channels.Selector;
6-
import java.nio.channels.SelectionKey;
7-
import java.nio.ByteBuffer;
8-
import java.io.IOException;
9-
import java.util.Set;
1011
import java.util.Iterator;
11-
import java.net.InetSocketAddress;
12-
import java.io.File;
12+
import java.util.Set;
1313

1414
public class EchoServer {
1515

16-
public static void main(String[] args)
17-
18-
throws IOException {
16+
public static void main(String[] args) throws IOException {
1917
Selector selector = Selector.open();
2018
ServerSocketChannel serverSocket = ServerSocketChannel.open();
2119
serverSocket.bind(new InetSocketAddress("localhost", 5454));
@@ -49,7 +47,6 @@ public static void main(String[] args)
4947
}
5048
}
5149

52-
5350
public static Process start() throws IOException, InterruptedException {
5451
String javaHome = System.getProperty("java.home");
5552
String javaBin = javaHome + File.separator + "bin" + File.separator + "java";

‎core-java/src/test/java/com/baeldung/java/nio/selector/NioEchoIntegrationTest.java‎

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,32 @@
44

55
import java.io.IOException;
66

7+
import org.junit.After;
8+
import org.junit.Before;
79
import org.junit.Test;
810

911
public class NioEchoIntegrationTest {
1012

13+
Process server;
14+
EchoClient client;
15+
16+
@Before
17+
public void setup() throws IOException, InterruptedException {
18+
server = EchoServer.start();
19+
client = EchoClient.start();
20+
}
21+
1122
@Test
12-
public void givenClient_whenServerEchosMessage_thenCorrect() throws IOException, InterruptedException {
13-
Process process = EchoServer.start();
14-
EchoClient client = EchoClient.start();
23+
public void givenServerClient_whenServerEchosMessage_thenCorrect() {
1524
String resp1 = client.sendMessage("hello");
1625
String resp2 = client.sendMessage("world");
1726
assertEquals("hello", resp1);
1827
assertEquals("world", resp2);
28+
}
1929

20-
process.destroy();
30+
@After
31+
public void teardown() throws IOException {
32+
server.destroy();
33+
EchoClient.stop();
2134
}
2235
}

0 commit comments

Comments
 (0)