• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2005 The Guava Authors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.google.common.base;
18 
19 import com.google.common.base.internal.Finalizer;
20 import com.google.common.testing.GcFinalization;
21 import java.lang.ref.ReferenceQueue;
22 import java.lang.ref.WeakReference;
23 import java.net.URL;
24 import java.net.URLClassLoader;
25 import java.util.Arrays;
26 import java.util.Collections;
27 import junit.framework.TestCase;
28 
29 /**
30  * Unit test for {@link FinalizableReferenceQueue}.
31  *
32  * @author Bob Lee
33  */
34 public class FinalizableReferenceQueueTest extends TestCase {
35 
36   private FinalizableReferenceQueue frq;
37 
38   @Override
tearDown()39   protected void tearDown() throws Exception {
40     frq = null;
41   }
42 
testFinalizeReferentCalled()43   public void testFinalizeReferentCalled() {
44     final MockReference reference = new MockReference(frq = new FinalizableReferenceQueue());
45 
46     GcFinalization.awaitDone(
47         new GcFinalization.FinalizationPredicate() {
48           public boolean isDone() {
49             return reference.finalizeReferentCalled;
50           }
51         });
52   }
53 
54   static class MockReference extends FinalizableWeakReference<Object> {
55 
56     volatile boolean finalizeReferentCalled;
57 
MockReference(FinalizableReferenceQueue frq)58     MockReference(FinalizableReferenceQueue frq) {
59       super(new Object(), frq);
60     }
61 
62     @Override
finalizeReferent()63     public void finalizeReferent() {
64       finalizeReferentCalled = true;
65     }
66   }
67 
68   /**
69    * Keeps a weak reference to the underlying reference queue. When this reference is cleared, we
70    * know that the background thread has stopped and released its strong reference.
71    */
72   private WeakReference<ReferenceQueue<Object>> queueReference;
73 
testThatFinalizerStops()74   public void testThatFinalizerStops() {
75     weaklyReferenceQueue();
76     GcFinalization.awaitClear(queueReference);
77   }
78 
79   /** If we don't keep a strong reference to the reference object, it won't be enqueued. */
80   FinalizableWeakReference<Object> reference;
81 
82   /** Create the FRQ in a method that goes out of scope so that we're sure it will be reclaimed. */
weaklyReferenceQueue()83   private void weaklyReferenceQueue() {
84     frq = new FinalizableReferenceQueue();
85     queueReference = new WeakReference<>(frq.queue);
86 
87     /*
88      * Queue and clear a reference for good measure. We test later on that
89      * the finalizer thread stopped, but we should test that it actually
90      * started first.
91      */
92     reference =
93         new FinalizableWeakReference<Object>(new Object(), frq) {
94           @Override
95           public void finalizeReferent() {
96             reference = null;
97             frq = null;
98           }
99         };
100   }
101 
102   @AndroidIncompatible // no concept of separate ClassLoaders
testDecoupledLoader()103   public void testDecoupledLoader() {
104     FinalizableReferenceQueue.DecoupledLoader decoupledLoader =
105         new FinalizableReferenceQueue.DecoupledLoader() {
106           @Override
107           URLClassLoader newLoader(URL base) {
108             return new DecoupledClassLoader(new URL[] {base});
109           }
110         };
111 
112     Class<?> finalizerCopy = decoupledLoader.loadFinalizer();
113 
114     assertNotNull(finalizerCopy);
115     assertNotSame(Finalizer.class, finalizerCopy);
116 
117     assertNotNull(FinalizableReferenceQueue.getStartFinalizer(finalizerCopy));
118   }
119 
120   static class DecoupledClassLoader extends URLClassLoader {
121 
DecoupledClassLoader(URL[] urls)122     public DecoupledClassLoader(URL[] urls) {
123       super(urls);
124     }
125 
126     @Override
loadClass(String name, boolean resolve)127     protected synchronized Class<?> loadClass(String name, boolean resolve)
128         throws ClassNotFoundException {
129       // Force Finalizer to load from this class loader, not its parent.
130       if (name.equals(Finalizer.class.getName())) {
131         Class<?> clazz = findClass(name);
132         if (resolve) {
133           resolveClass(clazz);
134         }
135         return clazz;
136       }
137 
138       return super.loadClass(name, resolve);
139     }
140   }
141 
142   @AndroidIncompatible // TODO(cpovirk): How significant is this failure?
testGetFinalizerUrl()143   public void testGetFinalizerUrl() {
144     assertNotNull(getClass().getResource("internal/Finalizer.class"));
145   }
146 
testFinalizeClassHasNoNestedClasses()147   public void testFinalizeClassHasNoNestedClasses() throws Exception {
148     // Ensure that the Finalizer class has no nested classes.
149     // See https://code.google.com/p/guava-libraries/issues/detail?id=1505
150     assertEquals(Collections.emptyList(), Arrays.asList(Finalizer.class.getDeclaredClasses()));
151   }
152 }
153