Skip to content

Commit e6498ca

Browse files
authored
samples (test): fixed the flaky test (GoogleCloudPlatform#3611)
* samples (test): fixed the flaky test * removed unused var * added missing exception
1 parent 49b59a6 commit e6498ca

File tree

2 files changed

+20
-7
lines changed

2 files changed

+20
-7
lines changed

vision/cloud-client/src/main/java/com/example/vision/snippets/DetectLandmarksUrl.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
import com.google.cloud.vision.v1.ImageAnnotatorClient;
2626
import com.google.cloud.vision.v1.ImageSource;
2727
import com.google.cloud.vision.v1.LocationInfo;
28+
import io.grpc.Status;
29+
import io.grpc.StatusRuntimeException;
2830
import java.io.IOException;
2931
import java.util.ArrayList;
3032
import java.util.List;
@@ -38,7 +40,7 @@ public static void detectLandmarksUrl() throws IOException {
3840
}
3941

4042
// Detects landmarks in the specified URI.
41-
public static void detectLandmarksUrl(String uri) throws IOException {
43+
public static void detectLandmarksUrl(String uri) throws IOException, StatusRuntimeException {
4244
List<AnnotateImageRequest> requests = new ArrayList<>();
4345

4446
ImageSource imgSource = ImageSource.newBuilder().setImageUri(uri).build();
@@ -58,7 +60,7 @@ public static void detectLandmarksUrl(String uri) throws IOException {
5860
for (AnnotateImageResponse res : responses) {
5961
if (res.hasError()) {
6062
System.out.format("Error: %s%n", res.getError().getMessage());
61-
return;
63+
throw new StatusRuntimeException(Status.UNAVAILABLE);
6264
}
6365

6466
// For full list of available annotations, see http://g.co/cloud/vision/docs

vision/cloud-client/src/test/java/com/example/vision/DetectLandmarksGcsTest.java

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import com.example.vision.snippets.DetectLandmarksGcs;
2222
import com.example.vision.snippets.DetectLandmarksUrl;
23+
import io.grpc.StatusRuntimeException;
2324
import java.io.ByteArrayOutputStream;
2425
import java.io.PrintStream;
2526
import org.junit.After;
@@ -63,11 +64,21 @@ public void testLandmarksUrl() throws Exception {
6364

6465
@Test
6566
public void testLandmarksGcs() throws Exception {
66-
// Act
67-
DetectLandmarksGcs.detectLandmarksGcs("gs://" + ASSET_BUCKET + "/vision/landmark/pofa.jpg");
67+
int tryCount = 0;
68+
int maxTries = 3;
6869

69-
// Assert
70-
String got = bout.toString().toLowerCase();
71-
assertThat(got).contains("palace of fine arts");
70+
while (tryCount < maxTries) {
71+
try {
72+
// Act
73+
DetectLandmarksGcs.detectLandmarksGcs("gs://" + ASSET_BUCKET + "/vision/landmark/pofa.jpg");
74+
// Assert
75+
String got = bout.toString().toLowerCase();
76+
assertThat(got).contains("palace of fine arts");
77+
break;
78+
} catch (StatusRuntimeException ex) {
79+
System.out.println("Retrying...");
80+
tryCount++;
81+
}
82+
}
7283
}
7384
}

0 commit comments

Comments
 (0)