Skip to content

Commit

Permalink
chore: Refactor the tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lqiu96 committed Apr 18, 2024
1 parent 457fb36 commit a1703eb
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -537,12 +537,13 @@ public static final class Builder {
@Nullable private Boolean attemptDirectPathXds;
@Nullable private Boolean allowNonDefaultServiceAccount;
@Nullable private ImmutableMap<String, ?> 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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}
Expand All @@ -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();
Expand Down Expand Up @@ -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();
Expand All @@ -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();
Expand All @@ -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();
}
Expand All @@ -690,14 +691,15 @@ public void canUseDirectPath_directPathEnvVarDisabled() {
.setAttemptDirectPath(true)
.setCredentials(computeEngineCredentials)
.setSystemProductNameReader(systemProductNameReader)
.setEndpoint("test.googleapis.com:443")
.setEndpoint(DEFAULT_ENDPOINT)
.build();
Truth.assertThat(provider.canUseDirectPath()).isFalse();
}

@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))
Expand All @@ -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();
}
Expand All @@ -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();
}
Expand All @@ -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();
}
Expand All @@ -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();
}
Expand All @@ -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();
}
Expand All @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, Object> attributeEntrySet : attributes.entrySet()) {
metricsTracer.addAttributes(attributeEntrySet.getKey(), String.valueOf(attributeEntrySet.getValue()));
metricsTracer.addAttributes(
attributeEntrySet.getKey(), String.valueOf(attributeEntrySet.getValue()));
}
return metricsTracer;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, Object> customAttributes = new HashMap<>();
customAttributes.put("directpath_enabled", channelProvider.canUseDirectPath());
customAttributes.put("testing", "showcase");
Expand Down

0 comments on commit a1703eb

Please sign in to comment.