services: added even more (context-related) test cases for CallMetricRecorder (#6033)

This commit is contained in:
Chengyuan Zhang 2019-08-01 10:09:26 -07:00 committed by GitHub
parent f9509694b9
commit b8933faae1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 0 deletions

View File

@ -16,6 +16,7 @@
package io.grpc.services;
import com.google.common.annotations.VisibleForTesting;
import io.grpc.Context;
import io.grpc.ExperimentalApi;
import java.util.Collections;
@ -96,6 +97,11 @@ public final class CallMetricRecorder {
return Collections.unmodifiableMap(savedMetrics);
}
@VisibleForTesting
boolean isDisabled() {
return disabled;
}
/**
* Turn this recorder into a no-op one.
*/

View File

@ -18,6 +18,7 @@ package io.grpc.services;
import static com.google.common.truth.Truth.assertThat;
import io.grpc.Context;
import java.util.Map;
import org.junit.Test;
import org.junit.runner.RunWith;
@ -63,4 +64,28 @@ public class CallMetricRecorderTest {
assertThat(dump)
.containsExactly("cost1", 4654.67, "cost2", 75.83);
}
@Test
public void getCurrent_sameEnabledInstance() {
CallMetricRecorder recorder = new CallMetricRecorder();
Context ctx = Context.ROOT.withValue(CallMetricRecorder.CONTEXT_KEY, recorder);
Context origCtx = ctx.attach();
try {
assertThat(CallMetricRecorder.getCurrent()).isSameInstanceAs(recorder);
assertThat(recorder.isDisabled()).isFalse();
} finally {
ctx.detach(origCtx);
}
}
@Test
public void getCurrent_blankContext() {
Context blankCtx = Context.ROOT;
Context origCtx = blankCtx.attach();
try {
assertThat(CallMetricRecorder.getCurrent().isDisabled()).isTrue();
} finally {
blankCtx.detach(origCtx);
}
}
}