Search in sources :

Example 1 with Manager

use of org.jivesoftware.smack.Manager in project Smack by igniterealtime.

the class MemoryLeakTestUtil method noResourceLeakTest.

@SuppressWarnings("UnusedVariable")
public static <M extends Manager> void noResourceLeakTest(Function<DummyConnection, M> managerSupplier) throws XmppStringprepException, IllegalArgumentException, InterruptedException {
    final int numConnections = 10;
    ReferenceQueue<DummyConnection> connectionsReferenceQueue = new ReferenceQueue<>();
    ReferenceQueue<Manager> managerReferenceQueue = new ReferenceQueue<>();
    // Those two sets ensure that we hold a strong reference to the created PhantomReferences until the end of the
    // test.
    @SuppressWarnings("ModifiedButNotUsed") Set<PhantomReference<DummyConnection>> connectionsPhantomReferences = new HashSet<>();
    @SuppressWarnings("ModifiedButNotUsed") Set<PhantomReference<Manager>> managersPhantomReferences = new HashSet<>();
    List<DummyConnection> connections = new ArrayList<>(numConnections);
    for (int i = 0; i < numConnections; i++) {
        DummyConnection connection = new DummyConnection("foo" + i, "bar", "baz");
        PhantomReference<DummyConnection> connectionPhantomReference = new PhantomReference<>(connection, connectionsReferenceQueue);
        connectionsPhantomReferences.add(connectionPhantomReference);
        Manager manager = managerSupplier.apply(connection);
        PhantomReference<Manager> managerPhantomReference = new PhantomReference<Manager>(manager, managerReferenceQueue);
        managersPhantomReferences.add(managerPhantomReference);
        connections.add(connection);
    }
    // Clear the only references to the created connections.
    connections = null;
    triggerGarbageCollection();
    // Now the connections should have been gc'ed, but not managers not yet.
    assertReferencesQueueSize(connectionsReferenceQueue, numConnections);
    assertReferencesQueueIsEmpty(managerReferenceQueue);
    // We new create another connection and explicitly a new Manager. This will trigger the cleanup mechanism in the
    // WeakHashMaps used by the Manager's iNSTANCE field. This should clean up all references to the Managers.
    DummyConnection connection = new DummyConnection("last", "bar", "baz");
    @SuppressWarnings("unused") Manager manager = managerSupplier.apply(connection);
    // The previous Managers should now be reclaimable by the garbage collector. First trigger a GC run.
    triggerGarbageCollection();
    // Now the Managers should have been freed and this means we should see their phantom references in the
    // reference queue.
    assertReferencesQueueSize(managerReferenceQueue, numConnections);
}
Also used : ReferenceQueue(java.lang.ref.ReferenceQueue) DummyConnection(org.jivesoftware.smack.DummyConnection) ArrayList(java.util.ArrayList) Manager(org.jivesoftware.smack.Manager) PhantomReference(java.lang.ref.PhantomReference) HashSet(java.util.HashSet)

Aggregations

PhantomReference (java.lang.ref.PhantomReference)1 ReferenceQueue (java.lang.ref.ReferenceQueue)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 DummyConnection (org.jivesoftware.smack.DummyConnection)1 Manager (org.jivesoftware.smack.Manager)1