Skip to content

Commit

Permalink
Merge pull request #2937 from llxia/test5
Browse files Browse the repository at this point in the history
Update CloneWeakReferenceTest for JDK11
  • Loading branch information
pshipton authored Sep 19, 2018
2 parents e3576f2 + 4bcafaa commit 3e04a01
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 31 deletions.
16 changes: 16 additions & 0 deletions test/functional/VM_Test/build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
<property name="src_80" location="./src_80" />
<property name="build" location="./bin" />
<property name="transformerListener" location="${TEST_ROOT}/Utils/src"/>
<property name="TestUtilities" location="../TestUtilities/src"/>

<path id="build.cp">
<fileset dir="${TEST_ROOT}/TestConfig/lib/" includes="junit4.jar" />
Expand Down Expand Up @@ -86,6 +87,11 @@
<src path="${src}" />
<src path="${src_80}" />
<src path="${excludeFiles}"/>
<src path="${TestUtilities}" />
<exclude name="**/attachAPI/**" />
<classpath>
<pathelement location="${TEST_ROOT}/TestConfig/lib/testng.jar"/>
</classpath>
</javac>
</then>
<elseif>
Expand All @@ -95,21 +101,31 @@
<src path="${src}" />
<src path="${src_80}" />
<src path="${excludeFiles}"/>
<src path="${TestUtilities}" />
<exclude name="**/attachAPI/**" />
<!-- 133609: ClassPathSettingClassLoaderTest failed in jdk9 -->
<exclude name="**/ClassPathSettingClassLoader.java" />
<exclude name="**/ClassPathSettingClassLoaderTest.java" />
<compilerarg line='${addExports}' />
<classpath>
<pathelement location="${TEST_ROOT}/TestConfig/lib/testng.jar"/>
</classpath>
</javac>
</then>
</elseif>
<else>
<javac destdir="${build}" debug="true" fork="true" executable="${compiler.javac}" includeAntRuntime="false" encoding="ISO-8859-1" classpathref="build.cp">
<src path="${src}" />
<src path="${excludeFiles}"/>
<src path="${TestUtilities}" />
<exclude name="**/attachAPI/**" />
<!-- 133609: ClassPathSettingClassLoaderTest failed in jdk9 -->
<exclude name="**/ClassPathSettingClassLoader.java" />
<exclude name="**/ClassPathSettingClassLoaderTest.java" />
<compilerarg line='${addExports}' />
<classpath>
<pathelement location="${TEST_ROOT}/TestConfig/lib/testng.jar"/>
</classpath>
</javac>
</else>
</if>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2001, 2012 IBM Corp. and others
* Copyright (c) 2001, 2018 IBM Corp. and others
*
* This program and the accompanying materials are made available under
* the terms of the Eclipse Public License 2.0 which accompanies this
Expand All @@ -22,6 +22,7 @@
package j9vm.test.clone;

import java.lang.ref.WeakReference;
import org.openj9.test.util.VersionCheck;

/**
* Test case to ensure that the VM is properly cloning weak references.
Expand All @@ -41,46 +42,60 @@ public static void main(String[] args) throws CloneNotSupportedException {
Object referent = new String("foobar");

SubWeakReference reference = new SubWeakReference(referent);
SubWeakReference clone = (SubWeakReference) reference.clone();

if (clone.get() == referent) {
System.out.println("original and cloned weak references using the same referent (expected)");
} else {
throw new RuntimeException("original and cloned weak references are not using the same referent!");
}

referent = null;

System.gc();

/* At this point the weak references may not have been cleared. In metronome, if a GC cycle
* is ongoing when System.gc is called, the cycle will be finished. This GC is a
* snapshot-at-the-beginning collector. If the GC cycle was started BEFORE referent was set
* to null, then String object will not be collected and the weak references will not be
* cleared.
*/

if (reference.get() != clone.get()) {
throw new RuntimeException("Referents not identical. reference.get(): " + reference.get() + " clone.get(): " + clone.get());
}

System.gc();

if (reference.get() != null) {
throw new RuntimeException("Reference referent not cleared: " + reference + " " + reference.get());
if (VersionCheck.major() >= 11) {
try {
SubWeakReference clone = (SubWeakReference) reference.clone();
// fail if CloneNotSupportedException is not thrown
throw new RuntimeException(
"CloneNotSupportedException is expected, but it is not thrown for JDK version: "
+ VersionCheck.major());
} catch (CloneNotSupportedException e) {
// CloneNotSupportedException is expected for JDK version >= 11
}
} else {
SubWeakReference clone = (SubWeakReference) reference.clone();

if (clone.get() == referent) {
System.out.println("original and cloned weak references using the same referent (expected)");
} else {
throw new RuntimeException("original and cloned weak references are not using the same referent!");
}

referent = null;

System.gc();

/* At this point the weak references may not have been cleared. In metronome, if a GC cycle
* is ongoing when System.gc is called, the cycle will be finished. This GC is a
* snapshot-at-the-beginning collector. If the GC cycle was started BEFORE referent was set
* to null, then String object will not be collected and the weak references will not be
* cleared.
*/

if (reference.get() != clone.get()) {
throw new RuntimeException("Referents not identical. reference.get(): " + reference.get() + " clone.get(): " + clone.get());
}

System.gc();

if (reference.get() != null) {
throw new RuntimeException("Reference referent not cleared: " + reference + " " + reference.get());
}

if (clone.get() != null) {
throw new RuntimeException("Clone referent not cleared: " + clone + " " + clone.get());
}
}

if (clone.get() != null) {
throw new RuntimeException("Clone referent not cleared: " + clone + " " + clone.get());
}
}

static class SubWeakReference extends WeakReference implements Cloneable {

public SubWeakReference(Object referent) {
super(referent);
}

public Object clone() throws CloneNotSupportedException {
return super.clone();
}
Expand Down

0 comments on commit 3e04a01

Please sign in to comment.