Skip to content

Commit 3626ac3

Browse files
committed
8204868: java/util/zip/ZipFile/TestCleaner.java still fails with "cleaner failed to clean zipfile."
Reviewed-by: lancea
1 parent 4bb3d81 commit 3626ac3

File tree

1 file changed

+33
-23
lines changed

1 file changed

+33
-23
lines changed

test/jdk/java/util/zip/ZipFile/TestCleaner.java

Lines changed: 33 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2018, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -23,16 +23,24 @@
2323

2424
/* @test
2525
* @bug 8185582 8197989
26-
* @modules java.base/java.util.zip:open java.base/jdk.internal.vm.annotation
2726
* @summary Check the resources of Inflater, Deflater and ZipFile are always
2827
* cleaned/released when the instance is not unreachable
28+
* @modules java.base/java.util.zip:open java.base/jdk.internal.vm.annotation
29+
* @library /test/lib
30+
* @comment The test relies on the Cleaner to invoke the cleaning actions. So
31+
* we use "othervm" to prevent any Cleaner delays that could be contributed by any
32+
* other tests or code that might have executed on the agentvm prior to this test
33+
* execution
34+
* @run main/othervm TestCleaner
2935
*/
3036

3137
import java.io.*;
3238
import java.lang.reflect.*;
3339
import java.util.*;
40+
import java.util.concurrent.atomic.AtomicLong;
3441
import java.util.zip.*;
3542
import jdk.internal.vm.annotation.DontInline;
43+
import jdk.test.lib.util.ForceGC;
3644
import static java.nio.charset.StandardCharsets.US_ASCII;
3745

3846
public class TestCleaner {
@@ -59,11 +67,11 @@ private static void testDeInflater() throws Throwable {
5967
Field zsRefDef = Deflater.class.getDeclaredField("zsRef");
6068
Field zsRefInf = Inflater.class.getDeclaredField("zsRef");
6169
if (!zsRefDef.trySetAccessible() || !zsRefInf.trySetAccessible()) {
62-
throw new RuntimeException("'zsRef' is not accesible");
70+
throw new RuntimeException("'zsRef' is not accessible");
6371
}
6472
if (addrOf(zsRefDef.get(new Deflater())) == -1 ||
6573
addrOf(zsRefInf.get(new Inflater())) == -1) {
66-
throw new RuntimeException("'addr' is not accesible");
74+
throw new RuntimeException("'addr' is not accessible");
6775
}
6876
List<Object> list = new ArrayList<>();
6977
byte[] buf1 = new byte[1024];
@@ -84,16 +92,17 @@ private static void testDeInflater() throws Throwable {
8492
}
8593
}
8694

87-
int n = 10;
88-
long cnt = list.size();
89-
while (n-- > 0 && cnt != 0) {
90-
Thread.sleep(100);
91-
System.gc();
92-
cnt = list.stream().filter(o -> addrOf(o) != 0).count();
95+
final AtomicLong numNotYetCleaned = new AtomicLong();
96+
// trigger GC
97+
final boolean resourcesCleaned = ForceGC.wait(() -> {
98+
final long remaining = list.stream().filter(o -> addrOf(o) != 0).count();
99+
numNotYetCleaned.set(remaining);
100+
return remaining == 0;
101+
});
102+
if (!resourcesCleaned) {
103+
throw new RuntimeException(numNotYetCleaned.get()
104+
+ " resources haven't yet been cleaned");
93105
}
94-
if (cnt != 0)
95-
throw new RuntimeException("cleaner failed to clean : " + cnt);
96-
97106
}
98107

99108
@DontInline
@@ -139,17 +148,18 @@ private static void testZipFile() throws Throwable {
139148
if (zsrc != null) {
140149
Field zfileField = zsrc.getClass().getDeclaredField("zfile");
141150
if (!zfileField.trySetAccessible()) {
142-
throw new RuntimeException("'ZipFile.Source.zfile' is not accesible");
143-
}
144-
//System.out.println("zffile: " + zfileField.get(zsrc));
145-
int n = 10;
146-
while (n-- > 0 && zfileField.get(zsrc) != null) {
147-
System.out.println("waiting gc ... " + n);
148-
System.gc();
149-
Thread.sleep(100);
151+
throw new RuntimeException("'ZipFile.Source.zfile' is not accessible");
150152
}
151-
if (zfileField.get(zsrc) != null) {
152-
throw new RuntimeException("cleaner failed to clean zipfile.");
153+
final boolean resourceCleaned = ForceGC.wait(() -> {
154+
try {
155+
return zfileField.get(zsrc) == null;
156+
} catch (IllegalAccessException e) {
157+
// shouldn't happen
158+
throw new RuntimeException(e);
159+
}
160+
});
161+
if (!resourceCleaned) {
162+
throw new RuntimeException("cleaner failed to clean zipfile " + zip);
153163
}
154164
}
155165
}

0 commit comments

Comments
 (0)