Skip to content

Commit 683f176

Browse files
committed
Amend test to execute service in thread
1 parent 50afba4 commit 683f176

7 files changed

Lines changed: 67 additions & 33 deletions

File tree

‎sockets/pom.xml‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
<artifactId>maven-compiler-plugin</artifactId>
1313
<version>3.3</version>
1414
<configuration>
15-
<source>7</source>
16-
<target>7</target>
15+
<source>8</source>
16+
<target>8</target>
1717
</configuration>
1818
</plugin>
1919
</plugins>

‎sockets/src/main/java/com/baeldung/socket/EchoClient.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public void startConnection(String ip, int port) {
1515
in = new BufferedReader(new InputStreamReader(
1616
clientSocket.getInputStream()));
1717
} catch (IOException e) {
18-
18+
System.out.print(e);
1919
}
2020

2121
}

‎sockets/src/main/java/com/baeldung/socket/GreetClient.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,6 @@ public void stopConnection() {
4141
} catch (IOException e) {
4242
e.printStackTrace();
4343
}
44-
4544
}
45+
4646
}

‎sockets/src/main/java/com/baeldung/socket/GreetServer.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public void start(int port) {
2121
if ("hello server".equals(greeting))
2222
out.println("hello client");
2323
else
24-
out.println("unrecognised greetin");
24+
out.println("unrecognised greeting");
2525
} catch (IOException e) {
2626
e.printStackTrace();
2727
}

‎sockets/src/test/java/com/baeldung/socket/EchoMultiTest.java‎

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,56 @@
11
package com.baeldung.socket;
22

3-
import static org.junit.Assert.*;
4-
3+
import org.junit.After;
4+
import org.junit.Before;
55
import org.junit.Test;
66

7+
import java.util.concurrent.Executors;
8+
9+
import static org.junit.Assert.assertEquals;
10+
711
public class EchoMultiTest {
12+
13+
{
14+
Executors.newSingleThreadExecutor().submit(() -> new EchoMultiServer().start(5555));
15+
}
16+
17+
EchoClient client = new EchoClient();
18+
19+
@Before
20+
public void init() {
21+
client.startConnection("127.0.0.1", 5555);
22+
}
23+
24+
@After
25+
public void finish(){
26+
client.stopConnection();
27+
}
28+
829
@Test
930
public void givenClient1_whenServerResponds_thenCorrect() {
10-
EchoClient client1 = new EchoClient();
11-
client1.startConnection("127.0.0.1", 5555);
12-
String msg1 = client1.sendMessage("hello");
13-
String msg2 = client1.sendMessage("world");
14-
String terminate = client1.sendMessage(".");
31+
String msg1 = client.sendMessage("hello");
32+
String msg2 = client.sendMessage("world");
33+
String terminate = client.sendMessage(".");
1534
assertEquals(msg1, "hello");
1635
assertEquals(msg2, "world");
1736
assertEquals(terminate, "bye");
1837
}
1938

2039
@Test
2140
public void givenClient2_whenServerResponds_thenCorrect() {
22-
EchoClient client1 = new EchoClient();
23-
client1.startConnection("127.0.0.1", 5555);
24-
String msg1 = client1.sendMessage("hello");
25-
String msg2 = client1.sendMessage("world");
26-
String terminate = client1.sendMessage(".");
41+
String msg1 = client.sendMessage("hello");
42+
String msg2 = client.sendMessage("world");
43+
String terminate = client.sendMessage(".");
2744
assertEquals(msg1, "hello");
2845
assertEquals(msg2, "world");
2946
assertEquals(terminate, "bye");
3047
}
3148

3249
@Test
3350
public void givenClient3_whenServerResponds_thenCorrect() {
34-
EchoClient client1 = new EchoClient();
35-
client1.startConnection("127.0.0.1", 5555);
36-
String msg1 = client1.sendMessage("hello");
37-
String msg2 = client1.sendMessage("world");
38-
String terminate = client1.sendMessage(".");
51+
String msg1 = client.sendMessage("hello");
52+
String msg2 = client.sendMessage("world");
53+
String terminate = client.sendMessage(".");
3954
assertEquals(msg1, "hello");
4055
assertEquals(msg2, "world");
4156
assertEquals(terminate, "bye");

‎sockets/src/test/java/com/baeldung/socket/EchoTest.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
package com.baeldung.socket;
22

3-
import static org.junit.Assert.assertEquals;
4-
53
import org.junit.After;
64
import org.junit.Before;
75
import org.junit.Test;
86

7+
import static org.junit.Assert.assertEquals;
8+
99
public class EchoTest {
1010
EchoClient client = null;
1111

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,35 @@
11
package com.baeldung.socket;
22

3-
import static org.junit.Assert.assertEquals;
4-
3+
import org.junit.After;
4+
import org.junit.Before;
55
import org.junit.Test;
66

7+
import java.util.concurrent.Executors;
8+
9+
import static org.junit.Assert.assertEquals;
10+
711
public class GreetServerTest {
8-
@Test
9-
public void givenGreetingClient_whenServerRespondsWhenStarted_thenCorrect() {
10-
GreetClient client = new GreetClient();
11-
client.startConnection("127.0.0.1", 6666);
12-
String response = client.sendMessage("hello server");
13-
assertEquals("hello client", response);
14-
}
1512

13+
GreetClient client;
14+
15+
{
16+
Executors.newSingleThreadExecutor().submit(() -> new GreetServer().start(6666));
17+
}
18+
19+
@Before
20+
public void init() {
21+
client = new GreetClient();
22+
}
23+
24+
@Test
25+
public void givenGreetingClient_whenServerRespondsWhenStarted_thenCorrect() {
26+
client.startConnection("127.0.0.1", 6666);
27+
String response = client.sendMessage("hello server");
28+
assertEquals("hello client", response);
29+
}
30+
31+
@After
32+
public void finish() {
33+
client.stopConnection();
34+
}
1635
}

0 commit comments

Comments
 (0)