From bc1427e7035af80333cbd26dbfe2da5574da7621 Mon Sep 17 00:00:00 2001 From: Mike <45373284+munkhuushmgl@users.noreply.github.com> Date: Thu, 29 Apr 2021 12:48:56 -0700 Subject: [PATCH] chore: removed all catch clauses which were causing error stack trace not appearing in the test result (#464) --- video/pom.xml | 7 ++ .../video/StreamingAnnotationToStorage.java | 7 +- .../video/StreamingAutoMlClassification.java | 3 +- .../StreamingExplicitContentDetection.java | 7 +- .../beta/video/StreamingLabelDetection.java | 7 +- .../beta/video/StreamingObjectTracking.java | 7 +- .../video/StreamingAnnotationToStorageIT.java | 4 +- .../StreamingExplicitContentDetectionIT.java | 4 +- .../beta/video/StreamingLabelDetectionIT.java | 4 +- .../beta/video/StreamingObjectTrackingIT.java | 4 +- video/src/test/java/video/DetectTextTest.java | 4 +- video/src/test/java/video/Retry.java | 64 ------------------- 12 files changed, 40 insertions(+), 82 deletions(-) delete mode 100644 video/src/test/java/video/Retry.java diff --git a/video/pom.xml b/video/pom.xml index 650dd4754c9..6b80cbc0163 100644 --- a/video/pom.xml +++ b/video/pom.xml @@ -60,5 +60,12 @@ 1.1.2 test + + com.google.cloud + google-cloud-core + 1.94.0 + test + tests + diff --git a/video/src/main/java/beta/video/StreamingAnnotationToStorage.java b/video/src/main/java/beta/video/StreamingAnnotationToStorage.java index bf096f881c4..ab4834f5b50 100644 --- a/video/src/main/java/beta/video/StreamingAnnotationToStorage.java +++ b/video/src/main/java/beta/video/StreamingAnnotationToStorage.java @@ -27,16 +27,19 @@ import com.google.cloud.videointelligence.v1p3beta1.StreamingVideoConfig; import com.google.cloud.videointelligence.v1p3beta1.StreamingVideoIntelligenceServiceClient; import com.google.protobuf.ByteString; +import io.grpc.StatusRuntimeException; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.util.Arrays; +import java.util.concurrent.TimeoutException; public class StreamingAnnotationToStorage { // Perform streaming video detection for explicit content - static void streamingAnnotationToStorage(String filePath, String gcsUri) { + static void streamingAnnotationToStorage(String filePath, String gcsUri) + throws IOException, TimeoutException, StatusRuntimeException { // String filePath = "path_to_your_video_file"; // String gcsUri = "gs://BUCKET_ID"; @@ -89,8 +92,6 @@ static void streamingAnnotationToStorage(String filePath, String gcsUri) { for (StreamingAnnotateVideoResponse response : call) { System.out.format("Storage Uri: %s\n", response.getAnnotationResultsUri()); } - } catch (IOException e) { - e.printStackTrace(); } } } diff --git a/video/src/main/java/beta/video/StreamingAutoMlClassification.java b/video/src/main/java/beta/video/StreamingAutoMlClassification.java index ab0411894ef..5a01c581505 100644 --- a/video/src/main/java/beta/video/StreamingAutoMlClassification.java +++ b/video/src/main/java/beta/video/StreamingAutoMlClassification.java @@ -35,12 +35,13 @@ import java.nio.file.Path; import java.nio.file.Paths; import java.util.Arrays; +import java.util.concurrent.TimeoutException; class StreamingAutoMlClassification { // Perform streaming video classification with an AutoML Model static void streamingAutoMlClassification(String filePath, String projectId, String modelId) - throws StatusRuntimeException, IOException { + throws TimeoutException, StatusRuntimeException, IOException { // String filePath = "path_to_your_video_file"; // String projectId = "YOUR_GCP_PROJECT_ID"; // String modelId = "YOUR_AUTO_ML_CLASSIFICATION_MODEL_ID"; diff --git a/video/src/main/java/beta/video/StreamingExplicitContentDetection.java b/video/src/main/java/beta/video/StreamingExplicitContentDetection.java index e20c6ccc3d8..2ce1c2d30f7 100644 --- a/video/src/main/java/beta/video/StreamingExplicitContentDetection.java +++ b/video/src/main/java/beta/video/StreamingExplicitContentDetection.java @@ -28,16 +28,19 @@ import com.google.cloud.videointelligence.v1p3beta1.StreamingVideoConfig; import com.google.cloud.videointelligence.v1p3beta1.StreamingVideoIntelligenceServiceClient; import com.google.protobuf.ByteString; +import io.grpc.StatusRuntimeException; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.util.Arrays; +import java.util.concurrent.TimeoutException; class StreamingExplicitContentDetection { // Perform streaming video detection for explicit content - static void streamingExplicitContentDetection(String filePath) { + static void streamingExplicitContentDetection(String filePath) + throws IOException, TimeoutException, StatusRuntimeException { // String filePath = "path_to_your_video_file"; try (StreamingVideoIntelligenceServiceClient client = @@ -92,8 +95,6 @@ static void streamingExplicitContentDetection(String filePath) { System.out.format("\tPornography: %s", frame.getPornographyLikelihood()); } } - } catch (IOException e) { - e.printStackTrace(); } } } diff --git a/video/src/main/java/beta/video/StreamingLabelDetection.java b/video/src/main/java/beta/video/StreamingLabelDetection.java index b9ec82fa972..4b26534ecd2 100644 --- a/video/src/main/java/beta/video/StreamingLabelDetection.java +++ b/video/src/main/java/beta/video/StreamingLabelDetection.java @@ -29,16 +29,19 @@ import com.google.cloud.videointelligence.v1p3beta1.StreamingVideoConfig; import com.google.cloud.videointelligence.v1p3beta1.StreamingVideoIntelligenceServiceClient; import com.google.protobuf.ByteString; +import io.grpc.StatusRuntimeException; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.util.Arrays; +import java.util.concurrent.TimeoutException; class StreamingLabelDetection { // Perform streaming video label detection - static void streamingLabelDetection(String filePath) { + static void streamingLabelDetection(String filePath) + throws IOException, TimeoutException, StatusRuntimeException { // String filePath = "path_to_your_video_file"; try (StreamingVideoIntelligenceServiceClient client = @@ -95,8 +98,6 @@ static void streamingLabelDetection(String filePath) { System.out.format("%fs: %s (%f)\n", offset, entity, confidence); } } - } catch (IOException e) { - e.printStackTrace(); } } } diff --git a/video/src/main/java/beta/video/StreamingObjectTracking.java b/video/src/main/java/beta/video/StreamingObjectTracking.java index d135bc1d930..0fe458f28a8 100644 --- a/video/src/main/java/beta/video/StreamingObjectTracking.java +++ b/video/src/main/java/beta/video/StreamingObjectTracking.java @@ -29,16 +29,19 @@ import com.google.cloud.videointelligence.v1p3beta1.StreamingVideoConfig; import com.google.cloud.videointelligence.v1p3beta1.StreamingVideoIntelligenceServiceClient; import com.google.protobuf.ByteString; +import io.grpc.StatusRuntimeException; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.util.Arrays; +import java.util.concurrent.TimeoutException; class StreamingObjectTracking { // Perform streaming video object tracking - static void streamingObjectTracking(String filePath) { + static void streamingObjectTracking(String filePath) + throws IOException, TimeoutException, StatusRuntimeException { // String filePath = "path_to_your_video_file"; try (StreamingVideoIntelligenceServiceClient client = @@ -104,8 +107,6 @@ static void streamingObjectTracking(String filePath) { System.out.format("\tBottom: %f\n", frame.getNormalizedBoundingBox().getBottom()); } } - } catch (IOException e) { - e.printStackTrace(); } } } diff --git a/video/src/test/java/beta/video/StreamingAnnotationToStorageIT.java b/video/src/test/java/beta/video/StreamingAnnotationToStorageIT.java index be135830062..9b0f9ef1098 100644 --- a/video/src/test/java/beta/video/StreamingAnnotationToStorageIT.java +++ b/video/src/test/java/beta/video/StreamingAnnotationToStorageIT.java @@ -24,7 +24,9 @@ import com.google.cloud.storage.Storage.BlobListOption; import com.google.cloud.storage.StorageOptions; import java.io.ByteArrayOutputStream; +import java.io.IOException; import java.io.PrintStream; +import java.util.concurrent.TimeoutException; import org.junit.After; import org.junit.Before; import org.junit.Test; @@ -58,7 +60,7 @@ public void tearDown() { } @Test - public void testStreamingAnnotationToStorage() { + public void testStreamingAnnotationToStorage() throws IOException, TimeoutException { String gcsUri = String.format("gs://%s/%s", PROJECT_ID, OUTPUT_PREFIX); StreamingAnnotationToStorage.streamingAnnotationToStorage("resources/cat.mp4", gcsUri); String got = bout.toString(); diff --git a/video/src/test/java/beta/video/StreamingExplicitContentDetectionIT.java b/video/src/test/java/beta/video/StreamingExplicitContentDetectionIT.java index 8e4f9b5006f..6ba53cba575 100644 --- a/video/src/test/java/beta/video/StreamingExplicitContentDetectionIT.java +++ b/video/src/test/java/beta/video/StreamingExplicitContentDetectionIT.java @@ -19,7 +19,9 @@ import static com.google.common.truth.Truth.assertThat; import java.io.ByteArrayOutputStream; +import java.io.IOException; import java.io.PrintStream; +import java.util.concurrent.TimeoutException; import org.junit.After; import org.junit.Before; import org.junit.Test; @@ -50,7 +52,7 @@ public void tearDown() { } @Test - public void testStreamingExplicitContent() { + public void testStreamingExplicitContent() throws IOException, TimeoutException { StreamingExplicitContentDetection.streamingExplicitContentDetection("resources/cat.mp4"); String got = bout.toString(); diff --git a/video/src/test/java/beta/video/StreamingLabelDetectionIT.java b/video/src/test/java/beta/video/StreamingLabelDetectionIT.java index f23cfe2b120..e779725b9d9 100644 --- a/video/src/test/java/beta/video/StreamingLabelDetectionIT.java +++ b/video/src/test/java/beta/video/StreamingLabelDetectionIT.java @@ -19,7 +19,9 @@ import static com.google.common.truth.Truth.assertThat; import java.io.ByteArrayOutputStream; +import java.io.IOException; import java.io.PrintStream; +import java.util.concurrent.TimeoutException; import org.junit.After; import org.junit.Before; import org.junit.Test; @@ -50,7 +52,7 @@ public void tearDown() { } @Test - public void testStreamingLabelDetection() { + public void testStreamingLabelDetection() throws IOException, TimeoutException { StreamingLabelDetection.streamingLabelDetection("resources/cat.mp4"); String got = bout.toString(); diff --git a/video/src/test/java/beta/video/StreamingObjectTrackingIT.java b/video/src/test/java/beta/video/StreamingObjectTrackingIT.java index d0df7c8dd39..2f16c1bd6d7 100644 --- a/video/src/test/java/beta/video/StreamingObjectTrackingIT.java +++ b/video/src/test/java/beta/video/StreamingObjectTrackingIT.java @@ -19,7 +19,9 @@ import static com.google.common.truth.Truth.assertThat; import java.io.ByteArrayOutputStream; +import java.io.IOException; import java.io.PrintStream; +import java.util.concurrent.TimeoutException; import org.junit.After; import org.junit.Before; import org.junit.Test; @@ -50,7 +52,7 @@ public void tearDown() { } @Test - public void testStreamingObjectTracking() { + public void testStreamingObjectTracking() throws IOException, TimeoutException { StreamingObjectTracking.streamingObjectTracking("resources/cat.mp4"); String got = bout.toString(); diff --git a/video/src/test/java/video/DetectTextTest.java b/video/src/test/java/video/DetectTextTest.java index 0bab2bedbc3..55c12f4330f 100644 --- a/video/src/test/java/video/DetectTextTest.java +++ b/video/src/test/java/video/DetectTextTest.java @@ -18,6 +18,7 @@ import static com.google.common.truth.Truth.assertThat; +import com.google.cloud.testing.junit4.MultipleAttemptsRule; import com.google.cloud.videointelligence.v1.TextAnnotation; import com.google.cloud.videointelligence.v1.VideoAnnotationResults; import java.io.ByteArrayOutputStream; @@ -46,7 +47,8 @@ public class DetectTextTest { "METRO", "RUE", "CARLO"); - @Rule public Retry retry = new Retry(3); + @Rule + public MultipleAttemptsRule multipleAttemptsRule = new MultipleAttemptsRule(3); private ByteArrayOutputStream bout; private PrintStream out; private PrintStream originalPrintStream; diff --git a/video/src/test/java/video/Retry.java b/video/src/test/java/video/Retry.java deleted file mode 100644 index 85dd5843dc9..00000000000 --- a/video/src/test/java/video/Retry.java +++ /dev/null @@ -1,64 +0,0 @@ -/* - * Copyright 2020 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 video; - -import java.util.Objects; -import org.junit.rules.TestRule; -import org.junit.runner.Description; -import org.junit.runners.model.Statement; - -public class Retry implements TestRule { - private int maxAttempts; - - public Retry(int maxAttempts) { - this.maxAttempts = maxAttempts; - } - - @Override - public Statement apply(Statement base, Description description) { - return statement(base, description); - } - - private Statement statement(final Statement base, final Description description) { - return new Statement() { - @Override - public void evaluate() throws Throwable { - Throwable caughtThrowable = null; - - // implement retry logic here - int factor = 1; - for (int attempt = 0; attempt < maxAttempts; attempt++) { - try { - base.evaluate(); - return; - } catch (Throwable t) { - caughtThrowable = t; - - // random_number_milliseconds that is less than or equal to 1000. - int randomNumberMilliseconds = (int) Math.floor(Math.random() * 1000) + 1; - Thread.sleep(1300 * factor + randomNumberMilliseconds); - System.out.println(description.getDisplayName() + ": run " + (attempt + 1) + " failed"); - factor += 1; - } - } - System.out.println( - description.getDisplayName() + ": giving up after " + maxAttempts + " failures"); - throw Objects.requireNonNull(caughtThrowable); - } - }; - } -}