test/runtime/jni/ReturnJNIWeak/ReturnJNIWeak.java

Fri, 10 May 2019 18:50:40 +0000

author
phh
date
Fri, 10 May 2019 18:50:40 +0000
changeset 9671
e86bc9786d83
permissions
-rw-r--r--

8223664: Add jtreg tests for 8223528, backport to jdk8u of 8176100
Summary: Add hotspot/test/runtime/jni directory with two tests for 8176100
Reviewed-by: kbarrett, coleenp, tschatzl

phh@9671 1 /*
phh@9671 2 * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
phh@9671 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
phh@9671 4 *
phh@9671 5 * This code is free software; you can redistribute it and/or modify it
phh@9671 6 * under the terms of the GNU General Public License version 2 only, as
phh@9671 7 * published by the Free Software Foundation.
phh@9671 8 *
phh@9671 9 * This code is distributed in the hope that it will be useful, but WITHOUT
phh@9671 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
phh@9671 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
phh@9671 12 * version 2 for more details (a copy is included in the LICENSE file that
phh@9671 13 * accompanied this code).
phh@9671 14 *
phh@9671 15 * You should have received a copy of the GNU General Public License version
phh@9671 16 * 2 along with this work; if not, write to the Free Software Foundation,
phh@9671 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
phh@9671 18 *
phh@9671 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
phh@9671 20 * or visit www.oracle.com if you need additional information or have any
phh@9671 21 * questions.
phh@9671 22 */
phh@9671 23
phh@9671 24 public final class ReturnJNIWeak {
phh@9671 25 static {
phh@9671 26 System.loadLibrary("ReturnJNIWeak");
phh@9671 27 }
phh@9671 28
phh@9671 29 private static final class TestObject {
phh@9671 30 public final int value;
phh@9671 31
phh@9671 32 public TestObject(int value) {
phh@9671 33 this.value = value;
phh@9671 34 }
phh@9671 35 }
phh@9671 36
phh@9671 37 private static volatile TestObject testObject = null;
phh@9671 38
phh@9671 39 private static native void registerObject(Object o);
phh@9671 40 private static native void unregisterObject();
phh@9671 41 private static native Object getObject();
phh@9671 42
phh@9671 43 // Create the test object and record it both strongly and weakly.
phh@9671 44 private static void remember(int value) {
phh@9671 45 TestObject o = new TestObject(value);
phh@9671 46 registerObject(o);
phh@9671 47 testObject = o;
phh@9671 48 }
phh@9671 49
phh@9671 50 // Remove both strong and weak references to the current test object.
phh@9671 51 private static void forget() {
phh@9671 52 unregisterObject();
phh@9671 53 testObject = null;
phh@9671 54 }
phh@9671 55
phh@9671 56 // Verify the weakly recorded object
phh@9671 57 private static void checkValue(int value) throws Exception {
phh@9671 58 Object o = getObject();
phh@9671 59 if (o == null) {
phh@9671 60 throw new RuntimeException("Weak reference unexpectedly null");
phh@9671 61 }
phh@9671 62 TestObject t = (TestObject)o;
phh@9671 63 if (t.value != value) {
phh@9671 64 throw new RuntimeException("Incorrect value");
phh@9671 65 }
phh@9671 66 }
phh@9671 67
phh@9671 68 // Verify we can create a weak reference and get it back.
phh@9671 69 private static void testSanity() throws Exception {
phh@9671 70 System.out.println("running testSanity");
phh@9671 71 int value = 5;
phh@9671 72 try {
phh@9671 73 remember(value);
phh@9671 74 checkValue(value);
phh@9671 75 } finally {
phh@9671 76 forget();
phh@9671 77 }
phh@9671 78 }
phh@9671 79
phh@9671 80 // Verify weak ref value survives across collection if strong ref exists.
phh@9671 81 private static void testSurvival() throws Exception {
phh@9671 82 System.out.println("running testSurvival");
phh@9671 83 int value = 10;
phh@9671 84 try {
phh@9671 85 remember(value);
phh@9671 86 checkValue(value);
phh@9671 87 System.gc();
phh@9671 88 // Verify weak ref still has expected value.
phh@9671 89 checkValue(value);
phh@9671 90 } finally {
phh@9671 91 forget();
phh@9671 92 }
phh@9671 93 }
phh@9671 94
phh@9671 95 // Verify weak ref cleared if no strong ref exists.
phh@9671 96 private static void testClear() throws Exception {
phh@9671 97 System.out.println("running testClear");
phh@9671 98 int value = 15;
phh@9671 99 try {
phh@9671 100 remember(value);
phh@9671 101 checkValue(value);
phh@9671 102 // Verify still good.
phh@9671 103 checkValue(value);
phh@9671 104 // Drop reference.
phh@9671 105 testObject = null;
phh@9671 106 System.gc();
phh@9671 107 // Verify weak ref cleared as expected.
phh@9671 108 Object recorded = getObject();
phh@9671 109 if (recorded != null) {
phh@9671 110 throw new RuntimeException("expected clear");
phh@9671 111 }
phh@9671 112 } finally {
phh@9671 113 forget();
phh@9671 114 }
phh@9671 115 }
phh@9671 116
phh@9671 117 public static void main(String[] args) throws Exception {
phh@9671 118 testSanity();
phh@9671 119 testSurvival();
phh@9671 120 testClear();
phh@9671 121 }
phh@9671 122 }

mercurial