OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 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.testing.local.LocalRobolectricTestRunner; |
| 14 |
| 15 import java.lang.ref.WeakReference; |
| 16 |
| 17 /** |
| 18 * Tests for {@link DiscardableReferencePool}. |
| 19 */ |
| 20 @RunWith(LocalRobolectricTestRunner.class) |
| 21 @Config(manifest = Config.NONE) |
| 22 public class DiscardableReferencePoolTest { |
| 23 /** |
| 24 * Tests that draining the pool clears references and allows objects to be g
arbage collected. |
| 25 */ |
| 26 @Test |
| 27 public void testDrain() { |
| 28 DiscardableReferencePool pool = new DiscardableReferencePool(); |
| 29 |
| 30 Object object = new Object(); |
| 31 WeakReference<Object> weakReference = new WeakReference<>(object); |
| 32 |
| 33 DiscardableReference<Object> discardableReference = pool.put(object); |
| 34 Assert.assertEquals(object, discardableReference.get()); |
| 35 |
| 36 // Drop reference to the object itself, to allow it to be garbage-collec
ted. |
| 37 object = null; |
| 38 |
| 39 pool.drain(); |
| 40 |
| 41 // The discardable reference should be null now. |
| 42 Assert.assertNull(discardableReference.get()); |
| 43 |
| 44 // The object is not (strongly) reachable anymore, so the weak reference
may or may not be |
| 45 // null (it could be if a GC has happened since the pool was drained). |
| 46 // After an explicit GC call it definitely should be null. |
| 47 Runtime.getRuntime().gc(); |
| 48 |
| 49 Assert.assertNull(weakReference.get()); |
| 50 } |
| 51 |
| 52 /** |
| 53 * Tests that dropping the (last) discardable reference to an object allows
it to be regularly |
| 54 * garbage collected. |
| 55 */ |
| 56 @Test |
| 57 public void testReferenceGCd() { |
| 58 DiscardableReferencePool pool = new DiscardableReferencePool(); |
| 59 |
| 60 Object object = new Object(); |
| 61 WeakReference<Object> weakReference = new WeakReference<>(object); |
| 62 |
| 63 DiscardableReference<Object> discardableReference = pool.put(object); |
| 64 Assert.assertEquals(object, discardableReference.get()); |
| 65 |
| 66 // Drop reference to the object itself and to the discardable reference,
allowing the object |
| 67 // to be garbage-collected. |
| 68 object = null; |
| 69 discardableReference = null; |
| 70 |
| 71 // The object is not (strongly) reachable anymore, so the weak reference
may or may not be |
| 72 // null (it could be if a GC has happened since the pool was drained). |
| 73 // After an explicit GC call it definitely should be null. |
| 74 Runtime.getRuntime().gc(); |
| 75 |
| 76 Assert.assertNull(weakReference.get()); |
| 77 } |
| 78 } |
OLD | NEW |