Skip to content

Commit 7356fba

Browse files
committed
test: Ensure closing of servers & cleaning
1 parent febbc58 commit 7356fba

File tree

7 files changed

+103
-96
lines changed

7 files changed

+103
-96
lines changed

src/test/java/org/monora/coolsocket/core/ClientManagementTest.java

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import org.monora.coolsocket.core.session.ClosedException;
1515
import org.monora.coolsocket.core.variant.Connections;
1616
import org.monora.coolsocket.core.variant.DefaultCoolSocket;
17+
import org.monora.coolsocket.core.variant.factory.TestConfigFactory;
1718

1819
import java.io.IOException;
1920
import java.net.InetAddress;
@@ -116,32 +117,35 @@ public void onConnected(@NotNull ActiveConnection activeConnection)
116117

117118
CoolSocket.Session session = coolSocket.getSession();
118119
ActiveConnection[] connections = new ActiveConnection[5];
120+
InetAddress localhost = InetAddress.getByName(TestConfigFactory.SOCKET_ADDRESS_HOST);
119121

120-
Assert.assertNotNull(session);
122+
try {
123+
Assert.assertNotNull(session);
121124

122-
for (int i = 0; i < connections.length; i++) {
123-
ActiveConnection activeConnection = Connections.connect();
124-
connections[i] = activeConnection;
125+
for (int i = 0; i < connections.length; i++) {
126+
ActiveConnection activeConnection = Connections.connect();
127+
connections[i] = activeConnection;
125128

126-
activeConnection.reply(message);
127-
}
129+
activeConnection.reply(message);
130+
}
128131

129-
Assert.assertEquals("Number of connections should be same.", connections.length,
130-
session.getConnectionManager().getActiveConnectionList().size());
132+
Assert.assertEquals("Number of connections should be same.", connections.length,
133+
session.getConnectionManager().getActiveConnectionList().size());
131134

132-
Assert.assertEquals("Number of connections should be same.", connections.length,
133-
session.getConnectionManager().getConnectionCountByAddress(InetAddress.getLocalHost()));
135+
Assert.assertEquals("Number of connections should be same.", connections.length,
136+
session.getConnectionManager().getConnectionCountByAddress(localhost));
134137

135-
for (ActiveConnection activeConnection : connections) {
136-
try {
137-
activeConnection.closeSafely();
138-
activeConnection.reply(message);
139-
} catch (ClosedException ignored) {
138+
for (ActiveConnection activeConnection : connections) {
139+
try {
140+
activeConnection.closeSafely();
141+
activeConnection.reply(message);
142+
} catch (ClosedException ignored) {
143+
}
140144
}
145+
} finally {
146+
coolSocket.stop();
141147
}
142148

143-
coolSocket.stop();
144-
145149
Assert.assertEquals("Connections should zeroed", 0,
146150
session.getConnectionManager().getActiveConnectionList().size());
147151
}

src/test/java/org/monora/coolsocket/core/CommandExecutionTest.java

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,8 @@
1010
import org.monora.coolsocket.core.variant.Connections;
1111
import org.monora.coolsocket.core.variant.DefaultCoolSocket;
1212
import org.monora.coolsocket.core.variant.StaticMessageCoolSocket;
13-
import org.monora.coolsocket.core.variant.factory.TestConfigFactory;
1413

1514
import java.io.IOException;
16-
import java.net.InetSocketAddress;
1715
import java.net.SocketException;
1816

1917
public class CommandExecutionTest
@@ -48,7 +46,7 @@ public void onConnected(@NotNull ActiveConnection activeConnection)
4846
@Test(expected = CancelledException.class)
4947
public void cancellationDuringReadBeginTest() throws IOException, InterruptedException
5048
{
51-
CoolSocket coolSocket = new DefaultCoolSocket()
49+
CoolSocket coolSocket = new DefaultCoolSocket()
5250
{
5351
@Override
5452
public void onConnected(@NotNull ActiveConnection activeConnection)
@@ -100,20 +98,23 @@ public void onConnected(@NotNull ActiveConnection activeConnection)
10098

10199
coolSocket.start();
102100

103-
ActiveConnection activeConnection = Connections.connect();
104-
105101
try {
106-
activeConnection.writeBegin(0);
107-
} catch (CancelledException ignored) {
108-
} catch (IOException e) {
109-
e.printStackTrace();
110-
}
102+
ActiveConnection activeConnection = Connections.connect();
111103

112-
Assert.assertEquals("The messages should be the same.", message,
113-
activeConnection.receive().getAsString());
104+
try {
105+
activeConnection.writeBegin(0);
106+
} catch (CancelledException ignored) {
107+
} catch (IOException e) {
108+
e.printStackTrace();
109+
}
110+
111+
Assert.assertEquals("The messages should be the same.", message,
112+
activeConnection.receive().getAsString());
114113

115-
activeConnection.close();
116-
coolSocket.stop();
114+
activeConnection.close();
115+
} finally {
116+
coolSocket.stop();
117+
}
117118
}
118119

119120
@Test

src/test/java/org/monora/coolsocket/core/ConfigTest.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,12 @@ public void randomPortTest() throws IOException, InterruptedException
1515
Assert.assertEquals("The random port should be 0 when not started.", 0,
1616
coolSocket.getLocalPort());
1717
coolSocket.start();
18-
Assert.assertNotEquals("The random port should be the assigned port when started",
19-
coolSocket.getLocalPort(), 0);
20-
coolSocket.stop();
18+
19+
try {
20+
Assert.assertNotEquals("The random port should be the assigned port when started",
21+
coolSocket.getLocalPort(), 0);
22+
} finally {
23+
coolSocket.stop();
24+
}
2125
}
2226
}

src/test/java/org/monora/coolsocket/core/DataTransactionTest.java

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@ public void onConnected(@NotNull ActiveConnection activeConnection)
6868
} finally {
6969
coolSocket.stop();
7070
}
71-
7271
}
7372

7473
@Test(expected = SizeMismatchException.class)
@@ -91,13 +90,10 @@ public void onConnected(@NotNull ActiveConnection activeConnection)
9190

9291
coolSocket.start();
9392

94-
ActiveConnection activeConnection = Connections.connect();
95-
activeConnection.setInternalCacheSize(100);
96-
97-
try {
93+
try (ActiveConnection activeConnection = Connections.connect()) {
94+
activeConnection.setInternalCacheSize(100);
9895
activeConnection.receive();
9996
} finally {
100-
activeConnection.close();
10197
coolSocket.stop();
10298
}
10399
}
@@ -124,11 +120,12 @@ public void onConnected(@NotNull ActiveConnection activeConnection)
124120

125121
coolSocket.start();
126122

127-
ActiveConnection activeConnection = Connections.connect();
128-
activeConnection.setInternalCacheSize(size);
129-
activeConnection.receive();
130-
activeConnection.close();
131-
coolSocket.stop();
123+
try (ActiveConnection activeConnection = Connections.connect()) {
124+
activeConnection.setInternalCacheSize(size);
125+
activeConnection.receive();
126+
} finally {
127+
coolSocket.stop();
128+
}
132129
}
133130

134131
@Test
@@ -156,10 +153,11 @@ public void onConnected(@NotNull ActiveConnection activeConnection)
156153

157154
coolSocket.start();
158155

159-
ActiveConnection activeConnection = Connections.connect();
160-
Assert.assertEquals("The messages should match.", message, activeConnection.receive().getAsString());
161-
activeConnection.close();
162-
coolSocket.stop();
156+
try (ActiveConnection activeConnection = Connections.connect()) {
157+
Assert.assertEquals("The messages should match.", message, activeConnection.receive().getAsString());
158+
} finally {
159+
coolSocket.stop();
160+
}
163161
}
164162

165163
@Test
@@ -188,11 +186,12 @@ public void onConnected(@NotNull ActiveConnection activeConnection)
188186

189187
coolSocket.start();
190188

191-
ActiveConnection activeConnection = Connections.connect();
192-
Assert.assertEquals("The message length should be zero.", 0, activeConnection.receive().length);
193-
194-
activeConnection.close();
195-
coolSocket.stop();
189+
try (ActiveConnection activeConnection = Connections.connect()) {
190+
Assert.assertEquals("The message length should be zero.", 0,
191+
activeConnection.receive().length);
192+
} finally {
193+
coolSocket.stop();
194+
}
196195
}
197196

198197
@Test(expected = SizeUnderflowException.class)

src/test/java/org/monora/coolsocket/core/PlainTransactionTest.java

Lines changed: 32 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
import org.monora.coolsocket.core.variant.Connections;
1111
import org.monora.coolsocket.core.variant.DefaultCoolSocket;
1212
import org.monora.coolsocket.core.variant.StaticMessageCoolSocket;
13-
import org.monora.coolsocket.core.variant.factory.TestConfigFactory;
1413

1514
import java.io.IOException;
1615

@@ -25,14 +24,15 @@ public void receiveTextDataTest() throws IOException, InterruptedException
2524
coolSocket.setStaticMessage(message);
2625
coolSocket.start();
2726

28-
ActiveConnection activeConnection = Connections.connect();
29-
Response response = activeConnection.receive();
30-
31-
activeConnection.close();
32-
coolSocket.stop();
27+
try (ActiveConnection activeConnection = Connections.connect()) {
28+
Response response = activeConnection.receive();
3329

34-
Assert.assertTrue("The response should have a body.", response.containsData());
35-
Assert.assertEquals("The sent and received msg should be the same.", message, response.getAsString());
30+
Assert.assertTrue("The response should have a body.", response.containsData());
31+
Assert.assertEquals("The sent and received msg should be the same.", message,
32+
response.getAsString());
33+
} finally {
34+
coolSocket.stop();
35+
}
3636
}
3737

3838
@Test(timeout = 3000)
@@ -43,14 +43,14 @@ public void sendTextDataTest() throws IOException, InterruptedException
4343
BlockingCoolSocket coolSocket = new BlockingCoolSocket();
4444
coolSocket.start();
4545

46-
ActiveConnection activeConnection = Connections.connect();
47-
activeConnection.reply(message);
48-
49-
Response response = coolSocket.waitForResponse();
50-
Assert.assertEquals("The messages should be same", message, response.getAsString());
46+
try (ActiveConnection activeConnection = Connections.connect()) {
47+
activeConnection.reply(message);
5148

52-
activeConnection.close();
53-
coolSocket.stop();
49+
Response response = coolSocket.waitForResponse();
50+
Assert.assertEquals("The messages should be same", message, response.getAsString());
51+
} finally {
52+
coolSocket.stop();
53+
}
5454
}
5555

5656
@Test
@@ -62,13 +62,12 @@ public void receivedDataHasValidInfoTest() throws IOException, InterruptedExcept
6262
coolSocket.setStaticMessage(message);
6363
coolSocket.start();
6464

65-
ActiveConnection activeConnection = Connections.connect();
66-
Response response = activeConnection.receive();
67-
68-
activeConnection.close();
69-
coolSocket.stop();
70-
71-
Assert.assertEquals("The length should be the same.", message.length(), response.length);
65+
try (ActiveConnection activeConnection = Connections.connect()) {
66+
Response response = activeConnection.receive();
67+
Assert.assertEquals("The length should be the same.", message.length(), response.length);
68+
} finally {
69+
coolSocket.stop();
70+
}
7271
}
7372

7473
@Test
@@ -125,19 +124,18 @@ public void onConnected(@NotNull ActiveConnection activeConnection)
125124

126125
coolSocket.start();
127126

128-
ActiveConnection activeConnection = Connections.connect();
129-
130-
for (int i = 0; i < loops; i++) {
131-
activeConnection.reply(message);
132-
activeConnection.reply(message);
133-
Assert.assertEquals("The message should match with the original.", message,
134-
activeConnection.receive().getAsString());
135-
Assert.assertEquals("The message should match with the original.", message,
136-
activeConnection.receive().getAsString());
127+
try (ActiveConnection activeConnection = Connections.connect()) {
128+
for (int i = 0; i < loops; i++) {
129+
activeConnection.reply(message);
130+
activeConnection.reply(message);
131+
Assert.assertEquals("The message should match with the original.", message,
132+
activeConnection.receive().getAsString());
133+
Assert.assertEquals("The message should match with the original.", message,
134+
activeConnection.receive().getAsString());
135+
}
136+
} finally {
137+
coolSocket.stop();
137138
}
138-
139-
activeConnection.close();
140-
coolSocket.stop();
141139
}
142140

143141
@Test

src/test/java/org/monora/coolsocket/core/SafetyTest.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,19 @@
44
import org.junit.Test;
55
import org.monora.coolsocket.core.session.ActiveConnection;
66
import org.monora.coolsocket.core.session.DescriptionMismatchException;
7+
import org.monora.coolsocket.core.variant.Connections;
8+
import org.monora.coolsocket.core.variant.DefaultCoolSocket;
79

810
import java.io.IOException;
9-
import java.net.InetSocketAddress;
1011

1112
public class SafetyTest
1213
{
13-
public static final int PORT = 43244;
14-
1514
@Test(expected = DescriptionMismatchException.class)
1615
public void blockReadingFromForeignDescription() throws IOException, InterruptedException
1716
{
1817
final byte[] bytes = new byte[10];
1918

20-
CoolSocket coolSocket = new CoolSocket(PORT)
19+
CoolSocket coolSocket = new DefaultCoolSocket()
2120
{
2221
@Override
2322
public void onConnected(@NotNull ActiveConnection activeConnection)
@@ -35,7 +34,7 @@ public void onConnected(@NotNull ActiveConnection activeConnection)
3534

3635
coolSocket.start();
3736

38-
try (ActiveConnection activeConnection = ActiveConnection.connect(new InetSocketAddress(PORT))) {
37+
try (ActiveConnection activeConnection = Connections.connect()) {
3938
activeConnection.readBegin();
4039
ActiveConnection.Description description2 = activeConnection.readBegin();
4140

src/test/java/org/monora/coolsocket/core/variant/factory/TestConfigFactory.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,15 @@
77

88
public class TestConfigFactory extends DefaultConfigFactory
99
{
10+
public static final String SOCKET_ADDRESS_HOST = "127.0.0.1";
11+
1012
public static final int SOCKET_PORT = 56323;
1113

12-
public static final SocketAddress SOCKET_ADDRESS = new InetSocketAddress(SOCKET_PORT);
14+
public static final SocketAddress SOCKET_ADDRESS = new InetSocketAddress(SOCKET_ADDRESS_HOST, SOCKET_PORT);
1315

14-
public static final int TIMEOUT_ACCEPT = 2000;
16+
public static final int TIMEOUT_ACCEPT = 5000;
1517

16-
public static final int TIMEOUT_READ = 2000;
18+
public static final int TIMEOUT_READ = 5000;
1719

1820
public TestConfigFactory()
1921
{

0 commit comments

Comments
 (0)