Skip to content

Commit

Permalink
samples(testing): add retry rule
Browse files Browse the repository at this point in the history
  • Loading branch information
anguillanneuf committed Mar 9, 2021
1 parent 0dd6d2d commit 8510577
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 1 deletion.
23 changes: 23 additions & 0 deletions samples/snippets/src/test/java/pubsub/Retry.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright 2021 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package pubsub;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

@Retention(RetentionPolicy.RUNTIME)
public @interface Retry {}
55 changes: 55 additions & 0 deletions samples/snippets/src/test/java/pubsub/RetryRule.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Copyright 2021 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package pubsub;

import java.util.concurrent.atomic.AtomicInteger;

import org.junit.rules.TestRule;
import org.junit.runner.Description;
import org.junit.runners.model.Statement;

public class RetryRule implements TestRule {

private AtomicInteger count;

public RetryRule(int count){
super();
this.count = new AtomicInteger(count);
}

@Override
public Statement apply(final Statement statement, final Description description) {
return new Statement() {
@Override
public void evaluate() throws Throwable {
while (count.getAndDecrement() > 0) {
try {
statement.evaluate();
return;
} catch (Throwable throwable) {
if (count.get() > 0 && description.getAnnotation(Retry.class)!= null) {
System.out.println(description.getDisplayName() + "failed.");
System.out.println(count.toString() + " retries remain.");
} else {
throw throwable;
}
}
}
}
};
}
}
5 changes: 4 additions & 1 deletion samples/snippets/src/test/java/pubsub/SchemaIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,9 @@ private static void requireEnvVar(String varName) {
System.getenv(varName));
}

@Rule public Timeout globalTimeout = Timeout.seconds(300); // 5 minute timeout
@Rule public Timeout globalTimeout = Timeout.seconds(600); // 10 minute timeout

@Rule public RetryRule retryRule = new RetryRule(3); // Retry 3 times.

@BeforeClass
public static void checkRequirements() {
Expand Down Expand Up @@ -116,6 +118,7 @@ public void tearDown() throws Exception {
}

@Test
@Retry
public void testSchema() throws Exception {
// Test creating Avro schema.
CreateAvroSchemaExample.createAvroSchemaExample(projectId, avroSchemaId, absoluteAvscFilePath);
Expand Down

0 comments on commit 8510577

Please sign in to comment.