Search in sources :

Example 6 with PhantomReference

use of java.lang.ref.PhantomReference in project robovm by robovm.

the class PhantomReferenceTest method test_get.

/**
     * java.lang.ref.PhantomReference#get()
     */
public void test_get() {
    ReferenceQueue rq = new ReferenceQueue();
    bool = new Boolean(false);
    PhantomReference pr = new PhantomReference(bool, rq);
    assertNull("get() should return null.", pr.get());
    pr.enqueue();
    assertNull("get() should return null.", pr.get());
    pr.clear();
    assertNull("get() should return null.", pr.get());
}
Also used : ReferenceQueue(java.lang.ref.ReferenceQueue) PhantomReference(java.lang.ref.PhantomReference)

Example 7 with PhantomReference

use of java.lang.ref.PhantomReference in project robovm by robovm.

the class ReferenceQueueTest method test_removeJ.

/**
     * java.lang.ref.ReferenceQueue#remove(long)
     */
public void test_removeJ() {
    try {
        assertNull("Queue should be empty. (poll)", rq.poll());
        assertNull("Queue should be empty. (remove(1))", rq.remove((long) 1));
        Thread ct = new Thread(new ChildThread());
        ct.start();
        Reference ret = rq.remove(0L);
        assertNotNull("Delayed remove failed.", ret);
    } catch (InterruptedException e) {
        fail("InterruptedExeException during test : " + e.getMessage());
    } catch (Exception e) {
        fail("Exception during test : " + e.getMessage());
    }
    Object obj = new Object();
    WeakReference wr = new WeakReference(obj, rq);
    Boolean b = new Boolean(true);
    SoftReference sr = new SoftReference(b, rq);
    String str = "Test";
    PhantomReference pr = new PhantomReference(str, rq);
    pr.enqueue();
    wr.enqueue();
    sr.enqueue();
    try {
        Reference result = rq.remove(1L);
        assertTrue((Boolean) result.get());
        result = rq.remove(1L);
        assertEquals(obj, result.get());
        result = rq.remove(1L);
        assertNull(result.get());
    } catch (IllegalArgumentException e1) {
        fail("IllegalArgumentException was thrown.");
    } catch (InterruptedException e1) {
        fail("InterruptedException was thrown.");
    }
    rq = new ReferenceQueue();
    isThrown = false;
    assertNull(rq.poll());
    class RemoveThread extends Thread {

        public void run() {
            try {
                rq.remove(1000L);
            } catch (InterruptedException ie) {
                isThrown = true;
            }
        }
    }
    RemoveThread rt = new RemoveThread();
    rt.start();
    try {
        Thread.sleep(10);
    } catch (InterruptedException ie) {
    }
    rt.interrupt();
    try {
        Thread.sleep(10);
    } catch (InterruptedException ie) {
    }
    assertTrue(isThrown);
    assertNull(rq.poll());
    try {
        rq.remove(-1);
        fail("IllegalArgumentException expected.");
    } catch (IllegalArgumentException iae) {
    //expected
    } catch (InterruptedException e) {
        fail("Unexpected InterruptedException.");
    }
}
Also used : ReferenceQueue(java.lang.ref.ReferenceQueue) PhantomReference(java.lang.ref.PhantomReference) Reference(java.lang.ref.Reference) SoftReference(java.lang.ref.SoftReference) WeakReference(java.lang.ref.WeakReference) SoftReference(java.lang.ref.SoftReference) WeakReference(java.lang.ref.WeakReference) PhantomReference(java.lang.ref.PhantomReference)

Example 8 with PhantomReference

use of java.lang.ref.PhantomReference in project robovm by robovm.

the class ReferenceTest method test_enqueue.

/**
     * java.lang.ref.Reference#enqueue()
     */
public void test_enqueue() {
    ReferenceQueue rq = new ReferenceQueue();
    obj = new Object();
    Reference ref = new SoftReference(obj, rq);
    assertTrue("Enqueue failed.", (!ref.isEnqueued()) && ((ref.enqueue()) && (ref.isEnqueued())));
    assertTrue("Not properly enqueued.", rq.poll().get() == obj);
    // This fails...
    assertTrue("Should remain enqueued.", !ref.isEnqueued());
    assertTrue("Can not enqueue twice.", (!ref.enqueue()) && (rq.poll() == null));
    rq = new ReferenceQueue();
    obj = new Object();
    ref = new WeakReference(obj, rq);
    assertTrue("Enqueue failed2.", (!ref.isEnqueued()) && ((ref.enqueue()) && (ref.isEnqueued())));
    assertTrue("Not properly enqueued2.", rq.poll().get() == obj);
    // This
    assertTrue("Should remain enqueued2.", !ref.isEnqueued());
    // fails.
    assertTrue("Can not enqueue twice2.", (!ref.enqueue()) && (rq.poll() == null));
    ref = new PhantomReference(obj, rq);
    assertTrue("Enqueue failed3.", (!ref.isEnqueued()) && ((ref.enqueue()) && (ref.isEnqueued())));
    assertNull("Not properly enqueued3.", rq.poll().get());
    // This
    assertTrue("Should remain enqueued3.", !ref.isEnqueued());
    // fails.
    assertTrue("Can not enqueue twice3.", (!ref.enqueue()) && (rq.poll() == null));
}
Also used : ReferenceQueue(java.lang.ref.ReferenceQueue) SoftReference(java.lang.ref.SoftReference) PhantomReference(java.lang.ref.PhantomReference) Reference(java.lang.ref.Reference) SoftReference(java.lang.ref.SoftReference) WeakReference(java.lang.ref.WeakReference) WeakReference(java.lang.ref.WeakReference) PhantomReference(java.lang.ref.PhantomReference)

Example 9 with PhantomReference

use of java.lang.ref.PhantomReference in project robovm by robovm.

the class ReferenceTest method test_clear.

/**
     * java.lang.ref.Reference#clear()
     */
public void test_clear() {
    tmpA = new Object();
    tmpB = new Object();
    tmpC = new Object();
    SoftReference sr = new SoftReference(tmpA, new ReferenceQueue());
    WeakReference wr = new WeakReference(tmpB, new ReferenceQueue());
    PhantomReference pr = new PhantomReference(tmpC, new ReferenceQueue());
    assertTrue("Start: Object not cleared.", (sr.get() != null) && (wr.get() != null));
    assertNull("Referent is not null.", pr.get());
    sr.clear();
    wr.clear();
    pr.clear();
    assertTrue("End: Object cleared.", (sr.get() == null) && (wr.get() == null));
    assertNull("Referent is not null.", pr.get());
    // Must reference tmpA and tmpB so the jit does not optimize them away
    assertTrue("should always pass", tmpA != sr.get() && tmpB != wr.get());
}
Also used : SoftReference(java.lang.ref.SoftReference) ReferenceQueue(java.lang.ref.ReferenceQueue) WeakReference(java.lang.ref.WeakReference) PhantomReference(java.lang.ref.PhantomReference)

Example 10 with PhantomReference

use of java.lang.ref.PhantomReference in project mockito by mockito.

the class TypeCachingMockBytecodeGeneratorTest method ensure_cache_returns_same_instance.

@Test
public void ensure_cache_returns_same_instance() throws Exception {
    // given
    ClassLoader classloader_with_life_shorter_than_cache = inMemoryClassLoader().withClassDefinition("foo.Bar", makeMarkerInterface("foo.Bar")).build();
    TypeCachingBytecodeGenerator cachingMockBytecodeGenerator = new TypeCachingBytecodeGenerator(new SubclassBytecodeGenerator(), true);
    Class<?> the_mock_type = cachingMockBytecodeGenerator.mockClass(withMockFeatures(classloader_with_life_shorter_than_cache.loadClass("foo.Bar"), Collections.<Class<?>>emptySet(), SerializableMode.NONE));
    Class<?> other_mock_type = cachingMockBytecodeGenerator.mockClass(withMockFeatures(classloader_with_life_shorter_than_cache.loadClass("foo.Bar"), Collections.<Class<?>>emptySet(), SerializableMode.NONE));
    assertThat(other_mock_type).isSameAs(the_mock_type);
    ReferenceQueue<Object> referenceQueue = new ReferenceQueue<Object>();
    Reference<Object> typeReference = new PhantomReference<Object>(the_mock_type, referenceQueue);
    // when
    classloader_with_life_shorter_than_cache = is_no_more_referenced();
    the_mock_type = is_no_more_referenced();
    other_mock_type = is_no_more_referenced();
    System.gc();
    ensure_gc_happened();
    // then
    assertThat(referenceQueue.poll()).isEqualTo(typeReference);
}
Also used : ReferenceQueue(java.lang.ref.ReferenceQueue) PhantomReference(java.lang.ref.PhantomReference) ClassLoaders.inMemoryClassLoader(org.mockitoutil.ClassLoaders.inMemoryClassLoader) Test(org.junit.Test)

Aggregations

PhantomReference (java.lang.ref.PhantomReference)12 ReferenceQueue (java.lang.ref.ReferenceQueue)8 WeakReference (java.lang.ref.WeakReference)5 Reference (java.lang.ref.Reference)4 SoftReference (java.lang.ref.SoftReference)4 Test (org.junit.Test)2 ClassLoaders.inMemoryClassLoader (org.mockitoutil.ClassLoaders.inMemoryClassLoader)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 MethodHandle (java.lang.invoke.MethodHandle)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1