From 1ebc2fe2f417a5bef3ec075bb016a3899ca46b5c Mon Sep 17 00:00:00 2001 From: Blake Li Date: Thu, 11 Dec 2025 10:59:31 -0500 Subject: [PATCH] tests: Add protobuf 3.x compatibility tests with a third party library tensorflow. (#4015) Add protobuf 3.x compatibility tests with a third party library tensorflow in showcase module. Tensorflow depends on [protobuf-java v3.21.9](https://github.com/tensorflow/java/blob/cbf942051d55291ba9bdb019b2f207f013889bba/tensorflow-core/pom.xml#L45) gen code and runtime. This proves that client libraries with 4.33 runtime and gen code are compatible with a third party library that contains 3.x gen code and runtime. --------- Co-authored-by: cloud-java-bot --- .github/workflows/ci.yaml | 6 ++ java-showcase/gapic-showcase/pom.xml | 27 ++++++++ .../v1beta1/it/ITProtobuf3Compatibility.java | 67 +++++++++++++++++++ java-showcase/pom.xml | 2 + 4 files changed, 102 insertions(+) create mode 100644 java-showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITProtobuf3Compatibility.java diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index a9c2d2bca0..1f66eb7d95 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -372,6 +372,12 @@ jobs: mvn clean verify -P '!showcase,enable-integration-tests,loggingTestBase,disabledLogging' \ --batch-mode \ --no-transfer-progress + - name: Showcase integration tests - Protobuf 3 compatibility with third party library Tensorflow + working-directory: java-showcase + run: | + mvn clean verify -P 'enable-integration-tests,protobuf3,showcase' \ + --batch-mode \ + --no-transfer-progress showcase-clirr: if: ${{ github.base_ref != '' }} # Only execute on pull_request trigger event diff --git a/java-showcase/gapic-showcase/pom.xml b/java-showcase/gapic-showcase/pom.xml index a9e4de22fe..feeead440b 100644 --- a/java-showcase/gapic-showcase/pom.xml +++ b/java-showcase/gapic-showcase/pom.xml @@ -201,6 +201,14 @@ test + + + org.tensorflow + tensorflow-core-platform + 1.1.0 + test + + io.opentelemetry @@ -361,6 +369,25 @@ + + protobuf3 + + + + org.apache.maven.plugins + maven-compiler-plugin + + + **/com/google/showcase/v1beta1/it/logging/*.java + + + **/com/google/showcase/v1beta1/it/ITProtobuf3Compatibility.java + + + + + + diff --git a/java-showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITProtobuf3Compatibility.java b/java-showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITProtobuf3Compatibility.java new file mode 100644 index 0000000000..04582fe7fe --- /dev/null +++ b/java-showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITProtobuf3Compatibility.java @@ -0,0 +1,67 @@ +package com.google.showcase.v1beta1.it; + +import static com.google.common.truth.Truth.assertThat; + +import org.junit.jupiter.api.Test; +import org.tensorflow.Graph; +import org.tensorflow.Session; +import org.tensorflow.op.Ops; +import org.tensorflow.op.core.Constant; +import org.tensorflow.op.math.Add; +import org.tensorflow.proto.GraphDef; +import org.tensorflow.types.TInt32; + +/** + * Tensorflow depends on protobuf 3.x gen code and runtime, we test it in showcase module to prove + * that it works with protobuf 4.33+ gen code and runtime that comes with client libraries. + */ +class ITProtobuf3Compatibility { + + @Test + void testTensorflow_helloWorldExample() { + try (Graph graph = new Graph()) { + // Hello world example for "10 + 32" operation. + Ops tf = Ops.create(graph); + + int expectedValue1 = 10; + int expectedValue2 = 32; + int expectedSum = 42; + + String name1 = "constant1"; + String name2 = "constant2"; + + Constant constant1 = tf.withName(name1).constant(expectedValue1); + Constant constant2 = tf.withName(name2).constant(expectedValue2); + + Add sum = tf.math.add(constant1, constant2); + + try (Session s = new Session(graph)) { + try (TInt32 result = (TInt32) s.runner().fetch(sum).run().get(0)) { + int actualResult = result.getInt(); + assertThat(actualResult).isEqualTo(expectedSum); + } + } + + // GraphDef is a protobuf gen code. + GraphDef graphDef = graph.toGraphDef(); + + // Inspect the protobuf gen code + Integer actual1 = getValueFromGraphDefByName(graphDef, name1); + Integer actual2 = getValueFromGraphDefByName(graphDef, name2); + + assertThat(actual1).isEqualTo(expectedValue1); + assertThat(actual2).isEqualTo(expectedValue2); + } + } + + private static Integer getValueFromGraphDefByName(GraphDef graphDef, String name1) { + return graphDef.getNodeList().stream() + .filter(nodeDef -> nodeDef.getName().equals(name1)) + .findFirst() + .get() + .getAttrOrThrow("value") + .getTensor() + .getIntValList() + .get(0); + } +} diff --git a/java-showcase/pom.xml b/java-showcase/pom.xml index 303c69e575..bda530a2b3 100644 --- a/java-showcase/pom.xml +++ b/java-showcase/pom.xml @@ -79,6 +79,7 @@ **/com/google/showcase/v1beta1/it/logging/*.java + **/com/google/showcase/v1beta1/it/ITProtobuf3Compatibility.java @@ -120,6 +121,7 @@ **/com/google/showcase/v1beta1/it/logging/*.java + **/com/google/showcase/v1beta1/it/ITProtobuf3Compatibility.java