File tree Expand file tree Collapse file tree 2 files changed +20
-7
lines changed
main/java/com/example/vision/snippets
test/java/com/example/vision Expand file tree Collapse file tree 2 files changed +20
-7
lines changed Original file line number Diff line number Diff line change 2525import com .google .cloud .vision .v1 .ImageAnnotatorClient ;
2626import com .google .cloud .vision .v1 .ImageSource ;
2727import com .google .cloud .vision .v1 .LocationInfo ;
28+ import io .grpc .Status ;
29+ import io .grpc .StatusRuntimeException ;
2830import java .io .IOException ;
2931import java .util .ArrayList ;
3032import 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
Original file line number Diff line number Diff line change 2020
2121import com .example .vision .snippets .DetectLandmarksGcs ;
2222import com .example .vision .snippets .DetectLandmarksUrl ;
23+ import io .grpc .StatusRuntimeException ;
2324import java .io .ByteArrayOutputStream ;
2425import java .io .PrintStream ;
2526import 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}
You can’t perform that action at this time.
0 commit comments