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);
}
}
Aggregations