Skip to content

Commit 5068c3f

Browse files
committed
test(showcase): clean up background resources in ITGdch to prevent gRPC channel leak
1 parent 1eb6aa3 commit 5068c3f

3 files changed

Lines changed: 60 additions & 9 deletions

File tree

  • java-showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it
  • sdk-platform-java
    • java-showcase-3.21.0/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it
    • java-showcase-3.25.8/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it

java-showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITGdch.java

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import com.google.api.client.json.GenericJson;
2727
import com.google.api.client.json.JsonFactory;
2828
import com.google.api.client.json.gson.GsonFactory;
29+
import com.google.api.gax.core.BackgroundResource;
2930
import com.google.api.gax.core.FixedCredentialsProvider;
3031
import com.google.api.gax.rpc.ClientContext;
3132
import com.google.auth.Credentials;
@@ -85,9 +86,25 @@ void setup(@TempDir Path tempDir) throws IOException {
8586

8687
@AfterEach
8788
void tearDown() throws InterruptedException {
88-
if (client != null) {
89-
client.close();
90-
client.awaitTermination(TestClientInitializer.AWAIT_TERMINATION_SECONDS, TimeUnit.SECONDS);
89+
try {
90+
if (client != null) {
91+
client.close();
92+
client.awaitTermination(TestClientInitializer.AWAIT_TERMINATION_SECONDS, TimeUnit.SECONDS);
93+
}
94+
} finally {
95+
if (context != null) {
96+
// EchoStubSettings.newBuilder(context) configures a FixedTransportChannelProvider using the
97+
// channel from ClientContext. FixedTransportChannelProvider disables auto-closing
98+
// (shouldAutoClose() == false), so closing the client does not close the channel. We must
99+
// manually shut down the background resources owned by ClientContext.
100+
for (BackgroundResource resource : context.getBackgroundResources()) {
101+
resource.shutdown();
102+
}
103+
for (BackgroundResource resource : context.getBackgroundResources()) {
104+
resource.awaitTermination(
105+
TestClientInitializer.AWAIT_TERMINATION_SECONDS, TimeUnit.SECONDS);
106+
}
107+
}
91108
}
92109
}
93110

sdk-platform-java/java-showcase-3.21.0/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITGdch.java

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import com.google.api.client.json.GenericJson;
2727
import com.google.api.client.json.JsonFactory;
2828
import com.google.api.client.json.gson.GsonFactory;
29+
import com.google.api.gax.core.BackgroundResource;
2930
import com.google.api.gax.core.FixedCredentialsProvider;
3031
import com.google.api.gax.rpc.ClientContext;
3132
import com.google.auth.Credentials;
@@ -85,9 +86,25 @@ void setup(@TempDir Path tempDir) throws IOException {
8586

8687
@AfterEach
8788
void tearDown() throws InterruptedException {
88-
if (client != null) {
89-
client.close();
90-
client.awaitTermination(TestClientInitializer.AWAIT_TERMINATION_SECONDS, TimeUnit.SECONDS);
89+
try {
90+
if (client != null) {
91+
client.close();
92+
client.awaitTermination(TestClientInitializer.AWAIT_TERMINATION_SECONDS, TimeUnit.SECONDS);
93+
}
94+
} finally {
95+
if (context != null) {
96+
// EchoStubSettings.newBuilder(context) configures a FixedTransportChannelProvider using the
97+
// channel from ClientContext. FixedTransportChannelProvider disables auto-closing
98+
// (shouldAutoClose() == false), so closing the client does not close the channel. We must
99+
// manually shut down the background resources owned by ClientContext.
100+
for (BackgroundResource resource : context.getBackgroundResources()) {
101+
resource.shutdown();
102+
}
103+
for (BackgroundResource resource : context.getBackgroundResources()) {
104+
resource.awaitTermination(
105+
TestClientInitializer.AWAIT_TERMINATION_SECONDS, TimeUnit.SECONDS);
106+
}
107+
}
91108
}
92109
}
93110

sdk-platform-java/java-showcase-3.25.8/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITGdch.java

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import com.google.api.client.json.GenericJson;
2727
import com.google.api.client.json.JsonFactory;
2828
import com.google.api.client.json.gson.GsonFactory;
29+
import com.google.api.gax.core.BackgroundResource;
2930
import com.google.api.gax.core.FixedCredentialsProvider;
3031
import com.google.api.gax.rpc.ClientContext;
3132
import com.google.auth.Credentials;
@@ -85,9 +86,25 @@ void setup(@TempDir Path tempDir) throws IOException {
8586

8687
@AfterEach
8788
void tearDown() throws InterruptedException {
88-
if (client != null) {
89-
client.close();
90-
client.awaitTermination(TestClientInitializer.AWAIT_TERMINATION_SECONDS, TimeUnit.SECONDS);
89+
try {
90+
if (client != null) {
91+
client.close();
92+
client.awaitTermination(TestClientInitializer.AWAIT_TERMINATION_SECONDS, TimeUnit.SECONDS);
93+
}
94+
} finally {
95+
if (context != null) {
96+
// EchoStubSettings.newBuilder(context) configures a FixedTransportChannelProvider using the
97+
// channel from ClientContext. FixedTransportChannelProvider disables auto-closing
98+
// (shouldAutoClose() == false), so closing the client does not close the channel. We must
99+
// manually shut down the background resources owned by ClientContext.
100+
for (BackgroundResource resource : context.getBackgroundResources()) {
101+
resource.shutdown();
102+
}
103+
for (BackgroundResource resource : context.getBackgroundResources()) {
104+
resource.awaitTermination(
105+
TestClientInitializer.AWAIT_TERMINATION_SECONDS, TimeUnit.SECONDS);
106+
}
107+
}
91108
}
92109
}
93110

0 commit comments

Comments
 (0)