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 assertFalse(s.isClosed());
273 }
274 });
275 }
276
277 /**
278 * Virtual thread interrupted while blocked in Socket read.
279 */
280 @Test
281 void testSocketReadInterrupt1() throws Exception {
282 testSocketReadInterrupt(0);
283 }
284
285 /**
286 * Virtual thread interrupted while blocked in Socket read with timeout
287 */
288 @Test
289 void testSocketReadInterrupt2() throws Exception {
290 testSocketReadInterrupt(60_000);
291 }
292
293 void testSocketReadInterrupt(int timeout) throws Exception {
294 VThreadRunner.run(() -> {
295 try (var connection = new Connection()) {
296 Socket s = connection.socket1();
307 try {
308 int n = s.getInputStream().read();
309 fail("read " + n);
310 } catch (SocketException expected) {
311 assertTrue(Thread.interrupted());
312 assertTrue(s.isClosed());
313 }
314 }
315 });
316 }
317
318 /**
319 * Socket close while virtual thread blocked in write.
320 */
321 @Test
322 void testSocketWriteAsyncClose() throws Exception {
323 VThreadRunner.run(() -> {
324 try (var connection = new Connection()) {
325 Socket s = connection.socket1();
326
327 // delayed close of s
328 runAfterParkedAsync(s::close);
329
330 // write to s should block, then throw
331 try {
332 byte[] ba = new byte[100*1024];
333 OutputStream out = s.getOutputStream();
334 for (;;) {
335 out.write(ba);
336 }
337 } catch (SocketException expected) { }
338 }
339 });
340 }
341
342 /**
343 * Socket shutdownOutput while virtual thread blocked in write.
344 */
345 @Test
346 void testSocketWriteAsyncShutdownOutput() throws Exception {
347 VThreadRunner.run(() -> {
348 try (var connection = new Connection()) {
349 Socket s = connection.socket1();
350
351 // delayed shutdown of s
352 runAfterParkedAsync(s::shutdownOutput);
353
354 // write to s should block, then throw
355 try {
356 byte[] ba = new byte[100*1024];
357 OutputStream out = s.getOutputStream();
358 for (;;) {
359 out.write(ba);
360 }
361 } catch (SocketException expected) { }
362 assertFalse(s.isClosed());
363 }
364 });
365 }
366
367 /**
368 * Virtual thread interrupted while blocked in Socket write.
369 */
370 @Test
371 void testSocketWriteInterrupt() throws Exception {
372 VThreadRunner.run(() -> {
373 try (var connection = new Connection()) {
374 Socket s = connection.socket1();
375
376 // delayed interrupt of current thread
377 Thread thisThread = Thread.currentThread();
378 runAfterParkedAsync(thisThread::interrupt);
379
380 // write to s should block, then throw
381 try {
382 byte[] ba = new byte[100*1024];
383 OutputStream out = s.getOutputStream();
384 for (;;) {
385 out.write(ba);
386 }
|