1 /*
   2  * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 import org.testng.annotations.Test;
  25 
  26 import java.util.concurrent.TimeUnit;
  27 import java.util.concurrent.locks.Condition;
  28 import java.util.concurrent.locks.ReentrantLock;
  29 
  30 import com.oracle.java.testlibrary.dcmd.CommandExecutor;
  31 import com.oracle.java.testlibrary.dcmd.JMXExecutor;
  32 
  33 /*
  34  * @test
  35  * @library /testlibrary
  36  * @build com.oracle.java.testlibrary.*
  37  * @build com.oracle.java.testlibrary.dcmd.*
  38  * @run testng RunFinalizationTest
  39  */
  40 public class RunFinalizationTest {
  41     static ReentrantLock lock = new ReentrantLock();
  42     static Condition cond = lock.newCondition();
  43     static volatile boolean wasFinalized = false;
  44     static volatile boolean wasInitialized = false;
  45 
  46     class MyObject {
  47         public MyObject() {
  48             /* Make sure object allocation/deallocation is not optimized out */
  49             wasInitialized = true;
  50         }
  51 
  52         protected void finalize() {
  53             lock.lock();
  54             wasFinalized = true;
  55             cond.signalAll();
  56             lock.unlock();
  57         }
  58     }
  59 
  60     public static MyObject o;
  61 
  62     public void run(CommandExecutor executor) {
  63         lock.lock();
  64         o = new MyObject();
  65         o = null;
  66         System.gc();
  67         executor.execute("GC.run_finalization");
  68 
  69         int waited = 0;
  70         int waitTime = 15;
  71 
  72         try {
  73             System.out.println("Waiting for signal from finalizer");
  74 
  75             while (!cond.await(waitTime, TimeUnit.SECONDS)) {
  76                 waited += waitTime;
  77                 System.out.println(String.format("Waited %d seconds", waited));
  78             }
  79 
  80             System.out.println("Received signal");
  81         } catch (InterruptedException e) {
  82             throw new RuntimeException("Test error: Interrupted while waiting for signal from finalizer", e);
  83         } finally {
  84             lock.unlock();
  85         }
  86 
  87         if (!wasFinalized) {
  88             throw new RuntimeException("Test failure: Object was not finalized");
  89         }
  90     }
  91 
  92     @Test
  93     public void jmx() {
  94         run(new JMXExecutor());
  95     }
  96 }