Skip to content

Commit 58d9815

Browse files
committed
more tests
1 parent 8147ed0 commit 58d9815

File tree

2 files changed

+87
-21
lines changed

2 files changed

+87
-21
lines changed

sync-proxy/src/org/modelingvalue/syncproxy/Main.java

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
import java.util.stream.*;
2222

2323
public class Main {
24+
private static final boolean VERBOSE = Boolean.getBoolean("VERBOSE");
25+
2426
private static int connectionNumber;
2527

2628
public static void main(String[] args) {
@@ -44,7 +46,9 @@ public Main(int port) throws IOException {
4446
listenSocket = new ServerSocket(port);
4547
this.port = listenSocket.getLocalPort();
4648
listenThread = new Thread(() -> {
47-
System.err.println("listening for clients on port " + this.port + "...");
49+
if (VERBOSE) {
50+
System.err.println("listening for clients on port " + this.port + "...");
51+
}
4852
while (!listenSocket.isClosed()) {
4953
try {
5054
addClient(listenSocket.accept());
@@ -54,7 +58,9 @@ public Main(int port) throws IOException {
5458
}
5559
}
5660
}
57-
System.err.println("stop listening for clients on port " + this.port);
61+
if (VERBOSE) {
62+
System.err.println("stop listening for clients on port " + this.port);
63+
}
5864
}, "SyncProxy-" + this.port);
5965
listenThread.start();
6066
}
@@ -64,13 +70,17 @@ public int getPort() {
6470
}
6571

6672
private synchronized void addClient(Socket sock) throws IOException {
67-
System.err.println("new client: " + sock + " (" + connectionSet.size() + " clients now)");
73+
if (VERBOSE) {
74+
System.err.println("new client: " + sock + " (" + connectionSet.size() + " clients now)");
75+
}
6876
connectionSet.add(new SockReader(sock, connectionNumber++));
6977
}
7078

7179
private synchronized void removeClient(SockReader sr) {
7280
if (connectionSet.remove(sr)) {
73-
System.err.println("lost client: " + sr.sock + " (" + connectionSet.size() + " clients now)");
81+
if (VERBOSE) {
82+
System.err.println("lost client: " + sr.sock + " (" + connectionSet.size() + " clients now)");
83+
}
7484
}
7585
}
7686

sync-proxy/tst/org/modelingvalue/syncproxy/MainTest.java

Lines changed: 73 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@ void checkThreads() throws IOException, InterruptedException {
3535
TestClient c1 = new TestClient(actualPort);
3636

3737
c0.writeLine("haystack1");
38-
assertEquals("haystack1", c1.readline());
38+
assertEquals("haystack1", c1.readLine());
3939

4040
c1.writeLine("haystack2");
41-
assertEquals("haystack2", c0.readline());
41+
assertEquals("haystack2", c0.readLine());
4242

4343
assertEquals(5, getThreadCount(initialThreads));
4444

@@ -61,18 +61,18 @@ void twoClientsA() throws IOException, InterruptedException {
6161
TestClient c1 = new TestClient(actualPort);
6262

6363
c0.writeLine("haystack1");
64-
assertEquals("haystack1", c1.readline());
64+
assertEquals("haystack1", c1.readLine());
6565

6666
c1.writeLine("haystack2");
67-
assertEquals("haystack2", c0.readline());
67+
assertEquals("haystack2", c0.readLine());
6868

6969
main.close();
7070

7171
c0.writeLine("closed");
72-
assertNull(c1.readline());
72+
assertNull(c1.readNull());
7373

7474
c1.writeLine("closed");
75-
assertNull(c0.readline());
75+
assertNull(c0.readNull());
7676

7777
c0.interrupt();
7878
c1.interrupt();
@@ -87,20 +87,60 @@ void twoClientsB() throws IOException, InterruptedException {
8787

8888
c0.writeLine("haystack1");
8989
c1.writeLine("haystack2");
90-
assertEquals("haystack1", c1.readline());
91-
assertEquals("haystack2", c0.readline());
90+
assertEquals("haystack1", c1.readLine());
91+
assertEquals("haystack2", c0.readLine());
9292

9393
main.close();
9494

9595
c0.writeLine("closed");
9696
c1.writeLine("closed");
97-
assertNull(c1.readline());
98-
assertNull(c0.readline());
97+
assertNull(c1.readNull());
98+
assertNull(c0.readNull());
9999

100100
c0.interrupt();
101101
c1.interrupt();
102102
}
103103

104+
@RepeatedTest(1)
105+
void longString() throws IOException, InterruptedException {
106+
Main main = new Main(0);
107+
int actualPort = main.getPort();
108+
TestClient c0 = new TestClient(actualPort);
109+
TestClient c1 = new TestClient(actualPort);
110+
111+
String s0 = longRandomString();
112+
String s1 = longRandomString();
113+
c0.writeLine(s0);
114+
c1.writeLine(s1);
115+
assertEquals(s0, c1.readLine());
116+
assertEquals(s1, c0.readLine());
117+
118+
main.close();
119+
c0.interrupt();
120+
c1.interrupt();
121+
}
122+
123+
@RepeatedTest(1)
124+
void manyStrings() throws IOException, InterruptedException {
125+
Main main = new Main(0);
126+
int actualPort = main.getPort();
127+
TestClient c0 = new TestClient(actualPort);
128+
TestClient c1 = new TestClient(actualPort);
129+
130+
for (int i = 0; i < 1000; i++) {
131+
String s0 = mediumRandomString();
132+
String s1 = mediumRandomString();
133+
c0.writeLine(s0);
134+
c1.writeLine(s1);
135+
assertEquals(s0, c1.readLine());
136+
assertEquals(s1, c0.readLine());
137+
}
138+
139+
main.close();
140+
c0.interrupt();
141+
c1.interrupt();
142+
}
143+
104144
@RepeatedTest(1)
105145
void threeClients() throws IOException, InterruptedException {
106146
Main main = new Main(0);
@@ -110,23 +150,35 @@ void threeClients() throws IOException, InterruptedException {
110150
TestClient c2 = new TestClient(actualPort);
111151

112152
c0.writeLine("haystack1");
113-
assertEquals("haystack1", c1.readline());
114-
assertEquals("haystack1", c2.readline());
153+
assertEquals("haystack1", c1.readLine());
154+
assertEquals("haystack1", c2.readLine());
115155

116156
c1.writeLine("haystack2");
117-
assertEquals("haystack2", c0.readline());
118-
assertEquals("haystack2", c2.readline());
157+
assertEquals("haystack2", c0.readLine());
158+
assertEquals("haystack2", c2.readLine());
119159

120160
c2.writeLine("haystack3");
121-
assertEquals("haystack3", c0.readline());
122-
assertEquals("haystack3", c1.readline());
161+
assertEquals("haystack3", c0.readLine());
162+
assertEquals("haystack3", c1.readLine());
123163

124164
main.close();
125165
c0.interrupt();
126166
c1.interrupt();
127167
c2.interrupt();
128168
}
129169

170+
private static String mediumRandomString() {
171+
byte[] bytes = new byte[1024];
172+
new Random().nextBytes(bytes);
173+
return Arrays.toString(bytes);
174+
}
175+
176+
private static String longRandomString() {
177+
byte[] bytes = new byte[1024 * 1024];
178+
new Random().nextBytes(bytes);
179+
return Arrays.toString(bytes);
180+
}
181+
130182
private static class TestClient extends WorkDaemon<String> {
131183
private final Socket sock;
132184
private final BufferedReader in;
@@ -167,8 +219,12 @@ public void writeLine(String line) {
167219
}
168220
}
169221

170-
public String readline() throws InterruptedException {
222+
public String readNull() throws InterruptedException {
171223
return sock.isClosed() || !sock.isConnected() ? null : lineQueue.poll(50, TimeUnit.MILLISECONDS);
172224
}
225+
226+
public String readLine() throws InterruptedException {
227+
return sock.isClosed() || !sock.isConnected() ? null : lineQueue.poll(1000, TimeUnit.MILLISECONDS);
228+
}
173229
}
174230
}

0 commit comments

Comments
 (0)