Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -62,24 +62,22 @@ final class ManagedChannelOrphanWrapper extends ForwardingManagedChannel {

@Override
public ManagedChannel shutdown() {
ManagedChannel result = super.shutdown();
phantom.clearSafely();
// This dummy check prevents the JIT from collecting 'this' too early
if (this.getClass() == null) {
throw new AssertionError();
}
return result;
return super.shutdown();
}

@Override
public ManagedChannel shutdownNow() {
ManagedChannel result = super.shutdownNow();
phantom.clearSafely();
// This dummy check prevents the JIT from collecting 'this' too early
if (this.getClass() == null) {
throw new AssertionError();
}
return result;
return super.shutdownNow();
}

@VisibleForTesting
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,31 +140,6 @@ public boolean isLoggable(LogRecord record) {
}
}

@Test
public void orphanedChannel_triggerWarningAndCoverage() {
ManagedChannel mc = new TestManagedChannel();
final ReferenceQueue<ManagedChannelOrphanWrapper> refqueue = new ReferenceQueue<>();
ConcurrentMap<ManagedChannelReference, ManagedChannelReference> refs =
new ConcurrentHashMap<>();

// Create the wrapper but NEVER call shutdown
@SuppressWarnings("UnusedVariable")
ManagedChannelOrphanWrapper wrapper = new ManagedChannelOrphanWrapper(mc, refqueue, refs);
wrapper = null; // Make it eligible for GC

// Trigger GC and clean the queue to hit the !wasShutdown branch
final AtomicInteger numOrphans = new AtomicInteger();
GcFinalization.awaitDone(new FinalizationPredicate() {
@Override
public boolean isDone() {
numOrphans.getAndAdd(ManagedChannelReference.cleanQueue(refqueue));
return numOrphans.get() > 0;
}
});

assertEquals(1, numOrphans.get());
}

@Test
public void refCycleIsGCed() {
ReferenceQueue<ManagedChannelOrphanWrapper> refqueue =
Expand Down
Loading