1 // Copyright 2017 The Chromium Authors 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 package org.chromium.base; 6 7 import org.junit.Assert; 8 import org.junit.Test; 9 import org.junit.runner.RunWith; 10 import org.robolectric.annotation.Config; 11 12 import org.chromium.base.DiscardableReferencePool.DiscardableReference; 13 import org.chromium.base.test.BaseRobolectricTestRunner; 14 15 import java.lang.ref.WeakReference; 16 17 /** Tests for {@link DiscardableReferencePool}. */ 18 @RunWith(BaseRobolectricTestRunner.class) 19 @Config(manifest = Config.NONE) 20 public class DiscardableReferencePoolTest { 21 /** 22 * Tests that draining the pool clears references and allows objects to be garbage collected. 23 */ 24 @Test testDrain()25 public void testDrain() { 26 DiscardableReferencePool pool = new DiscardableReferencePool(); 27 28 Object object = new Object(); 29 WeakReference<Object> weakReference = new WeakReference<>(object); 30 31 DiscardableReference<Object> discardableReference = pool.put(object); 32 Assert.assertEquals(object, discardableReference.get()); 33 34 // Drop reference to the object itself, to allow it to be garbage-collected. 35 object = null; 36 37 pool.drain(); 38 39 // The discardable reference should be null now. 40 Assert.assertNull(discardableReference.get()); 41 42 // The object is not (strongly) reachable anymore, so the weak reference may or may not be 43 // null (it could be if a GC has happened since the pool was drained). It should be 44 // eligible for garbage collection. 45 Assert.assertTrue(GarbageCollectionTestUtils.canBeGarbageCollected(weakReference)); 46 } 47 48 @Test testRemoveAfterDrainDoesNotThrow()49 public void testRemoveAfterDrainDoesNotThrow() { 50 DiscardableReferencePool pool = new DiscardableReferencePool(); 51 52 Object object = new Object(); 53 WeakReference<Object> weakReference = new WeakReference<>(object); 54 55 DiscardableReference<Object> discardableReference = pool.put(object); 56 Assert.assertEquals(object, discardableReference.get()); 57 58 // Release the strong reference. 59 object = null; 60 61 pool.drain(); 62 63 // Shouldn't throw any exception. 64 pool.remove(discardableReference); 65 66 // The discardable reference should be null now. 67 Assert.assertNull(discardableReference.get()); 68 69 // The object is not (strongly) reachable anymore, so the weak reference may or may not be 70 // null (it could be if a GC has happened since the pool was drained). It should be 71 // eligible for garbage collection. 72 Assert.assertTrue(GarbageCollectionTestUtils.canBeGarbageCollected(weakReference)); 73 } 74 75 @Test testDrainAfterRemoveDoesNotThrow()76 public void testDrainAfterRemoveDoesNotThrow() { 77 DiscardableReferencePool pool = new DiscardableReferencePool(); 78 79 Object object = new Object(); 80 WeakReference<Object> weakReference = new WeakReference<>(object); 81 82 DiscardableReference<Object> discardableReference = pool.put(object); 83 Assert.assertEquals(object, discardableReference.get()); 84 85 // Release the strong reference. 86 object = null; 87 88 pool.remove(discardableReference); 89 90 // Shouldn't throw any exception. 91 pool.drain(); 92 93 // The discardable reference should be null now. 94 Assert.assertNull(discardableReference.get()); 95 96 // The object is not (strongly) reachable anymore, so the weak reference may or may not be 97 // null (it could be if a GC has happened since the pool was drained). It should be 98 // eligible for garbage collection. 99 Assert.assertTrue(GarbageCollectionTestUtils.canBeGarbageCollected(weakReference)); 100 } 101 102 /** 103 * Tests that dropping the (last) discardable reference to an object allows it to be regularly 104 * garbage collected. 105 */ 106 @Test testReferenceGCd()107 public void testReferenceGCd() { 108 DiscardableReferencePool pool = new DiscardableReferencePool(); 109 110 Object object = new Object(); 111 WeakReference<Object> weakReference = new WeakReference<>(object); 112 113 DiscardableReference<Object> discardableReference = pool.put(object); 114 Assert.assertEquals(object, discardableReference.get()); 115 116 // Drop reference to the object itself and to the discardable reference, allowing the object 117 // to be garbage-collected. 118 object = null; 119 discardableReference = null; 120 121 // The object is not (strongly) reachable anymore, so the weak reference may or may not be 122 // null (it could be if a GC has happened since the pool was drained). It should be 123 // eligible for garbage collection. 124 Assert.assertTrue(GarbageCollectionTestUtils.canBeGarbageCollected(weakReference)); 125 } 126 } 127