core: make StatsContextFactory setters protected (#2634)
These are only used in internal tests. In production,
StatsContextFactory is loaded by the "Instrumentation" library and must
be one per process, thus we won't allow setting it on a per-channel or
per-server basis.
diff --git a/core/src/main/java/io/grpc/internal/AbstractManagedChannelImplBuilder.java b/core/src/main/java/io/grpc/internal/AbstractManagedChannelImplBuilder.java
index 6d5578b..cca6c55 100644
--- a/core/src/main/java/io/grpc/internal/AbstractManagedChannelImplBuilder.java
+++ b/core/src/main/java/io/grpc/internal/AbstractManagedChannelImplBuilder.java
@@ -44,7 +44,6 @@
import io.grpc.ClientInterceptor;
import io.grpc.CompressorRegistry;
import io.grpc.DecompressorRegistry;
-import io.grpc.Internal;
import io.grpc.LoadBalancer;
import io.grpc.LoadBalancer2;
import io.grpc.ManagedChannel;
@@ -259,11 +258,10 @@
}
/**
- * Override the default stats implementation. This is meant to be used in tests.
+ * Override the default stats implementation.
*/
@VisibleForTesting
- @Internal
- public T statsContextFactory(StatsContextFactory statsFactory) {
+ protected T statsContextFactory(StatsContextFactory statsFactory) {
this.statsFactory = statsFactory;
return thisT();
}
diff --git a/core/src/main/java/io/grpc/internal/AbstractServerImplBuilder.java b/core/src/main/java/io/grpc/internal/AbstractServerImplBuilder.java
index 78f2ca0..5cc4910 100644
--- a/core/src/main/java/io/grpc/internal/AbstractServerImplBuilder.java
+++ b/core/src/main/java/io/grpc/internal/AbstractServerImplBuilder.java
@@ -153,11 +153,10 @@
}
/**
- * Override the default stats implementation. This is meant to be used in tests.
+ * Override the default stats implementation.
*/
@VisibleForTesting
- @Internal
- public T statsContextFactory(StatsContextFactory statsFactory) {
+ protected T statsContextFactory(StatsContextFactory statsFactory) {
this.statsFactory = statsFactory;
return thisT();
}
diff --git a/core/src/test/java/io/grpc/internal/TestUtils.java b/core/src/test/java/io/grpc/internal/TestUtils.java
index 7e0f57a..1ca0660 100644
--- a/core/src/test/java/io/grpc/internal/TestUtils.java
+++ b/core/src/test/java/io/grpc/internal/TestUtils.java
@@ -36,6 +36,8 @@
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
+import com.google.instrumentation.stats.StatsContextFactory;
+
import io.grpc.CallOptions;
import io.grpc.Metadata;
import io.grpc.MethodDescriptor;
@@ -51,7 +53,7 @@
/**
* Common utility methods for tests.
*/
-final class TestUtils {
+public final class TestUtils {
static class MockClientTransportInfo {
/**
@@ -110,4 +112,23 @@
return captor;
}
+
+ /**
+ * Sets a custom {@link StatsContextFactory} for tests.
+ */
+ public static void setStatsContextFactory(
+ AbstractManagedChannelImplBuilder<?> builder, StatsContextFactory factory) {
+ builder.statsContextFactory(factory);
+ }
+
+ /**
+ * Sets a custom {@link StatsContextFactory} for tests.
+ */
+ public static void setStatsContextFactory(
+ AbstractServerImplBuilder<?> builder, StatsContextFactory factory) {
+ builder.statsContextFactory(factory);
+ }
+
+ private TestUtils() {
+ }
}