Skip to content

Commit 606d501

Browse files
committed
Add JUnit testing, move on to 1.0.1
1 parent c23657e commit 606d501

File tree

3 files changed

+142
-3
lines changed

3 files changed

+142
-3
lines changed

pom.xml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>com.genonbeta.coolsocket</groupId>
88
<artifactId>main</artifactId>
9-
<version>1.0</version>
9+
<version>1.0.1</version>
1010

1111
<repositories>
1212
<repository>
@@ -26,6 +26,12 @@
2626
<artifactId>json</artifactId>
2727
<version>20180130</version>
2828
</dependency>
29+
<dependency>
30+
<groupId>junit</groupId>
31+
<artifactId>junit</artifactId>
32+
<version>4.8.1</version>
33+
<scope>test</scope>
34+
</dependency>
2935
</dependencies>
3036

3137
<build>

src/main/java/com/genonbeta/CoolSocket/CoolSocket.java

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,8 @@ public boolean isInterrupted()
145145

146146
public boolean isServerAlive()
147147
{
148-
return this.getServerThread().isAlive();
148+
return this.getServerThread() != null
149+
&& this.getServerThread().isAlive();
149150
}
150151

151152
protected boolean respondRequest(final Socket socket)
@@ -236,16 +237,35 @@ public boolean start()
236237
return true;
237238
}
238239

240+
// ensures the server, if was stopping, has stopped
239241
public boolean startDelayed(int timeout)
240242
{
241243
long startTime = System.currentTimeMillis();
242244

243-
while (this.isServerAlive() && (System.currentTimeMillis() - startTime) < timeout) {
245+
while (this.isServerAlive()) {
246+
if ((System.currentTimeMillis() - startTime) > timeout)
247+
// We did not request start but it was already running, so it was rather not
248+
// requested to stop or the server blocked itself and does not respond
249+
return false;
244250
}
245251

246252
return this.start();
247253
}
248254

255+
// ensures the server is started otherwise returns false
256+
public boolean startEnsured(int timeout) {
257+
long startTime = System.currentTimeMillis();
258+
259+
if (!this.startDelayed(timeout))
260+
return false;
261+
262+
while (!this.isServerAlive())
263+
if ((System.currentTimeMillis() - startTime) > timeout)
264+
return false;
265+
266+
return true;
267+
}
268+
249269
public boolean stop()
250270
{
251271
if (this.isInterrupted())
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
package com.genonbeta.CoolSocket;
2+
3+
import org.junit.Test;
4+
5+
import java.io.IOException;
6+
import java.net.InetSocketAddress;
7+
import java.util.concurrent.TimeoutException;
8+
9+
public class Main
10+
{
11+
public static final int PORT_SERVER = 53535;
12+
13+
@Test
14+
public void main()
15+
{
16+
final TestServer testServer = new TestServer();
17+
18+
log("Server started ?= %s", testServer.startEnsured(5000));
19+
20+
CoolSocket.connect(client -> {
21+
if (!testServer.isServerAlive())
22+
return;
23+
24+
try {
25+
CoolSocket.ActiveConnection activeConnection = client.connect(new InetSocketAddress(PORT_SERVER), CoolSocket.NO_TIMEOUT);
26+
27+
activeConnection.reply("Hi server");
28+
29+
CoolSocket.ActiveConnection.Response response = activeConnection.receive();
30+
31+
log("Server says: %s; with total character of: %d; header of: %s",
32+
response.response,
33+
response.totalLength,
34+
response.headerIndex.toString());
35+
36+
activeConnection.getSocket().close();
37+
} catch (IOException | TimeoutException e) {
38+
e.printStackTrace();
39+
}
40+
});
41+
42+
while (testServer.isServerAlive()) {
43+
// the server has not shut down
44+
}
45+
46+
log("Program exited");
47+
}
48+
49+
public static class TestServer extends CoolSocket
50+
{
51+
private int connectionRemains = 10;
52+
53+
public TestServer()
54+
{
55+
super(PORT_SERVER);
56+
}
57+
58+
@Override
59+
public void onServerStarted()
60+
{
61+
super.onServerStarted();
62+
log(String.format("Server started on port %d", getLocalPort()));
63+
}
64+
65+
@Override
66+
public void onServerStopped()
67+
{
68+
super.onServerStopped();
69+
log("Server stopped");
70+
}
71+
72+
@Override
73+
protected void onConnected(ActiveConnection activeConnection)
74+
{
75+
connectionRemains--;
76+
77+
try {
78+
ActiveConnection.Response response = activeConnection.receive();
79+
80+
log("Client says: %s; with total character of: %d; header of: %s",
81+
response.response,
82+
response.totalLength,
83+
response.headerIndex.toString());
84+
85+
activeConnection.reply(String.format("Thanks, remaining server shutdown step is %s", connectionRemains));
86+
87+
activeConnection.getSocket().close();
88+
} catch (IOException | TimeoutException e) {
89+
e.printStackTrace();
90+
}
91+
92+
if (connectionRemains == 0)
93+
stop();
94+
}
95+
}
96+
97+
public static void log(String print)
98+
{
99+
log(print, (Object[]) null);
100+
}
101+
102+
public static void log(String print, Object... formatted)
103+
{
104+
StringBuilder stringBuilder = new StringBuilder()
105+
.append(Main.class.getName())
106+
.append("(): ")
107+
.append(print);
108+
109+
System.out.println(print == null
110+
? stringBuilder.toString()
111+
: String.format(stringBuilder.toString(), formatted));
112+
}
113+
}

0 commit comments

Comments
 (0)