Skip to content

Commit 1664e79

Browse files
author
Darragh Clarke
committed
8311792: java/net/httpclient/ResponsePublisher.java fails intermittently with AssertionError: Found some outstanding operations
Reviewed-by: dfuchs, jpai
1 parent 0901d75 commit 1664e79

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed

test/jdk/java/net/httpclient/ResponsePublisher.java

+58
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import com.sun.net.httpserver.HttpServer;
3737
import com.sun.net.httpserver.HttpsConfigurator;
3838
import com.sun.net.httpserver.HttpsServer;
39+
import jdk.internal.net.http.common.OperationTrackers;
3940
import jdk.test.lib.net.SimpleSSLContext;
4041
import org.testng.annotations.AfterTest;
4142
import org.testng.annotations.BeforeTest;
@@ -100,6 +101,16 @@ public class ResponsePublisher implements HttpServerAdapters {
100101
// a shared executor helps reduce the amount of threads created by the test
101102
static final Executor executor = Executors.newCachedThreadPool();
102103

104+
static final long start = System.nanoTime();
105+
106+
public static String now() {
107+
long now = System.nanoTime() - start;
108+
long secs = now / 1000_000_000;
109+
long mill = (now % 1000_000_000) / 1000_000;
110+
long nan = now % 1000_000;
111+
return String.format("[%d s, %d ms, %d ns] ", secs, mill, nan);
112+
}
113+
103114
interface BHS extends Supplier<BodyHandler<Publisher<List<ByteBuffer>>>> {
104115
static BHS of(BHS impl, String name) {
105116
return new BHSImpl(impl, name);
@@ -216,6 +227,12 @@ public void testExceptions(String uri, boolean sameClient, BHS handlers) throws
216227
// Get the final result and compare it with the expected body
217228
String body = ofString.getBody().toCompletableFuture().get();
218229
assertEquals(body, "");
230+
// ensure client closes before next iteration
231+
if (!sameClient) {
232+
var tracker = TRACKER.getTracker(client);
233+
client = null;
234+
clientCleanup(tracker);
235+
}
219236
}
220237
}
221238

@@ -239,6 +256,12 @@ public void testNoBody(String uri, boolean sameClient, BHS handlers) throws Exce
239256
// Get the final result and compare it with the expected body
240257
String body = ofString.getBody().toCompletableFuture().get();
241258
assertEquals(body, "");
259+
// ensure client closes before next iteration
260+
if (!sameClient) {
261+
var tracker = TRACKER.getTracker(client);
262+
client = null;
263+
clientCleanup(tracker);
264+
}
242265
}
243266
}
244267

@@ -265,6 +288,12 @@ public void testNoBodyAsync(String uri, boolean sameClient, BHS handlers) throws
265288
});
266289
// Get the final result and compare it with the expected body
267290
assertEquals(result.get(), "");
291+
// ensure client closes before next iteration
292+
if (!sameClient) {
293+
var tracker = TRACKER.getTracker(client);
294+
client = null;
295+
clientCleanup(tracker);
296+
}
268297
}
269298
}
270299

@@ -288,6 +317,12 @@ public void testAsString(String uri, boolean sameClient, BHS handlers) throws Ex
288317
// Get the final result and compare it with the expected body
289318
String body = ofString.getBody().toCompletableFuture().get();
290319
assertEquals(body, WITH_BODY);
320+
// ensure client closes before next iteration
321+
if (!sameClient) {
322+
var tracker = TRACKER.getTracker(client);
323+
client = null;
324+
clientCleanup(tracker);
325+
}
291326
}
292327
}
293328

@@ -314,6 +349,12 @@ public void testAsStringAsync(String uri, boolean sameClient, BHS handlers) thro
314349
// Get the final result and compare it with the expected body
315350
String body = result.get();
316351
assertEquals(body, WITH_BODY);
352+
// ensure client closes before next iteration
353+
if (!sameClient) {
354+
var tracker = TRACKER.getTracker(client);
355+
client = null;
356+
clientCleanup(tracker);
357+
}
317358
}
318359
}
319360

@@ -451,6 +492,23 @@ public void teardown() throws Exception {
451492
}
452493
}
453494

495+
// Wait for the client to be garbage collected.
496+
// we use the ReferenceTracker API rather than HttpClient::close here,
497+
// because we want to get some diagnosis if a client doesn't release
498+
// its resources and terminates as expected
499+
// By using the ReferenceTracker, we will get some diagnosis about what
500+
// is keeping the client alive if it doesn't get GC'ed within the
501+
// expected time frame.
502+
public void clientCleanup(OperationTrackers.Tracker tracker){
503+
System.gc();
504+
System.out.println(now() + "waiting for client to shutdown: " + tracker.getName());
505+
System.err.println(now() + "waiting for client to shutdown: " + tracker.getName());
506+
var error = TRACKER.check(tracker, 10000);
507+
if (error != null) throw error;
508+
System.out.println(now() + "client shutdown normally: " + tracker.getName());
509+
System.err.println(now() + "client shutdown normally: " + tracker.getName());
510+
}
511+
454512
static final String WITH_BODY = "Lorem ipsum dolor sit amet, consectetur" +
455513
" adipiscing elit, sed do eiusmod tempor incididunt ut labore et" +
456514
" dolore magna aliqua. Ut enim ad minim veniam, quis nostrud" +

0 commit comments

Comments
 (0)