2 * Copyright 2017-present Open Networking Foundation
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package io.atomix.utils.concurrent;
18 import org.junit.After;
19 import org.junit.Before;
20 import org.junit.Test;
23 * Retrying function test.
25 public class RetryingFunctionTest {
34 public void tearDown() {
38 @Test(expected = RetryableException.class)
39 public void testNoRetries() {
40 new RetryingFunction<>(this::succeedAfterOneFailure, RetryableException.class, 0, 10).apply(null);
44 public void testSuccessAfterOneRetry() {
45 new RetryingFunction<>(this::succeedAfterOneFailure, RetryableException.class, 1, 10).apply(null);
48 @Test(expected = RetryableException.class)
49 public void testFailureAfterOneRetry() {
50 new RetryingFunction<>(this::succeedAfterTwoFailures, RetryableException.class, 1, 10).apply(null);
54 public void testFailureAfterTwoRetries() {
55 new RetryingFunction<>(this::succeedAfterTwoFailures, RetryableException.class, 2, 10).apply(null);
58 @Test(expected = NonRetryableException.class)
59 public void testFailureWithNonRetryableFailure() {
60 new RetryingFunction<>(this::failCompletely, RetryableException.class, 2, 10).apply(null);
63 private String succeedAfterOneFailure(String input) {
65 throw new RetryableException();
71 private String succeedAfterTwoFailures(String input) {
73 throw new RetryableException();
79 private String failCompletely(String input) {
81 throw new NonRetryableException();
87 private static class RetryableException extends RuntimeException {
90 private static class NonRetryableException extends RuntimeException {