8262049: [TESTBUG] Fix TestReferenceRefersTo.java for Shenandoah IU mode

Reviewed-by: kbarrett, zgu
This commit is contained in:
Roman Kennke 2021-02-23 21:43:53 +00:00
parent e5304b3a99
commit c6eae06142
2 changed files with 132 additions and 12 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2020, 2021 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
@ -197,8 +197,6 @@ public class TestReferenceRefersTo {
expectCleared(testPhantom1, "testPhantom1");
expectCleared(testWeak2, "testWeak2");
expectValue(testWeak3, testObject3, "testWeak3");
// This is true for all currently supported concurrent collectors.
expectNotCleared(testWeak4, "testWeak4");
progress("verify get returns expected values");
if (testWeak2.get() != null) {
@ -213,11 +211,6 @@ public class TestReferenceRefersTo {
}
TestObject obj4 = testWeak4.get();
if (obj4 == null) {
fail("testWeak4.get() returned null");
} else if (obj4.value != 4) {
fail("testWeak4.get().value is " + obj4.value);
}
progress("verify queue entries");
long timeout = 60000; // 1 minute of milliseconds.
@ -243,10 +236,10 @@ public class TestReferenceRefersTo {
fail("testWeak2 not notified");
} else if (testWeak3 == null) {
fail("testWeak3 notified");
} else if (testWeak4 == null) {
if (obj4 != null) {
fail("testWeak4 notified");
}
}
if ((testWeak4 == null) != (obj4 == null)) {
fail("either referent is cleared and we got notified, or neither of this happened: referent: "
+ obj4 + ", notified: " + (testWeak4 == null));
}
} finally {

View File

@ -0,0 +1,127 @@
/*
* Copyright (c) 2020, 2021 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
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package gc;
/* @test
* @requires vm.gc != "Shenandoah" | vm.opt.ShenandoahGCMode != "iu"
* @library /test/lib
* @build sun.hotspot.WhiteBox
* @modules java.base
* @run main ClassFileInstaller sun.hotspot.WhiteBox
* @run main/othervm
* -Xbootclasspath/a:.
* -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI
* gc.TestReferenceRefersToDuringConcMark
*/
import java.lang.ref.Reference;
import java.lang.ref.WeakReference;
import sun.hotspot.WhiteBox;
public class TestReferenceRefersToDuringConcMark {
private static final WhiteBox WB = WhiteBox.getWhiteBox();
private static volatile Object testObject = null;
private static WeakReference<Object> testWeak = null;
private static void setup() {
testObject = new Object();
testWeak = new WeakReference<Object>(testObject);
}
private static void gcUntilOld(Object o) throws Exception {
if (!WB.isObjectInOldGen(o)) {
WB.fullGC();
if (!WB.isObjectInOldGen(o)) {
fail("object not promoted by full gc");
}
}
}
private static void gcUntilOld() throws Exception {
gcUntilOld(testObject);
gcUntilOld(testWeak);
}
private static void fail(String msg) throws Exception {
throw new RuntimeException(msg);
}
private static void expectNotCleared(Reference<Object> ref,
String which) throws Exception {
if (ref.refersTo(null)) {
fail("expected " + which + " to not be cleared");
}
}
private static void expectValue(Reference<Object> ref,
Object value,
String which) throws Exception {
expectNotCleared(ref, which);
if (!ref.refersTo(value)) {
fail(which + " doesn't refer to expected value");
}
}
private static void checkInitialStates() throws Exception {
expectValue(testWeak, testObject, "testWeak");
}
private static void discardStrongReferences() {
testObject = null;
}
private static void testConcurrentCollection() throws Exception {
setup();
gcUntilOld();
WB.concurrentGCAcquireControl();
try {
checkInitialStates();
discardStrongReferences();
WB.concurrentGCRunTo(WB.BEFORE_MARKING_COMPLETED);
// For most collectors - the configurations tested here -,
// calling get() will keep testObject alive.
if (testWeak.get() == null) {
fail("testWeak unexpectedly == null");
}
WB.concurrentGCRunToIdle();
expectNotCleared(testWeak, "testWeak");
} finally {
WB.concurrentGCReleaseControl();
}
}
public static void main(String[] args) throws Exception {
if (WB.supportsConcurrentGCBreakpoints()) {
testConcurrentCollection();
}
}
}