From 3e32b934ece7bc93ada2bc754342aff1db04c48a Mon Sep 17 00:00:00 2001 From: SendaoYan Date: Mon, 25 Nov 2024 14:56:58 +0000 Subject: [PATCH] 8343877: Test AsyncClose.java intermittent fails - Socket.getInputStream().read() wasn't preempted Backport-of: 752e1629555f0ec8630373ec87b049afdd709ea6 --- .../Socket_getInputStream_read.java | 32 ++++++++++--------- .../Socket_getOutputStream_write.java | 30 +++++++++-------- 2 files changed, 33 insertions(+), 29 deletions(-) diff --git a/test/jdk/java/net/Socket/asyncClose/Socket_getInputStream_read.java b/test/jdk/java/net/Socket/asyncClose/Socket_getInputStream_read.java index f2082fcf374..812541c9daa 100644 --- a/test/jdk/java/net/Socket/asyncClose/Socket_getInputStream_read.java +++ b/test/jdk/java/net/Socket/asyncClose/Socket_getInputStream_read.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2001, 2019, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2001, 2024, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -60,7 +60,7 @@ public void run() { } latch.countDown(); int n = in.read(); - failed("Socket.getInputStream().read() returned unexpectedly!!"); + failed("Socket.getInputStream().read() returned unexpectedly, n=" + n); } catch (SocketException se) { if (latch.getCount() != 1) { closed(); @@ -77,20 +77,22 @@ public void run() { public AsyncCloseTest go() { try { InetAddress lh = InetAddress.getLocalHost(); - ServerSocket ss = new ServerSocket(0, 0, lh); - s.connect( new InetSocketAddress(lh, ss.getLocalPort()) ); - Socket s2 = ss.accept(); - Thread thr = new Thread(this); - thr.start(); - latch.await(); - Thread.sleep(5000); //sleep, so Socket.getInputStream().read() can block - s.close(); - thr.join(); + try (ServerSocket ss = new ServerSocket(0, 0, lh)) { + s.connect(new InetSocketAddress(lh, ss.getLocalPort())); + try (Socket s2 = ss.accept()) { + Thread thr = new Thread(this); + thr.start(); + latch.await(); + Thread.sleep(5000); //sleep, so Socket.getInputStream().read() can block + s.close(); + thr.join(); + } - if (isClosed()) { - return passed(); - } else { - return failed("Socket.getInputStream().read() wasn't preempted"); + if (isClosed()) { + return passed(); + } else { + return failed("Socket.getInputStream().read() wasn't preempted"); + } } } catch (Exception x) { failed(x.getMessage()); diff --git a/test/jdk/java/net/Socket/asyncClose/Socket_getOutputStream_write.java b/test/jdk/java/net/Socket/asyncClose/Socket_getOutputStream_write.java index dc5eda06078..6a967ebcf8d 100644 --- a/test/jdk/java/net/Socket/asyncClose/Socket_getOutputStream_write.java +++ b/test/jdk/java/net/Socket/asyncClose/Socket_getOutputStream_write.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2001, 2019, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2001, 2024, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -66,20 +66,22 @@ public void run() { public AsyncCloseTest go() { try { InetAddress lh = InetAddress.getLocalHost(); - ServerSocket ss = new ServerSocket(0, 0, lh); - s.connect( new InetSocketAddress(lh, ss.getLocalPort()) ); - Socket s2 = ss.accept(); - Thread thr = new Thread(this); - thr.start(); - latch.await(); - Thread.sleep(1000); - s.close(); - thr.join(); + try (ServerSocket ss = new ServerSocket(0, 0, lh)) { + s.connect(new InetSocketAddress(lh, ss.getLocalPort())); + try (Socket s2 = ss.accept()) { + Thread thr = new Thread(this); + thr.start(); + latch.await(); + Thread.sleep(1000); + s.close(); + thr.join(); + } - if (isClosed()) { - return passed(); - } else { - return failed("Socket.getOutputStream().write() wasn't preempted"); + if (isClosed()) { + return passed(); + } else { + return failed("Socket.getOutputStream().write() wasn't preempted"); + } } } catch (Exception x) { failed(x.getMessage());