Skip to content

Commit 28cc591

Browse files
tests: Add protobuf 3.x compatibility tests with a third party library tensorflow (#4082)
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 <cloud-java-bot@google.com>
1 parent 22446d5 commit 28cc591

File tree

4 files changed

+102
-0
lines changed

4 files changed

+102
-0
lines changed

.github/workflows/ci.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,12 @@ jobs:
372372
mvn clean verify -P '!showcase,enable-integration-tests,loggingTestBase,disabledLogging' \
373373
--batch-mode \
374374
--no-transfer-progress
375+
- name: Showcase integration tests - Protobuf 3 compatibility with third party library Tensorflow
376+
working-directory: java-showcase
377+
run: |
378+
mvn clean verify -P 'enable-integration-tests,protobuf3,showcase' \
379+
--batch-mode \
380+
--no-transfer-progress
375381
376382
showcase-clirr:
377383
if: ${{ github.base_ref != '' }} # Only execute on pull_request trigger event

java-showcase/gapic-showcase/pom.xml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,14 @@
201201
<scope>test</scope>
202202
</dependency>
203203

204+
<!-- Protobuf 3.x compatibility tests, tensorflow depends on protobuf 3.x gen code and runtime -->
205+
<dependency>
206+
<groupId>org.tensorflow</groupId>
207+
<artifactId>tensorflow-core-platform</artifactId>
208+
<version>1.1.0</version>
209+
<scope>test</scope>
210+
</dependency>
211+
204212
<!-- Otel testing libraries -->
205213
<dependency>
206214
<groupId>io.opentelemetry</groupId>
@@ -361,6 +369,25 @@
361369
</plugins>
362370
</build>
363371
</profile>
372+
<profile>
373+
<id>protobuf3</id>
374+
<build>
375+
<plugins>
376+
<plugin>
377+
<groupId>org.apache.maven.plugins</groupId>
378+
<artifactId>maven-compiler-plugin</artifactId>
379+
<configuration>
380+
<testExcludes>
381+
<testExclude>**/com/google/showcase/v1beta1/it/logging/*.java</testExclude>
382+
</testExcludes>
383+
<testIncludes>
384+
<testInclude>**/com/google/showcase/v1beta1/it/ITProtobuf3Compatibility.java</testInclude>
385+
</testIncludes>
386+
</configuration>
387+
</plugin>
388+
</plugins>
389+
</build>
390+
</profile>
364391
</profiles>
365392

366393
</project>
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package com.google.showcase.v1beta1.it;
2+
3+
import static com.google.common.truth.Truth.assertThat;
4+
5+
import org.junit.jupiter.api.Test;
6+
import org.tensorflow.Graph;
7+
import org.tensorflow.Session;
8+
import org.tensorflow.op.Ops;
9+
import org.tensorflow.op.core.Constant;
10+
import org.tensorflow.op.math.Add;
11+
import org.tensorflow.proto.GraphDef;
12+
import org.tensorflow.types.TInt32;
13+
14+
/**
15+
* Tensorflow depends on protobuf 3.x gen code and runtime, we test it in showcase module to prove
16+
* that it works with protobuf 4.33+ gen code and runtime that comes with client libraries.
17+
*/
18+
class ITProtobuf3Compatibility {
19+
20+
@Test
21+
void testTensorflow_helloWorldExample() {
22+
try (Graph graph = new Graph()) {
23+
// Hello world example for "10 + 32" operation.
24+
Ops tf = Ops.create(graph);
25+
26+
int expectedValue1 = 10;
27+
int expectedValue2 = 32;
28+
int expectedSum = 42;
29+
30+
String name1 = "constant1";
31+
String name2 = "constant2";
32+
33+
Constant<TInt32> constant1 = tf.withName(name1).constant(expectedValue1);
34+
Constant<TInt32> constant2 = tf.withName(name2).constant(expectedValue2);
35+
36+
Add<TInt32> sum = tf.math.add(constant1, constant2);
37+
38+
try (Session s = new Session(graph)) {
39+
try (TInt32 result = (TInt32) s.runner().fetch(sum).run().get(0)) {
40+
int actualResult = result.getInt();
41+
assertThat(actualResult).isEqualTo(expectedSum);
42+
}
43+
}
44+
45+
// GraphDef is a protobuf gen code.
46+
GraphDef graphDef = graph.toGraphDef();
47+
48+
// Inspect the protobuf gen code
49+
Integer actual1 = getValueFromGraphDefByName(graphDef, name1);
50+
Integer actual2 = getValueFromGraphDefByName(graphDef, name2);
51+
52+
assertThat(actual1).isEqualTo(expectedValue1);
53+
assertThat(actual2).isEqualTo(expectedValue2);
54+
}
55+
}
56+
57+
private static Integer getValueFromGraphDefByName(GraphDef graphDef, String name1) {
58+
return graphDef.getNodeList().stream()
59+
.filter(nodeDef -> nodeDef.getName().equals(name1))
60+
.findFirst()
61+
.get()
62+
.getAttrOrThrow("value")
63+
.getTensor()
64+
.getIntValList()
65+
.get(0);
66+
}
67+
}

java-showcase/pom.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@
7979
<!-- Do not compile by default (without logging deps) -->
8080
<testExcludes>
8181
<testExclude>**/com/google/showcase/v1beta1/it/logging/*.java</testExclude>
82+
<testExclude>**/com/google/showcase/v1beta1/it/ITProtobuf3Compatibility.java</testExclude>
8283
</testExcludes>
8384
</configuration>
8485
</plugin>
@@ -120,6 +121,7 @@
120121
<!-- Do not compile by default (without logging deps) -->
121122
<testExcludes>
122123
<testExclude>**/com/google/showcase/v1beta1/it/logging/*.java</testExclude>
124+
<testExclude>**/com/google/showcase/v1beta1/it/ITProtobuf3Compatibility.java</testExclude>
123125
</testExcludes>
124126
</configuration>
125127
</plugin>

0 commit comments

Comments
 (0)