mirror of https://github.com/grpc/grpc-java.git
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:
parent
c850840342
commit
f27a8f26a8
|
|
@ -19,6 +19,7 @@ package io.grpc.xds;
|
||||||
import static com.google.common.truth.Truth.assertThat;
|
import static com.google.common.truth.Truth.assertThat;
|
||||||
|
|
||||||
import com.google.common.testing.GcFinalization;
|
import com.google.common.testing.GcFinalization;
|
||||||
|
import com.google.common.testing.GcFinalization.FinalizationPredicate;
|
||||||
import io.grpc.xds.SharedCallCounterMap.CounterReference;
|
import io.grpc.xds.SharedCallCounterMap.CounterReference;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
@ -50,9 +51,14 @@ public class SharedCallCounterMapTest {
|
||||||
public void autoCleanUp() {
|
public void autoCleanUp() {
|
||||||
@SuppressWarnings("UnusedVariable")
|
@SuppressWarnings("UnusedVariable")
|
||||||
AtomicLong counter = map.getOrCreate(CLUSTER, EDS_SERVICE_NAME);
|
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;
|
counter = null;
|
||||||
GcFinalization.awaitClear(ref);
|
GcFinalization.awaitDone(new FinalizationPredicate() {
|
||||||
|
@Override
|
||||||
|
public boolean isDone() {
|
||||||
|
return ref.isEnqueued();
|
||||||
|
}
|
||||||
|
});
|
||||||
map.cleanQueue();
|
map.cleanQueue();
|
||||||
assertThat(counters).isEmpty();
|
assertThat(counters).isEmpty();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue