/**
  * Tries to run garbage collection, and waits for the {@link WeakReference}s to the {@link Thing}s
  * in the maps last returned by {@link Parent#things()} and {@link Child#things()} for {@code
  * keys} to be cleared.
  */
 void gcAndWaitUntilWeakReferencesCleared(final Class<?>... keys) {
   GcFinalization.awaitDone(
       new FinalizationPredicate() {
         @Override
         public boolean isDone() {
           for (Class<?> key : keys) {
             if (parentAsserts.weakThingReferenceUncollected(key)
                 || childAsserts.weakThingReferenceUncollected(key)) {
               return false;
             }
           }
           return true;
         }
       });
 }
Пример #2
0
  public void testProxyClassUnloading() {
    Object testObject =
        Guice.createInjector(interceptorModule, testModule).getInstance(proxyTestClass);
    assertNotNull(testObject.getClass().getClassLoader());
    assertNotSame(testObject.getClass().getClassLoader(), systemClassLoader);

    // take a weak reference to the generated proxy class
    WeakReference<Class<?>> clazzRef = new WeakReference<Class<?>>(testObject.getClass());

    assertNotNull(clazzRef.get());

    // null the proxy
    testObject = null;

    /*
     * this should be enough to queue the weak reference
     * unless something is holding onto it accidentally.
     */
    GcFinalization.awaitClear(clazzRef);

    // This test could be somewhat flaky when the GC isn't working.
    // If it fails, run the test again to make sure it's failing reliably.
    assertNull("Proxy class was not unloaded.", clazzRef.get());
  }