|
| 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.google.cloud.testing.junit4; |
| 18 | + |
| 19 | +import static com.google.common.base.Preconditions.checkArgument; |
| 20 | + |
| 21 | +import java.util.ArrayList; |
| 22 | +import java.util.List; |
| 23 | +import org.junit.rules.TestRule; |
| 24 | +import org.junit.runner.Description; |
| 25 | +import org.junit.runners.model.MultipleFailureException; |
| 26 | +import org.junit.runners.model.Statement; |
| 27 | + |
| 28 | +/** |
| 29 | + * A JUnit rule that allows multiple attempts of a test execution before ultimately reporting |
| 30 | + * failure for the test. Attempts will be attempted with an exponential backoff which defaults to a |
| 31 | + * starting duration of 1 second. |
| 32 | + * |
| 33 | + * <p>If after the maximum number of attempts the test has still not succeeded, all failures will be |
| 34 | + * propagated as the result of the test allowing all errors to be visible (regardless if they are |
| 35 | + * the same failure or different ones). |
| 36 | + * |
| 37 | + * <p>To use this rule add the field declaration to your JUnit 4 Test class: |
| 38 | + * |
| 39 | + * <p><i>Note: It is important that the field is public</i> |
| 40 | + * |
| 41 | + * <pre>{@code |
| 42 | + * @Rule |
| 43 | + * public MultipleAttemptsRule multipleAttemptsRule = new MultipleAttemptsRule(3); |
| 44 | + * }</pre> |
| 45 | + * |
| 46 | + * @see org.junit.Rule |
| 47 | + */ |
| 48 | +public final class MultipleAttemptsRule implements TestRule { |
| 49 | + private final long initialBackoffMillis; |
| 50 | + private final int maxAttemptCount; |
| 51 | + |
| 52 | + /** |
| 53 | + * Construct a {@link MultipleAttemptsRule} which will attempt a test up to {@code attemptCount} |
| 54 | + * times before ultimately reporting failure of the test. |
| 55 | + * |
| 56 | + * <p>The initialBackoffMillis will be set to 1000L. |
| 57 | + * |
| 58 | + * @param maxAttemptCount max number of attempts before reporting failure, must be greater than 0 |
| 59 | + * @see #MultipleAttemptsRule(int, long) |
| 60 | + */ |
| 61 | + public MultipleAttemptsRule(int maxAttemptCount) { |
| 62 | + this(maxAttemptCount, 1000L); |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Construct a {@link MultipleAttemptsRule} which will attempt a test up to {@code attemptCount} |
| 67 | + * times before ultimately reporting failure of the test. |
| 68 | + * |
| 69 | + * <p>The {@code initialBackoffMillis} will be used as the first pause duration before |
| 70 | + * reattempting the test. |
| 71 | + * |
| 72 | + * @param maxAttemptCount max number of attempts before reporting failure, must be greater than 0 |
| 73 | + * @param initialBackoffMillis initial duration in millis to wait between attempts, must be |
| 74 | + * greater than or equal to 0 |
| 75 | + */ |
| 76 | + public MultipleAttemptsRule(int maxAttemptCount, long initialBackoffMillis) { |
| 77 | + checkArgument(maxAttemptCount > 0, "attemptCount must be > 0"); |
| 78 | + checkArgument(initialBackoffMillis >= 0, "initialBackoffMillis must be >= 0"); |
| 79 | + this.initialBackoffMillis = initialBackoffMillis; |
| 80 | + this.maxAttemptCount = maxAttemptCount; |
| 81 | + } |
| 82 | + |
| 83 | + @Override |
| 84 | + public Statement apply(final Statement base, Description description) { |
| 85 | + return new Statement() { |
| 86 | + @Override |
| 87 | + public void evaluate() throws Throwable { |
| 88 | + List<Throwable> failures = new ArrayList<>(); |
| 89 | + |
| 90 | + long retryIntervalMillis = initialBackoffMillis; |
| 91 | + |
| 92 | + for (int i = 1; i <= maxAttemptCount; i++) { |
| 93 | + try { |
| 94 | + base.evaluate(); |
| 95 | + return; |
| 96 | + } catch (Throwable t) { |
| 97 | + failures.add(t); |
| 98 | + Thread.sleep(retryIntervalMillis); |
| 99 | + retryIntervalMillis *= 1.5f; |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + MultipleFailureException.assertEmpty(failures); |
| 104 | + } |
| 105 | + }; |
| 106 | + } |
| 107 | +} |
0 commit comments