1 /*
   2  * Copyright (c) 2011, 2017, 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 /* @test
  25  * @bug 4268317 8132306 8175797
  26  * @summary Test if Reference.enqueue() works properly with GC
  27  * @run main ReferenceEnqueue
  28  */
  29 
  30 import java.lang.ref.*;
  31 import java.util.ArrayList;
  32 import java.util.List;
  33 
  34 public class ReferenceEnqueue {
  35 
  36     public static void main(String args[]) throws Exception {
  37         for (int i=0; i < 5; i++) {
  38             new WeakRef().run();
  39             new ExplicitEnqueue().run();
  40         }
  41         System.out.println("Test passed.");
  42     }
  43 
  44     static class WeakRef {
  45         final ReferenceQueue<Object> queue = new ReferenceQueue<Object>();
  46         final Reference<Object> ref;
  47         final int iterations = 1000;
  48 
  49         WeakRef() {
  50             this.ref = new WeakReference<Object>(new Object(), queue);
  51         }
  52 
  53         void run() throws InterruptedException {
  54             System.gc();
  55             for (int i = 0; i < iterations; i++) {
  56                 System.gc();
  57                 if (ref.isEnqueued()) {
  58                     break;
  59                 }
  60 
  61                 Thread.sleep(100);
  62             }
  63 
  64             if (ref.isEnqueued() == false) {
  65                 // GC have not enqueued refWeak for the timeout period
  66                 System.out.println("Reference not enqueued yet");
  67                 return;
  68             }
  69 
  70             if (ref.enqueue() == true) {
  71                 // enqueue() should return false since
  72                 // ref is already enqueued by the GC
  73                 throw new RuntimeException("Error: enqueue() returned true;"
  74                         + " expected false");
  75             }
  76 
  77             if (queue.poll() == null) {
  78                 // poll() should return ref enqueued by the GC
  79                 throw new RuntimeException("Error: poll() returned null;"
  80                         + " expected ref object");
  81             }
  82         }
  83     }
  84 
  85     static class ExplicitEnqueue {
  86         final ReferenceQueue<Object> queue = new ReferenceQueue<>();
  87         final List<Reference<Object>> refs = new ArrayList<>();
  88         final int iterations = 1000;
  89 
  90         ExplicitEnqueue() {
  91             this.refs.add(new SoftReference<>(new Object(), queue));
  92             this.refs.add(new WeakReference<>(new Object(), queue));
  93             // Can't test PhantomReference because get() always returns null.
  94         }
  95 
  96         void run() throws InterruptedException {
  97             for (Reference<Object> ref : refs) {
  98                 if (ref.enqueue() == false) {
  99                     throw new RuntimeException("Error: enqueue failed");
 100                 }
 101                 if (ref.get() != null) {
 102                     throw new RuntimeException("Error: referent must be cleared");
 103                 }
 104             }
 105 
 106             System.gc();
 107             for (int i = 0; refs.size() > 0 && i < iterations; i++) {
 108                 Reference<Object> ref = (Reference<Object>)queue.poll();
 109                 if (ref == null) {
 110                     System.gc();
 111                     Thread.sleep(100);
 112                     continue;
 113                 }
 114 
 115                 if (refs.remove(ref) == false) {
 116                     throw new RuntimeException("Error: unknown reference " + ref);
 117                 }
 118             }
 119 
 120             if (!refs.isEmpty()) {
 121                 throw new RuntimeException("Error: not all references are removed");
 122             }
 123         }
 124     }
 125 }