4040import java .nio .channels .SocketChannel ;
4141import java .util .Arrays ;
4242import java .util .Objects ;
43+ import java .util .concurrent .Executors ;
4344import java .util .concurrent .ThreadFactory ;
4445import java .util .concurrent .atomic .AtomicBoolean ;
4546import java .util .stream .Stream ;
5051
5152class PeerReadsAfterAsyncClose {
5253
54+ static Stream <ThreadFactory > factories () {
55+ return Stream .of (Executors .defaultThreadFactory ());
56+ }
57+
5358 /**
5459 * Close SocketChannel while a thread is blocked reading from the channel's socket.
5560 */
56- void testCloseDuringSocketChannelRead () throws Exception {
61+ @ ParameterizedTest
62+ @ MethodSource ("factories" )
63+ void testCloseDuringSocketChannelRead (ThreadFactory factory ) throws Exception {
5764 var loopback = InetAddress .getLoopbackAddress ();
5865 try (var listener = new ServerSocket ()) {
5966 listener .bind (new InetSocketAddress (loopback , 0 ));
@@ -63,7 +70,7 @@ void testCloseDuringSocketChannelRead() throws Exception {
6370
6471 // start thread to read from channel
6572 var cceThrown = new AtomicBoolean ();
66- Thread thread = new Thread (() -> {
73+ Thread thread = factory . newThread (() -> {
6774 try {
6875 sc .read (ByteBuffer .allocate (1 ));
6976 fail ();
@@ -96,18 +103,22 @@ void testCloseDuringSocketChannelRead() throws Exception {
96103 /**
97104 * Close Socket while a thread is blocked reading from the socket.
98105 */
99- void testCloseDuringSocketUntimedRead () throws Exception {
100- testCloseDuringSocketRead (0 );
106+ @ ParameterizedTest
107+ @ MethodSource ("factories" )
108+ void testCloseDuringSocketUntimedRead (ThreadFactory factory ) throws Exception {
109+ testCloseDuringSocketRead (factory , 0 );
101110 }
102111
103112 /**
104113 * Close Socket while a thread is blocked reading from the socket with a timeout.
105114 */
106- void testCloseDuringSockeTimedRead () throws Exception {
107- testCloseDuringSocketRead (60_000 );
115+ @ ParameterizedTest
116+ @ MethodSource ("factories" )
117+ void testCloseDuringSockeTimedRead (ThreadFactory factory ) throws Exception {
118+ testCloseDuringSocketRead (factory , 60_000 );
108119 }
109120
110- private void testCloseDuringSocketRead (int timeout ) throws Exception {
121+ private void testCloseDuringSocketRead (ThreadFactory factory , int timeout ) throws Exception {
111122 var loopback = InetAddress .getLoopbackAddress ();
112123 try (var listener = new ServerSocket ()) {
113124 listener .bind (new InetSocketAddress (loopback , 0 ));
@@ -117,7 +128,7 @@ private void testCloseDuringSocketRead(int timeout) throws Exception {
117128
118129 // start thread to read from socket
119130 var seThrown = new AtomicBoolean ();
120- Thread thread = new Thread (() -> {
131+ Thread thread = factory . newThread (() -> {
121132 try {
122133 s .setSoTimeout (timeout );
123134 s .getInputStream ().read ();
@@ -157,7 +168,7 @@ private void onReach(Thread target, String location, Runnable action) {
157168 int index = location .lastIndexOf ('.' );
158169 String className = location .substring (0 , index );
159170 String methodName = location .substring (index + 1 );
160- new Thread (() -> {
171+ Thread tDaemon = new Thread (() -> {
161172 try {
162173 boolean found = false ;
163174 while (!found ) {
@@ -170,7 +181,9 @@ private void onReach(Thread target, String location, Runnable action) {
170181 } catch (Exception e ) {
171182 e .printStackTrace ();
172183 }
173- }).start ();
184+ });
185+ tDaemon .setDaemon (true );
186+ tDaemon .start ();
174187 }
175188
176189 /**
0 commit comments