From 302d7f7f4439dce161fa1e74015d2a9442a25afb Mon Sep 17 00:00:00 2001 From: Tianzi Cai Date: Tue, 9 Mar 2021 14:32:35 -0800 Subject: [PATCH 1/2] samples(testing): add retry rule --- .../src/main/java/utilities/State.java | 9 ++-- .../src/main/java/utilities/StateProto.java | 9 ++-- .../snippets/src/test/java/pubsub/Retry.java | 23 ++++++++ .../src/test/java/pubsub/RetryRule.java | 54 +++++++++++++++++++ .../src/test/java/pubsub/SchemaIT.java | 5 +- 5 files changed, 89 insertions(+), 11 deletions(-) create mode 100644 samples/snippets/src/test/java/pubsub/Retry.java create mode 100644 samples/snippets/src/test/java/pubsub/RetryRule.java diff --git a/samples/snippets/src/main/java/utilities/State.java b/samples/snippets/src/main/java/utilities/State.java index 87ce59223..503acf61f 100644 --- a/samples/snippets/src/main/java/utilities/State.java +++ b/samples/snippets/src/main/java/utilities/State.java @@ -23,14 +23,13 @@ /** * This file is created using Avro tools. * - * To download, visit https://avro.apache.org/releases.html#Download + *

To download, visit https://avro.apache.org/releases.html#Download * - * Run the following command from the `samples/snippets` directory to - * generate this class: + *

Run the following command from the `samples/snippets` directory to generate this class: * - *`java -jar /location/to/your/avro-tools-1.10.1.jar compile schema src/main/resources/us-states.avsc src/main/java/` + *

`java -jar /location/to/your/avro-tools-1.10.1.jar compile schema + * src/main/resources/us-states.avsc src/main/java/` */ - package utilities; import org.apache.avro.message.BinaryMessageDecoder; diff --git a/samples/snippets/src/main/java/utilities/StateProto.java b/samples/snippets/src/main/java/utilities/StateProto.java index be57ce7b7..60701da37 100644 --- a/samples/snippets/src/main/java/utilities/StateProto.java +++ b/samples/snippets/src/main/java/utilities/StateProto.java @@ -20,14 +20,13 @@ /** * This file is created using protoc. * - * To download, visit https://developers.google.com/protocol-buffers/docs/downloads + *

To download, visit https://developers.google.com/protocol-buffers/docs/downloads * - * Run the following command from the `samples/snippets` directory to - * generate this class: + *

Run the following command from the `samples/snippets` directory to generate this class: * - *`protoc --proto_path=src/main/resources/ --java_out=src/main/java/ src/main/resources/us-states.proto` + *

`protoc --proto_path=src/main/resources/ --java_out=src/main/java/ + * src/main/resources/us-states.proto` */ - package utilities; public final class StateProto { diff --git a/samples/snippets/src/test/java/pubsub/Retry.java b/samples/snippets/src/test/java/pubsub/Retry.java new file mode 100644 index 000000000..22fbc8168 --- /dev/null +++ b/samples/snippets/src/test/java/pubsub/Retry.java @@ -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 {} diff --git a/samples/snippets/src/test/java/pubsub/RetryRule.java b/samples/snippets/src/test/java/pubsub/RetryRule.java new file mode 100644 index 000000000..178369274 --- /dev/null +++ b/samples/snippets/src/test/java/pubsub/RetryRule.java @@ -0,0 +1,54 @@ +/* + * 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; + } + } + } + } + }; + } +} diff --git a/samples/snippets/src/test/java/pubsub/SchemaIT.java b/samples/snippets/src/test/java/pubsub/SchemaIT.java index 45e899619..25fcf7b97 100644 --- a/samples/snippets/src/test/java/pubsub/SchemaIT.java +++ b/samples/snippets/src/test/java/pubsub/SchemaIT.java @@ -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() { @@ -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); From fb6c36ef77609d808d85136bbc4c2e3d9fa02ec4 Mon Sep 17 00:00:00 2001 From: Tianzi Cai Date: Wed, 10 Mar 2021 12:50:04 -0800 Subject: [PATCH 2/2] use google-cloud-core's MultipleAttemptsRule --- samples/install-without-bom/pom.xml | 6 +++ samples/snapshot/pom.xml | 6 +++ samples/snippets/pom.xml | 6 +++ .../snippets/src/test/java/pubsub/Retry.java | 23 -------- .../src/test/java/pubsub/RetryRule.java | 54 ------------------- .../src/test/java/pubsub/SchemaIT.java | 11 +--- 6 files changed, 20 insertions(+), 86 deletions(-) delete mode 100644 samples/snippets/src/test/java/pubsub/Retry.java delete mode 100644 samples/snippets/src/test/java/pubsub/RetryRule.java diff --git a/samples/install-without-bom/pom.xml b/samples/install-without-bom/pom.xml index 97065bdf1..c6c39ae8c 100644 --- a/samples/install-without-bom/pom.xml +++ b/samples/install-without-bom/pom.xml @@ -77,6 +77,12 @@ protobuf-java-util 3.15.3 + + com.google.cloud + google-cloud-core + 1.94.1 + tests + diff --git a/samples/snapshot/pom.xml b/samples/snapshot/pom.xml index b9d5f7c58..3fce50a90 100644 --- a/samples/snapshot/pom.xml +++ b/samples/snapshot/pom.xml @@ -76,6 +76,12 @@ 1.1.2 test + + com.google.cloud + google-cloud-core + 1.94.1 + tests + diff --git a/samples/snippets/pom.xml b/samples/snippets/pom.xml index 32daa6883..d9415a5cd 100644 --- a/samples/snippets/pom.xml +++ b/samples/snippets/pom.xml @@ -87,6 +87,12 @@ 1.1.2 test + + com.google.cloud + google-cloud-core + 1.94.1 + tests + diff --git a/samples/snippets/src/test/java/pubsub/Retry.java b/samples/snippets/src/test/java/pubsub/Retry.java deleted file mode 100644 index 22fbc8168..000000000 --- a/samples/snippets/src/test/java/pubsub/Retry.java +++ /dev/null @@ -1,23 +0,0 @@ -/* - * 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 {} diff --git a/samples/snippets/src/test/java/pubsub/RetryRule.java b/samples/snippets/src/test/java/pubsub/RetryRule.java deleted file mode 100644 index 178369274..000000000 --- a/samples/snippets/src/test/java/pubsub/RetryRule.java +++ /dev/null @@ -1,54 +0,0 @@ -/* - * 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; - } - } - } - } - }; - } -} diff --git a/samples/snippets/src/test/java/pubsub/SchemaIT.java b/samples/snippets/src/test/java/pubsub/SchemaIT.java index 25fcf7b97..e920891eb 100644 --- a/samples/snippets/src/test/java/pubsub/SchemaIT.java +++ b/samples/snippets/src/test/java/pubsub/SchemaIT.java @@ -23,6 +23,7 @@ import com.google.cloud.pubsub.v1.SchemaServiceClient; import com.google.cloud.pubsub.v1.SubscriptionAdminClient; import com.google.cloud.pubsub.v1.TopicAdminClient; +import com.google.cloud.testing.junit4.MultipleAttemptsRule; import com.google.pubsub.v1.Encoding; import com.google.pubsub.v1.ProjectSubscriptionName; import com.google.pubsub.v1.SchemaName; @@ -33,7 +34,6 @@ import java.util.UUID; import org.junit.After; import org.junit.Before; -import org.junit.BeforeClass; import org.junit.Rule; import org.junit.Test; import org.junit.rules.Timeout; @@ -74,13 +74,7 @@ private static void requireEnvVar(String varName) { } @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() { - requireEnvVar("GOOGLE_CLOUD_PROJECT"); - } + @Rule public MultipleAttemptsRule retryRule = new MultipleAttemptsRule(/*maxAttemptCount=*/ 3); @Before public void setUp() { @@ -118,7 +112,6 @@ public void tearDown() throws Exception { } @Test - @Retry public void testSchema() throws Exception { // Test creating Avro schema. CreateAvroSchemaExample.createAvroSchemaExample(projectId, avroSchemaId, absoluteAvscFilePath);