xds: use GC finalization predicate to avoid race between ref enqueued and calling cleanQueue() (#7629)

GcFinalization.awaitClear() only guarantees the reference is cleared but no guarantee for the ref is enqueued. This could cause race for calling cleanQueue() in the test.
This commit is contained in:
Chengyuan Zhang 2020-11-17 02:05:48 -08:00 committed by GitHub
parent c850840342
commit f27a8f26a8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 8 additions and 2 deletions

View File

@ -19,6 +19,7 @@ package io.grpc.xds;
import static com.google.common.truth.Truth.assertThat;
import com.google.common.testing.GcFinalization;
import com.google.common.testing.GcFinalization.FinalizationPredicate;
import io.grpc.xds.SharedCallCounterMap.CounterReference;
import java.util.HashMap;
import java.util.Map;
@ -50,9 +51,14 @@ public class SharedCallCounterMapTest {
public void autoCleanUp() {
@SuppressWarnings("UnusedVariable")
AtomicLong counter = map.getOrCreate(CLUSTER, EDS_SERVICE_NAME);
CounterReference ref = counters.get(CLUSTER).get(EDS_SERVICE_NAME);
final CounterReference ref = counters.get(CLUSTER).get(EDS_SERVICE_NAME);
counter = null;
GcFinalization.awaitClear(ref);
GcFinalization.awaitDone(new FinalizationPredicate() {
@Override
public boolean isDone() {
return ref.isEnqueued();
}
});
map.cleanQueue();
assertThat(counters).isEmpty();
}