From a1703ebd48502ca494b83ec70b54a4ac3676e412 Mon Sep 17 00:00:00 2001 From: Lawrence Qiu Date: Wed, 17 Apr 2024 22:52:19 -0400 Subject: [PATCH] chore: Refactor the tests --- .../InstantiatingGrpcChannelProvider.java | 3 ++- .../InstantiatingGrpcChannelProviderTest.java | 26 ++++++++++--------- .../api/gax/tracing/MetricsTracerFactory.java | 3 ++- .../showcase/v1beta1/it/ITOtelMetrics.java | 1 + 4 files changed, 19 insertions(+), 14 deletions(-) diff --git a/gax-java/gax-grpc/src/main/java/com/google/api/gax/grpc/InstantiatingGrpcChannelProvider.java b/gax-java/gax-grpc/src/main/java/com/google/api/gax/grpc/InstantiatingGrpcChannelProvider.java index 369a25b5a3..24769d8922 100644 --- a/gax-java/gax-grpc/src/main/java/com/google/api/gax/grpc/InstantiatingGrpcChannelProvider.java +++ b/gax-java/gax-grpc/src/main/java/com/google/api/gax/grpc/InstantiatingGrpcChannelProvider.java @@ -537,12 +537,13 @@ public static final class Builder { @Nullable private Boolean attemptDirectPathXds; @Nullable private Boolean allowNonDefaultServiceAccount; @Nullable private ImmutableMap directPathServiceConfig; - private SystemProductNameReader systemProductNameReader = new SystemProductNameReader(); + private SystemProductNameReader systemProductNameReader; private Builder() { processorCount = Runtime.getRuntime().availableProcessors(); envProvider = System::getenv; channelPoolSettings = ChannelPoolSettings.staticallySized(1); + systemProductNameReader = new SystemProductNameReader(); } private Builder(InstantiatingGrpcChannelProvider provider) { diff --git a/gax-java/gax-grpc/src/test/java/com/google/api/gax/grpc/InstantiatingGrpcChannelProviderTest.java b/gax-java/gax-grpc/src/test/java/com/google/api/gax/grpc/InstantiatingGrpcChannelProviderTest.java index f5f59ddb47..62b9d75a60 100644 --- a/gax-java/gax-grpc/src/test/java/com/google/api/gax/grpc/InstantiatingGrpcChannelProviderTest.java +++ b/gax-java/gax-grpc/src/test/java/com/google/api/gax/grpc/InstantiatingGrpcChannelProviderTest.java @@ -76,6 +76,7 @@ @RunWith(JUnit4.class) public class InstantiatingGrpcChannelProviderTest extends AbstractMtlsTransportChannelTest { + private static final String DEFAULT_ENDPOINT = "test.googleapis.com:443"; private String originalOSNameSystemProperty; private EnvironmentProvider envProvider; private ComputeEngineCredentials computeEngineCredentials; @@ -335,7 +336,7 @@ public void testDirectPathWithGDUEndpoint() { InstantiatingGrpcChannelProvider.newBuilder() .setAttemptDirectPath(true) .setAttemptDirectPathXds() - .setEndpoint("test.googleapis.com:443") + .setEndpoint(DEFAULT_ENDPOINT) .build(); assertThat(provider.canUseDirectPathWithUniverseDomain()).isTrue(); } @@ -357,7 +358,7 @@ public void testDirectPathXdsEnabled() throws IOException { InstantiatingGrpcChannelProvider.newBuilder() .setAttemptDirectPath(true) .setAttemptDirectPathXds() - .setEndpoint("test.googleapis.com:443") + .setEndpoint(DEFAULT_ENDPOINT) .build(); assertThat(provider.isDirectPathXdsEnabled()).isTrue(); @@ -622,7 +623,7 @@ public void testLogDirectPathMisconfigWrongCredential() throws Exception { .setAttemptDirectPath(true) .setHeaderProvider(Mockito.mock(HeaderProvider.class)) .setExecutor(Mockito.mock(Executor.class)) - .setEndpoint("test.googleapis.com:443") + .setEndpoint(DEFAULT_ENDPOINT) .build(); TransportChannel transportChannel = provider.getTransportChannel(); @@ -648,7 +649,7 @@ public void testLogDirectPathMisconfigNotOnGCE() throws Exception { .setAllowNonDefaultServiceAccount(true) .setHeaderProvider(Mockito.mock(HeaderProvider.class)) .setExecutor(Mockito.mock(Executor.class)) - .setEndpoint("test.googleapis.com:443") + .setEndpoint(DEFAULT_ENDPOINT) .build(); TransportChannel transportChannel = provider.getTransportChannel(); @@ -672,7 +673,7 @@ public void canUseDirectPath_happyPath() { .setAttemptDirectPath(true) .setCredentials(computeEngineCredentials) .setSystemProductNameReader(systemProductNameReader) - .setEndpoint("test.googleapis.com:443") + .setEndpoint(DEFAULT_ENDPOINT) .build(); Truth.assertThat(provider.canUseDirectPath()).isTrue(); } @@ -690,7 +691,7 @@ public void canUseDirectPath_directPathEnvVarDisabled() { .setAttemptDirectPath(true) .setCredentials(computeEngineCredentials) .setSystemProductNameReader(systemProductNameReader) - .setEndpoint("test.googleapis.com:443") + .setEndpoint(DEFAULT_ENDPOINT) .build(); Truth.assertThat(provider.canUseDirectPath()).isFalse(); } @@ -698,6 +699,7 @@ public void canUseDirectPath_directPathEnvVarDisabled() { @Test public void canUseDirectPath_directPathEnvVarNotSet_attemptDirectPathIsTrue() { EnvironmentProvider envProvider = Mockito.mock(EnvironmentProvider.class); + // If system property is not set, then System.getProperty() returns null Mockito.when( envProvider.getenv( InstantiatingGrpcChannelProvider.DIRECT_PATH_ENV_DISABLE_DIRECT_PATH)) @@ -708,7 +710,7 @@ public void canUseDirectPath_directPathEnvVarNotSet_attemptDirectPathIsTrue() { .setAttemptDirectPath(true) .setCredentials(computeEngineCredentials) .setSystemProductNameReader(systemProductNameReader) - .setEndpoint("test.googleapis.com:443") + .setEndpoint(DEFAULT_ENDPOINT) .build(); Truth.assertThat(provider.canUseDirectPath()).isTrue(); } @@ -726,7 +728,7 @@ public void canUseDirectPath_directPathEnvVarNotSet_attemptDirectPathIsFalse() { .setAttemptDirectPath(false) .setCredentials(computeEngineCredentials) .setSystemProductNameReader(systemProductNameReader) - .setEndpoint("test.googleapis.com:443") + .setEndpoint(DEFAULT_ENDPOINT) .build(); Truth.assertThat(provider.canUseDirectPath()).isFalse(); } @@ -740,7 +742,7 @@ public void canUseDirectPath_nonComputeCredentials() { .setAttemptDirectPath(true) .setCredentials(credentials) .setSystemProductNameReader(systemProductNameReader) - .setEndpoint("test.googleapis.com:443") + .setEndpoint(DEFAULT_ENDPOINT) .build(); Truth.assertThat(provider.canUseDirectPath()).isFalse(); } @@ -756,7 +758,7 @@ public void canUseDirectPath_systemProductNameInvalid() throws IOException { .setAttemptDirectPath(true) .setCredentials(computeEngineCredentials) .setSystemProductNameReader(systemProductNameReader) - .setEndpoint("test.googleapis.com:443") + .setEndpoint(DEFAULT_ENDPOINT) .build(); Truth.assertThat(provider.canUseDirectPath()).isFalse(); } @@ -771,7 +773,7 @@ public void canUseDirectPath_systemPropertyIsNotLinux() throws IOException { .setAttemptDirectPath(true) .setCredentials(computeEngineCredentials) .setSystemProductNameReader(systemProductNameReader) - .setEndpoint("test.googleapis.com:443") + .setEndpoint(DEFAULT_ENDPOINT) .build(); Truth.assertThat(provider.canUseDirectPath()).isFalse(); } @@ -787,7 +789,7 @@ public void canUseDirectPath_systemProductNameThrowsIOException() throws IOExcep .setAttemptDirectPath(true) .setCredentials(computeEngineCredentials) .setSystemProductNameReader(systemProductNameReader) - .setEndpoint("test.googleapis.com:443") + .setEndpoint(DEFAULT_ENDPOINT) .build(); Truth.assertThat(provider.canUseDirectPath()).isFalse(); } diff --git a/gax-java/gax/src/main/java/com/google/api/gax/tracing/MetricsTracerFactory.java b/gax-java/gax/src/main/java/com/google/api/gax/tracing/MetricsTracerFactory.java index bce48eef3e..15fcbbd0bb 100644 --- a/gax-java/gax/src/main/java/com/google/api/gax/tracing/MetricsTracerFactory.java +++ b/gax-java/gax/src/main/java/com/google/api/gax/tracing/MetricsTracerFactory.java @@ -70,7 +70,8 @@ public ApiTracer newTracer(ApiTracer parent, SpanName spanName, OperationType op new MetricsTracer( MethodName.of(spanName.getClientName(), spanName.getMethodName()), metricsRecorder); for (Map.Entry attributeEntrySet : attributes.entrySet()) { - metricsTracer.addAttributes(attributeEntrySet.getKey(), String.valueOf(attributeEntrySet.getValue())); + metricsTracer.addAttributes( + attributeEntrySet.getKey(), String.valueOf(attributeEntrySet.getValue())); } return metricsTracer; } diff --git a/showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITOtelMetrics.java b/showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITOtelMetrics.java index b8684cb845..60a363c7ba 100644 --- a/showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITOtelMetrics.java +++ b/showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITOtelMetrics.java @@ -795,6 +795,7 @@ public void recordsCustomAttributes() throws InterruptedException, IOException { .setChannelConfigurator(ManagedChannelBuilder::usePlaintext) .build(); + // Add custom attributes to be added as client level attributes Map customAttributes = new HashMap<>(); customAttributes.put("directpath_enabled", channelProvider.canUseDirectPath()); customAttributes.put("testing", "showcase");