Skip to content

Commit 721d14f

Browse files
authored
samples: added retry logic for flaky tests (GoogleCloudPlatform#3957)
1 parent 15d5196 commit 721d14f

File tree

3 files changed

+167
-37
lines changed

3 files changed

+167
-37
lines changed

video/cloud-client/src/test/java/com/example/video/DetectIT.java

+1-37
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,7 @@ public class DetectIT {
4343
static final String EXPLICIT_CONTENT_LOCATION = "gs://cloud-samples-data/video/cat.mp4";
4444
static final String SPEECH_GCS_LOCATION =
4545
"gs://java-docs-samples-testing/video/googlework_short.mp4";
46-
private static final List<String> POSSIBLE_TEXTS = Arrays.asList(
47-
"Google", "SUR", "SUR", "ROTO", "Vice President", "58oo9", "LONDRES", "OMAR", "PARIS",
48-
"METRO", "RUE", "CARLO");
46+
4947
private ByteArrayOutputStream bout;
5048
private PrintStream out;
5149

@@ -119,38 +117,4 @@ public void testTrackObjectsGcs() throws Exception {
119117
String got = bout.toString();
120118
assertThat(got).contains("Entity id");
121119
}
122-
123-
@Test
124-
public void testTextDetection() throws Exception {
125-
VideoAnnotationResults result = TextDetection.detectText("resources/googlework_short.mp4");
126-
127-
boolean textExists = false;
128-
for (TextAnnotation textAnnotation : result.getTextAnnotationsList()) {
129-
for (String possibleText : POSSIBLE_TEXTS) {
130-
if (textAnnotation.getText().toUpperCase().contains(possibleText.toUpperCase())) {
131-
textExists = true;
132-
break;
133-
}
134-
}
135-
}
136-
137-
assertThat(textExists).isTrue();
138-
}
139-
140-
@Test
141-
public void testTextDetectionGcs() throws Exception {
142-
VideoAnnotationResults result = TextDetection.detectTextGcs(SPEECH_GCS_LOCATION);
143-
144-
boolean textExists = false;
145-
for (TextAnnotation textAnnotation : result.getTextAnnotationsList()) {
146-
for (String possibleText : POSSIBLE_TEXTS) {
147-
if (textAnnotation.getText().toUpperCase().contains(possibleText.toUpperCase())) {
148-
textExists = true;
149-
break;
150-
}
151-
}
152-
}
153-
154-
assertThat(textExists).isTrue();
155-
}
156120
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/*
2+
* Copyright 2020 Google Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
18+
package com.example.video;
19+
20+
import static com.google.common.truth.Truth.assertThat;
21+
22+
import com.google.cloud.videointelligence.v1.TextAnnotation;
23+
import com.google.cloud.videointelligence.v1.VideoAnnotationResults;
24+
import java.io.ByteArrayOutputStream;
25+
import java.io.PrintStream;
26+
import java.util.Arrays;
27+
import java.util.List;
28+
import org.junit.After;
29+
import org.junit.Before;
30+
import org.junit.Rule;
31+
import org.junit.Test;
32+
33+
public class DetectTextTest {
34+
static final String SPEECH_GCS_LOCATION =
35+
"gs://java-docs-samples-testing/video/googlework_short.mp4";
36+
private static final List<String> POSSIBLE_TEXTS =
37+
Arrays.asList(
38+
"Google",
39+
"SUR",
40+
"SUR",
41+
"ROTO",
42+
"Vice President",
43+
"58oo9",
44+
"LONDRES",
45+
"OMAR",
46+
"PARIS",
47+
"METRO",
48+
"RUE",
49+
"CARLO");
50+
@Rule public Retry retry = new Retry(3);
51+
private ByteArrayOutputStream bout;
52+
private PrintStream out;
53+
private PrintStream originalPrintStream;
54+
55+
@Before
56+
public void setUp() {
57+
bout = new ByteArrayOutputStream();
58+
out = new PrintStream(bout);
59+
originalPrintStream = System.out;
60+
System.setOut(out);
61+
}
62+
63+
@After
64+
public void tearDown() {
65+
System.out.flush();
66+
System.setOut(originalPrintStream);
67+
}
68+
69+
@Test
70+
public void testTextDetection() throws Exception {
71+
VideoAnnotationResults result = TextDetection.detectText("resources/googlework_short.mp4");
72+
73+
boolean textExists = false;
74+
for (TextAnnotation textAnnotation : result.getTextAnnotationsList()) {
75+
for (String possibleText : POSSIBLE_TEXTS) {
76+
if (textAnnotation.getText().toUpperCase().contains(possibleText.toUpperCase())) {
77+
textExists = true;
78+
break;
79+
}
80+
}
81+
}
82+
83+
assertThat(textExists).isTrue();
84+
}
85+
86+
@Test
87+
public void testTextDetectionGcs() throws Exception {
88+
VideoAnnotationResults result = TextDetection.detectTextGcs(SPEECH_GCS_LOCATION);
89+
90+
boolean textExists = false;
91+
for (TextAnnotation textAnnotation : result.getTextAnnotationsList()) {
92+
for (String possibleText : POSSIBLE_TEXTS) {
93+
if (textAnnotation.getText().toUpperCase().contains(possibleText.toUpperCase())) {
94+
textExists = true;
95+
break;
96+
}
97+
}
98+
}
99+
100+
assertThat(textExists).isTrue();
101+
}
102+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* Copyright 2020 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.video;
18+
19+
import java.util.Objects;
20+
import org.junit.rules.TestRule;
21+
import org.junit.runner.Description;
22+
import org.junit.runners.model.Statement;
23+
24+
public class Retry implements TestRule {
25+
private int maxAttempts;
26+
27+
public Retry(int maxAttempts) {
28+
this.maxAttempts = maxAttempts;
29+
}
30+
31+
@Override
32+
public Statement apply(Statement base, Description description) {
33+
return statement(base, description);
34+
}
35+
36+
private Statement statement(final Statement base, final Description description) {
37+
return new Statement() {
38+
@Override
39+
public void evaluate() throws Throwable {
40+
Throwable caughtThrowable = null;
41+
42+
// implement retry logic here
43+
int factor = 1;
44+
for (int attempt = 0; attempt < maxAttempts; attempt++) {
45+
try {
46+
base.evaluate();
47+
return;
48+
} catch (Throwable t) {
49+
caughtThrowable = t;
50+
51+
// random_number_milliseconds that is less than or equal to 1000.
52+
int randomNumberMilliseconds = (int) Math.floor(Math.random() * 1000) + 1;
53+
Thread.sleep(1300 * factor + randomNumberMilliseconds);
54+
System.out.println(description.getDisplayName() + ": run " + (attempt + 1) + " failed");
55+
factor += 1;
56+
}
57+
}
58+
System.out.println(
59+
description.getDisplayName() + ": giving up after " + maxAttempts + " failures");
60+
throw Objects.requireNonNull(caughtThrowable);
61+
}
62+
};
63+
}
64+
}

0 commit comments

Comments
 (0)