Search in sources :

Example 1 with ManagedChannelReference

use of io.grpc.internal.ManagedChannelOrphanWrapper.ManagedChannelReference in project grpc-java by grpc.

the class ManagedChannelOrphanWrapperTest method orphanedChannelsAreLogged.

@Test
public void orphanedChannelsAreLogged() {
    ManagedChannel mc = new TestManagedChannel();
    String channelString = mc.toString();
    final ReferenceQueue<ManagedChannelOrphanWrapper> refqueue = new ReferenceQueue<>();
    ConcurrentMap<ManagedChannelReference, ManagedChannelReference> refs = new ConcurrentHashMap<>();
    assertEquals(0, refs.size());
    @SuppressWarnings("UnusedVariable") ManagedChannelOrphanWrapper channel = new ManagedChannelOrphanWrapper(mc, refqueue, refs);
    assertEquals(1, refs.size());
    // Try to capture the log output but without causing terminal noise.  Adding the filter must
    // be done before clearing the ref or else it might be missed.
    final List<LogRecord> records = new ArrayList<>(1);
    Logger orphanLogger = Logger.getLogger(ManagedChannelOrphanWrapper.class.getName());
    Filter oldFilter = orphanLogger.getFilter();
    orphanLogger.setFilter(new Filter() {

        @Override
        public boolean isLoggable(LogRecord record) {
            synchronized (records) {
                records.add(record);
            }
            return false;
        }
    });
    try {
        channel = null;
        final AtomicInteger numOrphans = new AtomicInteger();
        GcFinalization.awaitDone(new FinalizationPredicate() {

            @Override
            public boolean isDone() {
                numOrphans.getAndAdd(ManagedChannelReference.cleanQueue(refqueue));
                return numOrphans.get() > 0;
            }
        });
        assertEquals("unexpected extra orphans", 1, numOrphans.get());
        LogRecord lr;
        synchronized (records) {
            assertEquals(1, records.size());
            lr = records.get(0);
        }
        assertThat(lr.getMessage()).contains("shutdown");
        assertThat(lr.getParameters()).asList().containsExactly(channelString).inOrder();
        assertEquals(Level.SEVERE, lr.getLevel());
        assertEquals(0, refs.size());
    } finally {
        orphanLogger.setFilter(oldFilter);
    }
}
Also used : ReferenceQueue(java.lang.ref.ReferenceQueue) ArrayList(java.util.ArrayList) Logger(java.util.logging.Logger) ManagedChannelReference(io.grpc.internal.ManagedChannelOrphanWrapper.ManagedChannelReference) LogRecord(java.util.logging.LogRecord) Filter(java.util.logging.Filter) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ManagedChannel(io.grpc.ManagedChannel) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) FinalizationPredicate(com.google.common.testing.GcFinalization.FinalizationPredicate) Test(org.junit.Test)

Aggregations

FinalizationPredicate (com.google.common.testing.GcFinalization.FinalizationPredicate)1 ManagedChannel (io.grpc.ManagedChannel)1 ManagedChannelReference (io.grpc.internal.ManagedChannelOrphanWrapper.ManagedChannelReference)1 ReferenceQueue (java.lang.ref.ReferenceQueue)1 ArrayList (java.util.ArrayList)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Filter (java.util.logging.Filter)1 LogRecord (java.util.logging.LogRecord)1 Logger (java.util.logging.Logger)1 Test (org.junit.Test)1