From 1f1dcb37bbc8d7dd338662b717731a41b68383e3 Mon Sep 17 00:00:00 2001 From: Mike Prieto Date: Mon, 11 May 2026 15:25:17 +0000 Subject: [PATCH 1/4] test: Add integration test for UsePubSubEmulatorExample --- ...ub-integration-tests-against-emulator.yaml | 59 +++++++++++++++++ .../pubsub/UsePubSubEmulatorExampleIT.java | 63 +++++++++++++++++++ 2 files changed, 122 insertions(+) create mode 100644 .github/workflows/java-pubsub-integration-tests-against-emulator.yaml create mode 100644 java-pubsub/samples/snippets/src/test/java/pubsub/UsePubSubEmulatorExampleIT.java diff --git a/.github/workflows/java-pubsub-integration-tests-against-emulator.yaml b/.github/workflows/java-pubsub-integration-tests-against-emulator.yaml new file mode 100644 index 000000000000..43a7b3d85988 --- /dev/null +++ b/.github/workflows/java-pubsub-integration-tests-against-emulator.yaml @@ -0,0 +1,59 @@ +# Copyright 2026 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +on: + push: + branches: + - main + pull_request: +name: java-pubsub integration-tests-against-emulator +jobs: + filter: + runs-on: ubuntu-latest + outputs: + library: ${{ steps.filter.outputs.library }} + steps: + - uses: actions/checkout@v4 + - uses: dorny/paths-filter@v3 + id: filter + with: + filters: | + library: + - 'java-pubsub/**' + integration-tests: + needs: filter + if: ${{ needs.filter.outputs.library == 'true' }} + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v4 + - uses: actions/setup-java@v4 + with: + distribution: temurin + java-version: 11 + + - name: Start Pub/Sub Emulator + run: | + docker run -d -p 8085:8085 --name pubsub-emulator gcr.io/google.com/cloudsdktool/cloud-sdk:433.0.1-emulators gcloud beta emulators pubsub start --project=abc --host-port=0.0.0.0:8085 + echo "Waiting for emulator to start..." + sleep 10 + + - name: Running tests + run: .kokoro/build.sh + env: + JOB_TYPE: test + BUILD_SUBDIR: java-pubsub + PUBSUB_EMULATOR_HOST: localhost:8085 + GOOGLE_CLOUD_PROJECT: abc + SUREFIRE_JVM_OPT: '-Dtest=UsePubSubEmulatorExampleIT' diff --git a/java-pubsub/samples/snippets/src/test/java/pubsub/UsePubSubEmulatorExampleIT.java b/java-pubsub/samples/snippets/src/test/java/pubsub/UsePubSubEmulatorExampleIT.java new file mode 100644 index 000000000000..f40966e5bcf9 --- /dev/null +++ b/java-pubsub/samples/snippets/src/test/java/pubsub/UsePubSubEmulatorExampleIT.java @@ -0,0 +1,63 @@ +/* + * Copyright 2026 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package pubsub; + +import static com.google.common.truth.Truth.assertThat; +import static junit.framework.TestCase.assertNotNull; + +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import org.junit.After; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +public class UsePubSubEmulatorExampleIT { + private ByteArrayOutputStream bout; + private PrintStream out; + + private static void requireEnvVar(String varName) { + assertNotNull( + "Environment variable " + varName + " is required to perform these tests.", + System.getenv(varName)); + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("PUBSUB_EMULATOR_HOST"); + } + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + System.setOut(out); + } + + @After + public void tearDown() { + System.setOut(null); + } + + @Test + public void testUsePubSubEmulatorExample() throws Exception { + UsePubSubEmulatorExample.main(); + String output = bout.toString(); + assertThat(output).contains("Created topic: "); + assertThat(output).contains("Published message ID: "); + } +} From 598d82945682cae5aa0314cb6b470a6f6878c6ca Mon Sep 17 00:00:00 2001 From: Mike Prieto Date: Mon, 11 May 2026 15:30:32 +0000 Subject: [PATCH 2/4] test: Update Pub/Sub emulator integration test configuration --- ...ub-integration-tests-against-emulator.yaml | 21 +++++++++---------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/.github/workflows/java-pubsub-integration-tests-against-emulator.yaml b/.github/workflows/java-pubsub-integration-tests-against-emulator.yaml index 43a7b3d85988..854ccadd71bc 100644 --- a/.github/workflows/java-pubsub-integration-tests-against-emulator.yaml +++ b/.github/workflows/java-pubsub-integration-tests-against-emulator.yaml @@ -24,8 +24,8 @@ jobs: outputs: library: ${{ steps.filter.outputs.library }} steps: - - uses: actions/checkout@v4 - - uses: dorny/paths-filter@v3 + - uses: actions/checkout@v6 + - uses: dorny/paths-filter@v4 id: filter with: filters: | @@ -37,17 +37,16 @@ jobs: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v4 - - uses: actions/setup-java@v4 + - uses: actions/checkout@v6 + - uses: actions/setup-java@v5 with: distribution: temurin - java-version: 11 - - - name: Start Pub/Sub Emulator - run: | - docker run -d -p 8085:8085 --name pubsub-emulator gcr.io/google.com/cloudsdktool/cloud-sdk:433.0.1-emulators gcloud beta emulators pubsub start --project=abc --host-port=0.0.0.0:8085 - echo "Waiting for emulator to start..." - sleep 10 + java-version: 17 + - name: gcr.io/google.com/cloudsdktool/cloud-sdk:567.0.0-emulators + entrypoint: 'gcloud' + args: ['beta', 'emulators', 'pubsub', 'start', '--project=abc', '--host-port=0.0.0.0:8085'] + env: + - 'PUBSUB_EMULATOR_HOST=localhost:8085' - name: Running tests run: .kokoro/build.sh From 1a08618be2c7575e216156ee3d3021713662b48d Mon Sep 17 00:00:00 2001 From: Mike Prieto Date: Mon, 11 May 2026 16:35:23 +0000 Subject: [PATCH 3/4] test: Make updates to Pub/Sub emulator integration test --- ...va-pubsub-integration-tests-against-emulator.yaml | 10 +++++----- .../test/java/pubsub/UsePubSubEmulatorExampleIT.java | 12 +++++++----- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/.github/workflows/java-pubsub-integration-tests-against-emulator.yaml b/.github/workflows/java-pubsub-integration-tests-against-emulator.yaml index 854ccadd71bc..b4a93f932e79 100644 --- a/.github/workflows/java-pubsub-integration-tests-against-emulator.yaml +++ b/.github/workflows/java-pubsub-integration-tests-against-emulator.yaml @@ -42,11 +42,11 @@ jobs: with: distribution: temurin java-version: 17 - - name: gcr.io/google.com/cloudsdktool/cloud-sdk:567.0.0-emulators - entrypoint: 'gcloud' - args: ['beta', 'emulators', 'pubsub', 'start', '--project=abc', '--host-port=0.0.0.0:8085'] - env: - - 'PUBSUB_EMULATOR_HOST=localhost:8085' + - name: Start Pub/Sub Emulator + run: | + docker run -d -p 8085:8085 --name pubsub-emulator gcr.io/google.com/cloudsdktool/cloud-sdk:567.0.0-emulators gcloud beta emulators pubsub start --project=abc --host-port=0.0.0.0:8085 + echo "Waiting for emulator to start..." + sleep 10 - name: Running tests run: .kokoro/build.sh diff --git a/java-pubsub/samples/snippets/src/test/java/pubsub/UsePubSubEmulatorExampleIT.java b/java-pubsub/samples/snippets/src/test/java/pubsub/UsePubSubEmulatorExampleIT.java index f40966e5bcf9..f19f9ae2979e 100644 --- a/java-pubsub/samples/snippets/src/test/java/pubsub/UsePubSubEmulatorExampleIT.java +++ b/java-pubsub/samples/snippets/src/test/java/pubsub/UsePubSubEmulatorExampleIT.java @@ -17,7 +17,7 @@ package pubsub; import static com.google.common.truth.Truth.assertThat; -import static junit.framework.TestCase.assertNotNull; +import static com.google.common.truth.Truth.assertWithMessage; import java.io.ByteArrayOutputStream; import java.io.PrintStream; @@ -29,11 +29,12 @@ public class UsePubSubEmulatorExampleIT { private ByteArrayOutputStream bout; private PrintStream out; + private PrintStream originalOut; private static void requireEnvVar(String varName) { - assertNotNull( - "Environment variable " + varName + " is required to perform these tests.", - System.getenv(varName)); + assertWithMessage("Environment variable " + varName + " is required to perform these tests.") + .that(System.getenv(varName)) + .isNotNull(); } @BeforeClass @@ -43,6 +44,7 @@ public static void checkRequirements() { @Before public void setUp() { + originalOut = System.out; bout = new ByteArrayOutputStream(); out = new PrintStream(bout); System.setOut(out); @@ -50,7 +52,7 @@ public void setUp() { @After public void tearDown() { - System.setOut(null); + System.setOut(originalOut); } @Test From 16391906c9206ba8e39b073b39a136445eda3f98 Mon Sep 17 00:00:00 2001 From: Mike Prieto Date: Mon, 11 May 2026 16:48:38 +0000 Subject: [PATCH 4/4] test: Skip Pub/Sub emulator test when not present in a module --- .../java-pubsub-integration-tests-against-emulator.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/java-pubsub-integration-tests-against-emulator.yaml b/.github/workflows/java-pubsub-integration-tests-against-emulator.yaml index b4a93f932e79..702d00f85212 100644 --- a/.github/workflows/java-pubsub-integration-tests-against-emulator.yaml +++ b/.github/workflows/java-pubsub-integration-tests-against-emulator.yaml @@ -55,4 +55,4 @@ jobs: BUILD_SUBDIR: java-pubsub PUBSUB_EMULATOR_HOST: localhost:8085 GOOGLE_CLOUD_PROJECT: abc - SUREFIRE_JVM_OPT: '-Dtest=UsePubSubEmulatorExampleIT' + SUREFIRE_JVM_OPT: '-Dtest=UsePubSubEmulatorExampleIT -Dsurefire.failIfNoSpecifiedTests=false'