195 } catch (IOException ioe) {
196 // expected
197 }
198 }
199 });
200 }
201
202 /**
203 * Socket close while virtual thread blocked in read.
204 */
205 @Test
206 void testSocketReadAsyncClose1() throws Exception {
207 testSocketReadAsyncClose(0);
208 }
209
210 /**
211 * Socket close while virtual thread blocked in timed read.
212 */
213 @Test
214 void testSocketReadAsyncClose2() throws Exception {
215 testSocketReadAsyncClose(0);
216 }
217
218 void testSocketReadAsyncClose(int timeout) throws Exception {
219 VThreadRunner.run(() -> {
220 try (var connection = new Connection()) {
221 Socket s = connection.socket1();
222
223 // delayed close of s
224 runAfterParkedAsync(s::close);
225
226 // read from s should block, then throw
227 if (timeout > 0) {
228 s.setSoTimeout(timeout);
229 }
230 try {
231 int n = s.getInputStream().read();
232 fail("read " + n);
233 } catch (SocketException expected) { }
234 }
235 });
236 }
237
238 /**
239 * Virtual thread interrupted while blocked in Socket read.
240 */
241 @Test
242 void testSocketReadInterrupt1() throws Exception {
243 testSocketReadInterrupt(0);
244 }
245
246 /**
247 * Virtual thread interrupted while blocked in Socket read with timeout
248 */
249 @Test
250 void testSocketReadInterrupt2() throws Exception {
251 testSocketReadInterrupt(60_000);
252 }
253
254 void testSocketReadInterrupt(int timeout) throws Exception {
255 VThreadRunner.run(() -> {
256 try (var connection = new Connection()) {
257 Socket s = connection.socket1();
268 try {
269 int n = s.getInputStream().read();
270 fail("read " + n);
271 } catch (SocketException expected) {
272 assertTrue(Thread.interrupted());
273 assertTrue(s.isClosed());
274 }
275 }
276 });
277 }
278
279 /**
280 * Socket close while virtual thread blocked in write.
281 */
282 @Test
283 void testSocketWriteAsyncClose() throws Exception {
284 VThreadRunner.run(() -> {
285 try (var connection = new Connection()) {
286 Socket s = connection.socket1();
287
288 // delayedclose of s
289 runAfterParkedAsync(s::close);
290
291 // write to s should block, then throw
292 try {
293 byte[] ba = new byte[100*1024];
294 OutputStream out = s.getOutputStream();
295 for (;;) {
296 out.write(ba);
297 }
298 } catch (SocketException expected) { }
299 }
300 });
301 }
302
303 /**
304 * Virtual thread interrupted while blocked in Socket write.
305 */
306 @Test
307 void testSocketWriteInterrupt() throws Exception {
308 VThreadRunner.run(() -> {
309 try (var connection = new Connection()) {
310 Socket s = connection.socket1();
311
312 // delayed interrupt of current thread
313 Thread thisThread = Thread.currentThread();
314 runAfterParkedAsync(thisThread::interrupt);
315
316 // write to s should block, then throw
317 try {
318 byte[] ba = new byte[100*1024];
319 OutputStream out = s.getOutputStream();
320 for (;;) {
321 out.write(ba);
322 }
|
195 } catch (IOException ioe) {
196 // expected
197 }
198 }
199 });
200 }
201
202 /**
203 * Socket close while virtual thread blocked in read.
204 */
205 @Test
206 void testSocketReadAsyncClose1() throws Exception {
207 testSocketReadAsyncClose(0);
208 }
209
210 /**
211 * Socket close while virtual thread blocked in timed read.
212 */
213 @Test
214 void testSocketReadAsyncClose2() throws Exception {
215 testSocketReadAsyncClose(60_000);
216 }
217
218 void testSocketReadAsyncClose(int timeout) throws Exception {
219 VThreadRunner.run(() -> {
220 try (var connection = new Connection()) {
221 Socket s = connection.socket1();
222
223 // delayed close of s
224 runAfterParkedAsync(s::close);
225
226 // read from s should block, then throw
227 if (timeout > 0) {
228 s.setSoTimeout(timeout);
229 }
230 try {
231 int n = s.getInputStream().read();
232 fail("read " + n);
233 } catch (SocketException expected) { }
234 }
235 });
236 }
237
238 /**
239 * Socket shutdownInput while virtual thread blocked in read.
240 */
241 @Test
242 void testSocketReadAsyncShutdownInput1() throws Exception {
243 testSocketReadAsyncShutdownInput(0);
244 }
245
246 /**
247 * Socket shutdownInput while virtual thread blocked in timed read.
248 */
249 @Test
250 void testSocketReadAsyncShutdownInput2() throws Exception {
251 testSocketReadAsyncShutdownInput(60_000);
252 }
253
254 void testSocketReadAsyncShutdownInput(int timeout) throws Exception {
255 VThreadRunner.run(() -> {
256 try (var connection = new Connection()) {
257 Socket s = connection.socket1();
258
259 // delayed shutdown of s
260 runAfterParkedAsync(s::shutdownInput);
261
262 // read from s should block, then throw
263 if (timeout > 0) {
264 s.setSoTimeout(timeout);
265 }
266
267 // -1 or SocketException
268 try {
269 int n = s.getInputStream().read();
270 assertEquals(-1, n);
271 } catch (SocketException e) { }
272
273 assertTrue(s.isInputShutdown());
274 assertFalse(s.isClosed());
275 }
276 });
277 }
278
279 /**
280 * Virtual thread interrupted while blocked in Socket read.
281 */
282 @Test
283 void testSocketReadInterrupt1() throws Exception {
284 testSocketReadInterrupt(0);
285 }
286
287 /**
288 * Virtual thread interrupted while blocked in Socket read with timeout
289 */
290 @Test
291 void testSocketReadInterrupt2() throws Exception {
292 testSocketReadInterrupt(60_000);
293 }
294
295 void testSocketReadInterrupt(int timeout) throws Exception {
296 VThreadRunner.run(() -> {
297 try (var connection = new Connection()) {
298 Socket s = connection.socket1();
309 try {
310 int n = s.getInputStream().read();
311 fail("read " + n);
312 } catch (SocketException expected) {
313 assertTrue(Thread.interrupted());
314 assertTrue(s.isClosed());
315 }
316 }
317 });
318 }
319
320 /**
321 * Socket close while virtual thread blocked in write.
322 */
323 @Test
324 void testSocketWriteAsyncClose() throws Exception {
325 VThreadRunner.run(() -> {
326 try (var connection = new Connection()) {
327 Socket s = connection.socket1();
328
329 // delayed close of s
330 runAfterParkedAsync(s::close);
331
332 // write to s should block, then throw
333 try {
334 byte[] ba = new byte[100*1024];
335 OutputStream out = s.getOutputStream();
336 for (;;) {
337 out.write(ba);
338 }
339 } catch (SocketException expected) { }
340 }
341 });
342 }
343
344 /**
345 * Socket shutdownOutput while virtual thread blocked in write.
346 */
347 @Test
348 void testSocketWriteAsyncShutdownOutput() throws Exception {
349 VThreadRunner.run(() -> {
350 try (var connection = new Connection()) {
351 Socket s = connection.socket1();
352
353 // delayed shutdown of s
354 runAfterParkedAsync(s::shutdownOutput);
355
356 // write to s should block, then throw
357 try {
358 byte[] ba = new byte[100*1024];
359 OutputStream out = s.getOutputStream();
360 for (;;) {
361 out.write(ba);
362 }
363 } catch (SocketException expected) { }
364
365 assertTrue(s.isOutputShutdown());
366 assertFalse(s.isClosed());
367 }
368 });
369 }
370
371 /**
372 * Virtual thread interrupted while blocked in Socket write.
373 */
374 @Test
375 void testSocketWriteInterrupt() throws Exception {
376 VThreadRunner.run(() -> {
377 try (var connection = new Connection()) {
378 Socket s = connection.socket1();
379
380 // delayed interrupt of current thread
381 Thread thisThread = Thread.currentThread();
382 runAfterParkedAsync(thisThread::interrupt);
383
384 // write to s should block, then throw
385 try {
386 byte[] ba = new byte[100*1024];
387 OutputStream out = s.getOutputStream();
388 for (;;) {
389 out.write(ba);
390 }
|