Remove most of atomix.utils.*
[controller.git] / third-party / atomix / utils / src / test / java / io / atomix / utils / concurrent / RetryingFunctionTest.java
1 /*
2  * Copyright 2017-present Open Networking Foundation
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 package io.atomix.utils.concurrent;
17
18 import org.junit.After;
19 import org.junit.Before;
20 import org.junit.Test;
21
22 /**
23  * Retrying function test.
24  */
25 public class RetryingFunctionTest {
26   private int round;
27
28   @Before
29   public void setUp() {
30     round = 1;
31   }
32
33   @After
34   public void tearDown() {
35     round = 0;
36   }
37
38   @Test(expected = RetryableException.class)
39   public void testNoRetries() {
40     new RetryingFunction<>(this::succeedAfterOneFailure, RetryableException.class, 0, 10).apply(null);
41   }
42
43   @Test
44   public void testSuccessAfterOneRetry() {
45     new RetryingFunction<>(this::succeedAfterOneFailure, RetryableException.class, 1, 10).apply(null);
46   }
47
48   @Test(expected = RetryableException.class)
49   public void testFailureAfterOneRetry() {
50     new RetryingFunction<>(this::succeedAfterTwoFailures, RetryableException.class, 1, 10).apply(null);
51   }
52
53   @Test
54   public void testFailureAfterTwoRetries() {
55     new RetryingFunction<>(this::succeedAfterTwoFailures, RetryableException.class, 2, 10).apply(null);
56   }
57
58   @Test(expected = NonRetryableException.class)
59   public void testFailureWithNonRetryableFailure() {
60     new RetryingFunction<>(this::failCompletely, RetryableException.class, 2, 10).apply(null);
61   }
62
63   private String succeedAfterOneFailure(String input) {
64     if (round++ <= 1) {
65       throw new RetryableException();
66     } else {
67       return "pass";
68     }
69   }
70
71   private String succeedAfterTwoFailures(String input) {
72     if (round++ <= 2) {
73       throw new RetryableException();
74     } else {
75       return "pass";
76     }
77   }
78
79   private String failCompletely(String input) {
80     if (round++ <= 1) {
81       throw new NonRetryableException();
82     } else {
83       return "pass";
84     }
85   }
86
87   private static class RetryableException extends RuntimeException {
88   }
89
90   private static class NonRetryableException extends RuntimeException {
91   }
92 }