-
Notifications
You must be signed in to change notification settings - Fork 1.1k
test: Add integration test for UsePubSubEmulatorExample #13159
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
1f1dcb3
test: Add integration test for UsePubSubEmulatorExample
michaelpri10 598d829
test: Update Pub/Sub emulator integration test configuration
michaelpri10 7021a94
Merge branch 'googleapis:main' into main
michaelpri10 1a08618
test: Make updates to Pub/Sub emulator integration test
michaelpri10 1639190
test: Skip Pub/Sub emulator test when not present in a module
michaelpri10 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
58 changes: 58 additions & 0 deletions
58
.github/workflows/java-pubsub-integration-tests-against-emulator.yaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| # 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@v6 | ||
| - uses: dorny/paths-filter@v4 | ||
| 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@v6 | ||
| - uses: actions/setup-java@v5 | ||
| with: | ||
| distribution: temurin | ||
| java-version: 17 | ||
| - 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 | ||
| env: | ||
| JOB_TYPE: test | ||
| BUILD_SUBDIR: java-pubsub | ||
| PUBSUB_EMULATOR_HOST: localhost:8085 | ||
| GOOGLE_CLOUD_PROJECT: abc | ||
| SUREFIRE_JVM_OPT: '-Dtest=UsePubSubEmulatorExampleIT -Dsurefire.failIfNoSpecifiedTests=false' |
65 changes: 65 additions & 0 deletions
65
java-pubsub/samples/snippets/src/test/java/pubsub/UsePubSubEmulatorExampleIT.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| /* | ||
| * 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 com.google.common.truth.Truth.assertWithMessage; | ||
|
|
||
| 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 PrintStream originalOut; | ||
|
|
||
| private static void requireEnvVar(String varName) { | ||
| assertWithMessage("Environment variable " + varName + " is required to perform these tests.") | ||
| .that(System.getenv(varName)) | ||
| .isNotNull(); | ||
| } | ||
|
|
||
| @BeforeClass | ||
| public static void checkRequirements() { | ||
| requireEnvVar("PUBSUB_EMULATOR_HOST"); | ||
| } | ||
|
|
||
| @Before | ||
| public void setUp() { | ||
| originalOut = System.out; | ||
| bout = new ByteArrayOutputStream(); | ||
| out = new PrintStream(bout); | ||
| System.setOut(out); | ||
| } | ||
|
|
||
| @After | ||
| public void tearDown() { | ||
| System.setOut(originalOut); | ||
| } | ||
|
|
||
| @Test | ||
| public void testUsePubSubEmulatorExample() throws Exception { | ||
| UsePubSubEmulatorExample.main(); | ||
| String output = bout.toString(); | ||
| assertThat(output).contains("Created topic: "); | ||
| assertThat(output).contains("Published message ID: "); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Setting
System.outtonullin thetearDownmethod will cause aNullPointerExceptionfor any subsequent code (including other tests in the same JVM) that attempts to use the standard output stream. It is best practice to capture the originalPrintStreamin thesetUpmethod and restore it in thetearDownmethod. Additionally, therequireEnvVarmethod has been updated to use Truth assertions for consistency.