@@ -35,10 +35,10 @@ void checkThreads() throws IOException, InterruptedException {
35
35
TestClient c1 = new TestClient (actualPort );
36
36
37
37
c0 .writeLine ("haystack1" );
38
- assertEquals ("haystack1" , c1 .readline ());
38
+ assertEquals ("haystack1" , c1 .readLine ());
39
39
40
40
c1 .writeLine ("haystack2" );
41
- assertEquals ("haystack2" , c0 .readline ());
41
+ assertEquals ("haystack2" , c0 .readLine ());
42
42
43
43
assertEquals (5 , getThreadCount (initialThreads ));
44
44
@@ -61,18 +61,18 @@ void twoClientsA() throws IOException, InterruptedException {
61
61
TestClient c1 = new TestClient (actualPort );
62
62
63
63
c0 .writeLine ("haystack1" );
64
- assertEquals ("haystack1" , c1 .readline ());
64
+ assertEquals ("haystack1" , c1 .readLine ());
65
65
66
66
c1 .writeLine ("haystack2" );
67
- assertEquals ("haystack2" , c0 .readline ());
67
+ assertEquals ("haystack2" , c0 .readLine ());
68
68
69
69
main .close ();
70
70
71
71
c0 .writeLine ("closed" );
72
- assertNull (c1 .readline ());
72
+ assertNull (c1 .readNull ());
73
73
74
74
c1 .writeLine ("closed" );
75
- assertNull (c0 .readline ());
75
+ assertNull (c0 .readNull ());
76
76
77
77
c0 .interrupt ();
78
78
c1 .interrupt ();
@@ -87,20 +87,60 @@ void twoClientsB() throws IOException, InterruptedException {
87
87
88
88
c0 .writeLine ("haystack1" );
89
89
c1 .writeLine ("haystack2" );
90
- assertEquals ("haystack1" , c1 .readline ());
91
- assertEquals ("haystack2" , c0 .readline ());
90
+ assertEquals ("haystack1" , c1 .readLine ());
91
+ assertEquals ("haystack2" , c0 .readLine ());
92
92
93
93
main .close ();
94
94
95
95
c0 .writeLine ("closed" );
96
96
c1 .writeLine ("closed" );
97
- assertNull (c1 .readline ());
98
- assertNull (c0 .readline ());
97
+ assertNull (c1 .readNull ());
98
+ assertNull (c0 .readNull ());
99
99
100
100
c0 .interrupt ();
101
101
c1 .interrupt ();
102
102
}
103
103
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
+
104
144
@ RepeatedTest (1 )
105
145
void threeClients () throws IOException , InterruptedException {
106
146
Main main = new Main (0 );
@@ -110,23 +150,35 @@ void threeClients() throws IOException, InterruptedException {
110
150
TestClient c2 = new TestClient (actualPort );
111
151
112
152
c0 .writeLine ("haystack1" );
113
- assertEquals ("haystack1" , c1 .readline ());
114
- assertEquals ("haystack1" , c2 .readline ());
153
+ assertEquals ("haystack1" , c1 .readLine ());
154
+ assertEquals ("haystack1" , c2 .readLine ());
115
155
116
156
c1 .writeLine ("haystack2" );
117
- assertEquals ("haystack2" , c0 .readline ());
118
- assertEquals ("haystack2" , c2 .readline ());
157
+ assertEquals ("haystack2" , c0 .readLine ());
158
+ assertEquals ("haystack2" , c2 .readLine ());
119
159
120
160
c2 .writeLine ("haystack3" );
121
- assertEquals ("haystack3" , c0 .readline ());
122
- assertEquals ("haystack3" , c1 .readline ());
161
+ assertEquals ("haystack3" , c0 .readLine ());
162
+ assertEquals ("haystack3" , c1 .readLine ());
123
163
124
164
main .close ();
125
165
c0 .interrupt ();
126
166
c1 .interrupt ();
127
167
c2 .interrupt ();
128
168
}
129
169
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
+
130
182
private static class TestClient extends WorkDaemon <String > {
131
183
private final Socket sock ;
132
184
private final BufferedReader in ;
@@ -167,8 +219,12 @@ public void writeLine(String line) {
167
219
}
168
220
}
169
221
170
- public String readline () throws InterruptedException {
222
+ public String readNull () throws InterruptedException {
171
223
return sock .isClosed () || !sock .isConnected () ? null : lineQueue .poll (50 , TimeUnit .MILLISECONDS );
172
224
}
225
+
226
+ public String readLine () throws InterruptedException {
227
+ return sock .isClosed () || !sock .isConnected () ? null : lineQueue .poll (1000 , TimeUnit .MILLISECONDS );
228
+ }
173
229
}
174
230
}
0 commit comments