blob: db092df450f0002ed29cb38e3f4b6718296c1fea [file] [log] [blame]
Madan Jampania29c6772015-08-17 13:17:07 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Madan Jampania29c6772015-08-17 13:17:07 -07003 *
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 */
16package org.onlab.util;
17
18import org.junit.After;
19import org.junit.Before;
20import org.junit.Test;
21
22/**
23 * Unit tests for RetryingFunction.
24 *
25 */
26public class RetryingFunctionTest {
27
28 private int round;
29
30 @Before
31 public void setUp() {
32 round = 1;
33 }
34
35 @After
36 public void tearDown() {
37 round = 0;
38 }
39
40 @Test(expected = RetryableException.class)
41 public void testNoRetries() {
42 new RetryingFunction<>(this::succeedAfterOneFailure, RetryableException.class, 0, 10).apply(null);
43 }
44
45 @Test
46 public void testSuccessAfterOneRetry() {
47 new RetryingFunction<>(this::succeedAfterOneFailure, RetryableException.class, 1, 10).apply(null);
48 }
49
50 @Test(expected = RetryableException.class)
51 public void testFailureAfterOneRetry() {
52 new RetryingFunction<>(this::succeedAfterTwoFailures, RetryableException.class, 1, 10).apply(null);
53 }
54
55 @Test
56 public void testFailureAfterTwoRetries() {
57 new RetryingFunction<>(this::succeedAfterTwoFailures, RetryableException.class, 2, 10).apply(null);
58 }
59
60 @Test(expected = NonRetryableException.class)
61 public void testFailureWithNonRetryableFailure() {
62 new RetryingFunction<>(this::failCompletely, RetryableException.class, 2, 10).apply(null);
63 }
64
65 private String succeedAfterOneFailure(String input) {
66 if (round++ <= 1) {
67 throw new RetryableException();
68 } else {
69 return "pass";
70 }
71 }
72
73 private String succeedAfterTwoFailures(String input) {
74 if (round++ <= 2) {
75 throw new RetryableException();
76 } else {
77 return "pass";
78 }
79 }
80
81 private String failCompletely(String input) {
82 if (round++ <= 1) {
83 throw new NonRetryableException();
84 } else {
85 return "pass";
86 }
87 }
88
89 private class RetryableException extends RuntimeException {
90 }
91
92 private class NonRetryableException extends RuntimeException {
93 }
94}