Skip to content

Commit 35c3cea

Browse files
munkhuushmglShabirmean
authored andcommitted
samples: Translate flaky tests (#297)
Fixes #292 #268 #267 ☕️ This is the draft I do not know it is considered as good test design using base class I basically got the idea from the stack overflow answer: https://stackoverflow.com/questions/8295100/how-to-re-run-failed-junit-tests-immediately
1 parent 1172de4 commit 35c3cea

9 files changed

+118
-8
lines changed

translate/snippets/src/main/java/com/example/translate/BatchTranslateText.java

+6-1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import com.google.cloud.translate.v3.TranslationServiceClient;
3030
import java.io.IOException;
3131
import java.util.concurrent.ExecutionException;
32+
import java.util.concurrent.ThreadLocalRandom;
3233
import java.util.concurrent.TimeUnit;
3334
import java.util.concurrent.TimeoutException;
3435

@@ -85,7 +86,11 @@ public static void batchTranslateText(
8586
client.batchTranslateTextAsync(request);
8687

8788
System.out.println("Waiting for operation to complete...");
88-
BatchTranslateResponse response = future.get(450, TimeUnit.SECONDS);
89+
90+
// random number between 300 - 450 (maximum allowed seconds)
91+
long randomNumber = ThreadLocalRandom.current().nextInt(300, 450);
92+
BatchTranslateResponse response = future.get(randomNumber, TimeUnit.SECONDS);
93+
8994
System.out.printf("Total Characters: %s\n", response.getTotalCharacters());
9095
System.out.printf("Translated Characters: %s\n", response.getTranslatedCharacters());
9196
}

translate/snippets/src/main/java/com/example/translate/BatchTranslateTextWithGlossary.java

+6-1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import com.google.cloud.translate.v3.TranslationServiceClient;
3232
import java.io.IOException;
3333
import java.util.concurrent.ExecutionException;
34+
import java.util.concurrent.ThreadLocalRandom;
3435
import java.util.concurrent.TimeUnit;
3536
import java.util.concurrent.TimeoutException;
3637

@@ -103,7 +104,11 @@ public static void batchTranslateTextWithGlossary(
103104
client.batchTranslateTextAsync(request);
104105

105106
System.out.println("Waiting for operation to complete...");
106-
BatchTranslateResponse response = future.get(450, TimeUnit.SECONDS);
107+
108+
// random number between 300 - 450 (maximum allowed seconds)
109+
long randomNumber = ThreadLocalRandom.current().nextInt(300, 450);
110+
BatchTranslateResponse response = future.get(randomNumber, TimeUnit.SECONDS);
111+
107112
// Display the translation for each input text provided
108113
System.out.printf("Total Characters: %s\n", response.getTotalCharacters());
109114
System.out.printf("Translated Characters: %s\n", response.getTranslatedCharacters());

translate/snippets/src/main/java/com/example/translate/BatchTranslateTextWithGlossaryAndModel.java

+6-1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import com.google.cloud.translate.v3.TranslationServiceClient;
3232
import java.io.IOException;
3333
import java.util.concurrent.ExecutionException;
34+
import java.util.concurrent.ThreadLocalRandom;
3435
import java.util.concurrent.TimeUnit;
3536
import java.util.concurrent.TimeoutException;
3637

@@ -110,7 +111,11 @@ public static void batchTranslateTextWithGlossaryAndModel(
110111
client.batchTranslateTextAsync(request);
111112

112113
System.out.println("Waiting for operation to complete...");
113-
BatchTranslateResponse response = future.get(450, TimeUnit.SECONDS);
114+
115+
// random number between 300 - 450 (maximum allowed seconds)
116+
long randomNumber = ThreadLocalRandom.current().nextInt(300, 450);
117+
BatchTranslateResponse response = future.get(randomNumber, TimeUnit.SECONDS);
118+
114119
// Display the translation for each input text provided
115120
System.out.printf("Total Characters: %s\n", response.getTotalCharacters());
116121
System.out.printf("Translated Characters: %s\n", response.getTranslatedCharacters());

translate/snippets/src/main/java/com/example/translate/BatchTranslateTextWithModel.java

+6-1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import com.google.cloud.translate.v3.TranslationServiceClient;
3030
import java.io.IOException;
3131
import java.util.concurrent.ExecutionException;
32+
import java.util.concurrent.ThreadLocalRandom;
3233
import java.util.concurrent.TimeUnit;
3334
import java.util.concurrent.TimeoutException;
3435

@@ -100,7 +101,11 @@ public static void batchTranslateTextWithModel(
100101
client.batchTranslateTextAsync(request);
101102

102103
System.out.println("Waiting for operation to complete...");
103-
BatchTranslateResponse response = future.get(450, TimeUnit.SECONDS);
104+
105+
// random number between 300 - 450 (maximum allowed seconds)
106+
long randomNumber = ThreadLocalRandom.current().nextInt(300, 450);
107+
BatchTranslateResponse response = future.get(randomNumber, TimeUnit.SECONDS);
108+
104109
// Display the translation for each input text provided
105110
System.out.printf("Total Characters: %s\n", response.getTotalCharacters());
106111
System.out.printf("Translated Characters: %s\n", response.getTranslatedCharacters());

translate/snippets/src/test/java/com/example/translate/BatchTranslateTextTests.java

+8-1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import org.junit.After;
3333
import org.junit.Before;
3434
import org.junit.BeforeClass;
35+
import org.junit.Rule;
3536
import org.junit.Test;
3637
import org.junit.runner.RunWith;
3738
import org.junit.runners.JUnit4;
@@ -48,6 +49,7 @@ public class BatchTranslateTextTests {
4849

4950
private ByteArrayOutputStream bout;
5051
private PrintStream out;
52+
@Rule public Retry retry = new Retry(3);
5153

5254
private static void cleanUpBucket() {
5355
Storage storage = StorageOptions.getDefaultInstance().getService();
@@ -87,23 +89,28 @@ public static void checkRequirements() {
8789
requireEnvVar("GOOGLE_CLOUD_PROJECT");
8890
}
8991

92+
private PrintStream originalPrintStream;
93+
9094
@Before
9195
public void setUp() {
9296
bout = new ByteArrayOutputStream();
9397
out = new PrintStream(bout);
98+
originalPrintStream = System.out;
9499
System.setOut(out);
95100
}
96101

97102
@After
98103
public void tearDown() {
99104
cleanUpBucket();
100-
System.setOut(null);
105+
System.out.flush();
106+
System.setOut(originalPrintStream);
101107
}
102108

103109
@Test
104110
public void testBatchTranslateText()
105111
throws InterruptedException, ExecutionException, IOException, TimeoutException {
106112
BatchTranslateText.batchTranslateText(PROJECT_ID, "en", "es", INPUT_URI, OUTPUT_URI);
113+
107114
String got = bout.toString();
108115
assertThat(got).contains("Total Characters: 13");
109116
}

translate/snippets/src/test/java/com/example/translate/BatchTranslateTextWithGlossaryAndModelTests.java

+7-1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import org.junit.After;
3535
import org.junit.Before;
3636
import org.junit.BeforeClass;
37+
import org.junit.Rule;
3738
import org.junit.Test;
3839
import org.junit.runner.RunWith;
3940
import org.junit.runners.JUnit4;
@@ -56,6 +57,7 @@ public class BatchTranslateTextWithGlossaryAndModelTests {
5657

5758
private ByteArrayOutputStream bout;
5859
private PrintStream out;
60+
@Rule public Retry retry = new Retry(3);
5961

6062
private static final void cleanUpBucket() {
6163
Storage storage = StorageOptions.getDefaultInstance().getService();
@@ -95,6 +97,8 @@ public static void checkRequirements() {
9597
requireEnvVar("GOOGLE_CLOUD_PROJECT");
9698
}
9799

100+
private PrintStream originalPrintStream;
101+
98102
@Before
99103
public void setUp() throws InterruptedException, ExecutionException, IOException {
100104
// Create a glossary that can be used in the test
@@ -107,6 +111,7 @@ public void setUp() throws InterruptedException, ExecutionException, IOException
107111

108112
bout = new ByteArrayOutputStream();
109113
out = new PrintStream(bout);
114+
originalPrintStream = System.out;
110115
System.setOut(out);
111116
}
112117

@@ -115,7 +120,8 @@ public void tearDown() throws InterruptedException, ExecutionException, IOExcept
115120
cleanUpBucket();
116121
// Delete the created glossary
117122
DeleteGlossary.deleteGlossary(PROJECT_ID, GLOSSARY_ID);
118-
System.setOut(null);
123+
System.out.flush();
124+
System.setOut(originalPrintStream);
119125
}
120126

121127
@Test

translate/snippets/src/test/java/com/example/translate/BatchTranslateTextWithGlossaryTests.java

+7-1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import org.junit.After;
3535
import org.junit.Before;
3636
import org.junit.BeforeClass;
37+
import org.junit.Rule;
3738
import org.junit.Test;
3839
import org.junit.runner.RunWith;
3940
import org.junit.runners.JUnit4;
@@ -55,6 +56,7 @@ public class BatchTranslateTextWithGlossaryTests {
5556

5657
private ByteArrayOutputStream bout;
5758
private PrintStream out;
59+
private PrintStream originalPrintStream;
5860

5961
private static final void cleanUpBucket() {
6062
Storage storage = StorageOptions.getDefaultInstance().getService();
@@ -106,6 +108,7 @@ public void setUp() throws InterruptedException, ExecutionException, IOException
106108

107109
bout = new ByteArrayOutputStream();
108110
out = new PrintStream(bout);
111+
originalPrintStream = System.out;
109112
System.setOut(out);
110113
}
111114

@@ -115,9 +118,12 @@ public void tearDown() throws InterruptedException, ExecutionException, IOExcept
115118
cleanUpBucket();
116119
// Delete the created glossary
117120
DeleteGlossary.deleteGlossary(PROJECT_ID, GLOSSARY_ID);
118-
System.setOut(null);
121+
System.out.flush();
122+
System.setOut(originalPrintStream);
119123
}
120124

125+
@Rule public Retry retry = new Retry(3);
126+
121127
@Test
122128
public void testBatchTranslateTextWithGlossary()
123129
throws InterruptedException, ExecutionException, IOException, TimeoutException {

translate/snippets/src/test/java/com/example/translate/BatchTranslateTextWithModelTests.java

+7-1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import org.junit.After;
3333
import org.junit.Before;
3434
import org.junit.BeforeClass;
35+
import org.junit.Rule;
3536
import org.junit.Test;
3637
import org.junit.runner.RunWith;
3738
import org.junit.runners.JUnit4;
@@ -50,6 +51,7 @@ public class BatchTranslateTextWithModelTests {
5051

5152
private ByteArrayOutputStream bout;
5253
private PrintStream out;
54+
private PrintStream originalPrintStream;
5355

5456
private static final void cleanUpBucket() {
5557
Storage storage = StorageOptions.getDefaultInstance().getService();
@@ -93,15 +95,19 @@ public static void checkRequirements() {
9395
public void setUp() {
9496
bout = new ByteArrayOutputStream();
9597
out = new PrintStream(bout);
98+
originalPrintStream = System.out;
9699
System.setOut(out);
97100
}
98101

99102
@After
100103
public void tearDown() {
101104
cleanUpBucket();
102-
System.setOut(null);
105+
System.out.flush();
106+
System.setOut(originalPrintStream);
103107
}
104108

109+
@Rule public Retry retry = new Retry(3);
110+
105111
@Test
106112
public void testBatchTranslateTextWithModel()
107113
throws InterruptedException, ExecutionException, IOException, TimeoutException {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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.translate;
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+
}
59+
System.out.println(
60+
description.getDisplayName() + ": giving up after " + maxAttempts + " failures");
61+
throw Objects.requireNonNull(caughtThrowable);
62+
}
63+
};
64+
}
65+
}

0 commit comments

Comments
 (0)