Skip to content
This repository was archived by the owner on May 8, 2026. It is now read-only.

Commit 76b1c3e

Browse files
committed
consolidate channel pool copying into BigtableChannelPoolSettings, refine test
Change-Id: I90bb9d20a10e9b930507bf260b8bb77e2628abe2
1 parent 542de27 commit 76b1c3e

3 files changed

Lines changed: 41 additions & 41 deletions

File tree

google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/gaxx/grpc/BigtableChannelPoolSettings.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
import com.google.api.core.BetaApi;
1919
import com.google.auto.value.AutoValue;
20+
import com.google.api.gax.grpc.ChannelPoolSettings;
21+
import com.google.common.annotations.VisibleForTesting;
2022
import com.google.common.base.Preconditions;
2123
import java.time.Duration;
2224

@@ -112,6 +114,17 @@ boolean isStaticSize() {
112114

113115
public abstract Builder toBuilder();
114116

117+
public static BigtableChannelPoolSettings copyFrom(ChannelPoolSettings externalSettings) {
118+
return BigtableChannelPoolSettings.builder()
119+
.setMinRpcsPerChannel(externalSettings.getMinRpcsPerChannel())
120+
.setMaxRpcsPerChannel(externalSettings.getMaxRpcsPerChannel())
121+
.setMinChannelCount(externalSettings.getMinChannelCount())
122+
.setMaxChannelCount(externalSettings.getMaxChannelCount())
123+
.setInitialChannelCount(externalSettings.getInitialChannelCount())
124+
.setPreemptiveRefreshEnabled(externalSettings.isPreemptiveRefreshEnabled())
125+
.build();
126+
}
127+
115128
public static BigtableChannelPoolSettings staticallySized(int size) {
116129
return builder()
117130
.setInitialChannelCount(size)

google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/gaxx/grpc/BigtableTransportChannelProvider.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@
2323
import com.google.api.gax.rpc.TransportChannel;
2424
import com.google.api.gax.rpc.TransportChannelProvider;
2525
import com.google.auth.Credentials;
26-
import com.google.cloud.bigtable.gaxx.utils.ChannelPoolSettingsCopier;
27-
import com.google.common.annotations.VisibleForTesting;
2826
import com.google.common.base.Preconditions;
2927
import io.grpc.ManagedChannel;
3028
import java.io.IOException;
@@ -134,7 +132,7 @@ private TransportChannel createTransportChannel() throws IOException {
134132
}
135133
};
136134

137-
BigtableChannelPoolSettings btPoolSettings = ChannelPoolSettingsCopier.toBigtableChannelPoolSettings(delegate.getChannelPoolSettings());
135+
BigtableChannelPoolSettings btPoolSettings = BigtableChannelPoolSettings.copyFrom(delegate.getChannelPoolSettings());
138136

139137
BigtableChannelPool btChannelPool = BigtableChannelPool.create(btPoolSettings, channelFactory);
140138

google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/gaxx/utils/ChannelPoolCopyTest.java

Lines changed: 27 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
import com.google.api.gax.grpc.ChannelPoolSettings;
66
import com.google.cloud.bigtable.gaxx.grpc.BigtableChannelPoolSettings;
77
import java.lang.reflect.Modifier;
8-
import java.util.List;
8+
import java.util.Set;
9+
import com.google.common.collect.ImmutableSet;
910
import java.lang.reflect.Method;
1011
import java.util.Arrays;
1112
import java.util.stream.Collectors;
@@ -32,60 +33,48 @@ public void testToBigtableChannelPoolSettingsAllFieldsSetCopiesCorrectly() throw
3233
.setPreemptiveRefreshEnabled(true)
3334
.build();
3435

35-
BigtableChannelPoolSettings copiedSettings =
36-
ChannelPoolSettingsCopier.toBigtableChannelPoolSettings(originalSettings);
36+
BigtableChannelPoolSettings copiedSettings = BigtableChannelPoolSettings.copyFrom(originalSettings);
3737
assertSettingsCopiedCorrectly(originalSettings, copiedSettings);
3838
}
3939

4040
@Test
4141
public void testToBigtableChannelPoolSettingsDefaultValuesCopiesCorrectly() throws Exception {
42-
// 1. Arrange
4342
ChannelPoolSettings originalSettings = ChannelPoolSettings.builder().build();
44-
45-
// 2. Act
46-
BigtableChannelPoolSettings copiedSettings =
47-
ChannelPoolSettingsCopier.toBigtableChannelPoolSettings(originalSettings);
48-
49-
// 3. Assert
43+
BigtableChannelPoolSettings copiedSettings = BigtableChannelPoolSettings.copyFrom(originalSettings);
5044
assertSettingsCopiedCorrectly(originalSettings, copiedSettings);
5145
}
5246

53-
@Test
54-
public void testToBigtableChannelPoolSettingsNullInputReturnsNull() {
55-
ChannelPoolSettings originalSettings = null;
56-
BigtableChannelPoolSettings copiedSettings =
57-
ChannelPoolSettingsCopier.toBigtableChannelPoolSettings(originalSettings);
58-
assertThat(copiedSettings).isNull();
59-
}
47+
// @Test
48+
// public void testToBigtableChannelPoolSettingsNullInputReturnsNull() {
49+
// ChannelPoolSettings originalSettings = null;
50+
// BigtableChannelPoolSettings copiedSettings =
51+
// ChannelPoolSettingsCopier.toBigtableChannelPoolSettings(originalSettings);
52+
// assertThat(copiedSettings).isNull();
53+
// }
6054

6155
private void assertSettingsCopiedCorrectly(
6256
ChannelPoolSettings originalSettings, BigtableChannelPoolSettings copiedSettings)
6357
throws Exception {
64-
Class<ChannelPoolSettings> originalSettingsClass = ChannelPoolSettings.class;
6558

66-
// Get all public abstract methods from ChannelPoolSettings that start with "get" or "is"
67-
// and exclude methods from Object.class
68-
List<Method> originalPoolGetters = Arrays.stream(ChannelPoolSettings.class.getMethods())
59+
Set<String> supportedGetters = ImmutableSet.of("getMinRpcsPerChannel", "getMaxRpcsPerChannel", "getMinChannelCount", "getMaxChannelCount", "getInitialChannelCount", "isPreemptiveRefreshEnabled", "isStaticSize");
60+
61+
Set<String> actualGetters = Arrays.stream(ChannelPoolSettings.class.getDeclaredMethods())
6962
.filter(method -> Modifier.isPublic(method.getModifiers())
7063
&& Modifier.isAbstract(method.getModifiers())
71-
&& (method.getName().startsWith("get") || method.getName().startsWith("is"))
72-
&& method.getDeclaringClass() != Object.class) // Filter out java.lang.Object.getClass()
73-
.collect(Collectors.toList());
64+
&& (method.getName().startsWith("get") || method.getName().startsWith("is")))
65+
.map(Method::getName)
66+
.collect(Collectors.toSet());
7467

75-
for (Method getterMethod : originalPoolGetters) {
76-
try {
77-
Object originalValue = getterMethod.invoke(originalSettings);
78-
Method correspondingMethod = BigtableChannelPoolSettings.class.getMethod(getterMethod.getName());
79-
Object copiedValue = correspondingMethod.invoke(copiedSettings);
68+
// If this fails then we need to add support for the additional attributes on the gax ChannelPool
69+
// Relevant things to update the copier and the other tests in this file
70+
assertThat(supportedGetters).containsAtLeastElementsIn(actualGetters);
8071

81-
assertThat(copiedValue).isEqualTo(originalValue);
82-
} catch (ReflectiveOperationException e) {
83-
throw new AssertionError(
84-
String.format("Reflection error accessing method '%s': %s", getterMethod.getName(), e.getMessage()), e);
85-
} catch (Exception e) { // Catch any other unexpected exceptions
86-
throw new Exception(
87-
String.format("Unexpected error during comparison for method '%s': %s", getterMethod.getName(), e.getMessage()), e);
88-
}
89-
}
72+
assertThat(originalSettings.getInitialChannelCount()).isEqualTo(copiedSettings.getInitialChannelCount());
73+
assertThat(originalSettings.getMaxChannelCount()).isEqualTo(copiedSettings.getMaxChannelCount());
74+
assertThat(originalSettings.getMinChannelCount()).isEqualTo(copiedSettings.getMinChannelCount());
75+
assertThat(originalSettings.getMaxRpcsPerChannel()).isEqualTo(copiedSettings.getMaxRpcsPerChannel());
76+
assertThat(originalSettings.getMinRpcsPerChannel()).isEqualTo(copiedSettings.getMinRpcsPerChannel());
77+
assertThat(originalSettings.getInitialChannelCount()).isEqualTo(copiedSettings.getInitialChannelCount());
78+
assertThat(originalSettings.isPreemptiveRefreshEnabled()).isEqualTo(copiedSettings.isPreemptiveRefreshEnabled());
9079
}
9180
}

0 commit comments

Comments
 (0)