| 1 |
/* |
| 2 |
* Licensed to the Apache Software Foundation (ASF) under one |
| 3 |
* or more contributor license agreements. See the NOTICE file |
| 4 |
* distributed with this work for additional information |
| 5 |
* regarding copyright ownership. The ASF licenses this file |
| 6 |
* to you under the Apache License, Version 2.0 (the |
| 7 |
* "License"); you may not use this file except in compliance |
| 8 |
* with the License. You may obtain a copy of the License at |
| 9 |
* |
| 10 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 11 |
* |
| 12 |
* Unless required by applicable law or agreed to in writing, |
| 13 |
* software distributed under the License is distributed on an |
| 14 |
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 |
* KIND, either express or implied. See the License for the |
| 16 |
* specific language governing permissions and limitations |
| 17 |
* under the License. |
| 18 |
*/ |
| 19 |
package org.eclipse.aether.named; |
| 20 |
|
| 21 |
import java.util.concurrent.TimeUnit; |
| 22 |
import java.util.concurrent.atomic.LongAdder; |
| 23 |
|
| 24 |
import org.junit.Rule; |
| 25 |
import org.junit.Test; |
| 26 |
import org.junit.rules.TestName; |
| 27 |
|
| 28 |
import static org.eclipse.aether.named.support.Retry.retry; |
| 29 |
import static org.hamcrest.MatcherAssert.assertThat; |
| 30 |
import static org.hamcrest.Matchers.equalTo; |
| 31 |
import static org.hamcrest.Matchers.greaterThan; |
| 32 |
|
| 33 |
/** |
| 34 |
* UT for {@link org.eclipse.aether.named.support.Retry}. |
| 35 |
*/ |
| 36 |
public class RetryTest { |
| 37 |
private static final long RETRY_SLEEP_MILLIS = 250L; |
| 38 |
|
| 39 |
@Rule |
| 40 |
public TestName testName = new TestName(); |
| 41 |
|
| 42 |
@Test |
| 43 |
public void happy() throws InterruptedException { |
| 44 |
LongAdder retries = new LongAdder(); |
| 45 |
String result = retry( |
| 46 |
1L, |
| 47 |
TimeUnit.SECONDS, |
| 48 |
RETRY_SLEEP_MILLIS, |
| 49 |
() -> { |
| 50 |
retries.increment(); |
| 51 |
return "happy"; |
| 52 |
}, |
| 53 |
null, |
| 54 |
"notHappy"); |
| 55 |
assertThat(result, equalTo("happy")); |
| 56 |
assertThat(retries.sum(), equalTo(1L)); |
| 57 |
} |
| 58 |
|
| 59 |
@Test |
| 60 |
public void notHappy() throws InterruptedException { |
| 61 |
LongAdder retries = new LongAdder(); |
| 62 |
String result = retry( |
| 63 |
1L, |
| 64 |
TimeUnit.SECONDS, |
| 65 |
RETRY_SLEEP_MILLIS, |
| 66 |
() -> { |
| 67 |
retries.increment(); |
| 68 |
return null; |
| 69 |
}, |
| 70 |
null, |
| 71 |
"notHappy"); |
| 72 |
assertThat(result, equalTo("notHappy")); |
| 73 |
assertThat(retries.sum(), greaterThan(1L)); |
| 74 |
} |
| 75 |
|
| 76 |
@Test |
| 77 |
public void happyAfterSomeTime() throws InterruptedException { |
| 78 |
LongAdder retries = new LongAdder(); |
| 79 |
String result = retry( |
| 80 |
1L, |
| 81 |
TimeUnit.SECONDS, |
| 82 |
RETRY_SLEEP_MILLIS, |
| 83 |
() -> { |
| 84 |
retries.increment(); |
| 85 |
return retries.sum() == 2 ? "got it" : null; |
| 86 |
}, |
| 87 |
null, |
| 88 |
"notHappy"); |
| 89 |
assertThat(result, equalTo("got it")); |
| 90 |
assertThat(retries.sum(), equalTo(2L)); |
| 91 |
} |
| 92 |
|
| 93 |
@Test |
| 94 |
public void happyFirstAttempt() throws InterruptedException { |
| 95 |
LongAdder retries = new LongAdder(); |
| 96 |
String result = retry( |
| 97 |
5, |
| 98 |
RETRY_SLEEP_MILLIS, |
| 99 |
() -> { |
| 100 |
retries.increment(); |
| 101 |
return "happy"; |
| 102 |
}, |
| 103 |
null, |
| 104 |
"notHappy"); |
| 105 |
assertThat(result, equalTo("happy")); |
| 106 |
assertThat(retries.sum(), equalTo(1L)); |
| 107 |
} |
| 108 |
|
| 109 |
@Test |
| 110 |
public void notHappyAnyAttempt() throws InterruptedException { |
| 111 |
LongAdder retries = new LongAdder(); |
| 112 |
String result = retry( |
| 113 |
5, |
| 114 |
RETRY_SLEEP_MILLIS, |
| 115 |
() -> { |
| 116 |
retries.increment(); |
| 117 |
return null; |
| 118 |
}, |
| 119 |
null, |
| 120 |
"notHappy"); |
| 121 |
assertThat(result, equalTo("notHappy")); |
| 122 |
assertThat(retries.sum(), equalTo(5L)); |
| 123 |
} |
| 124 |
|
| 125 |
@Test |
| 126 |
public void happyAfterSomeAttempt() throws InterruptedException { |
| 127 |
LongAdder retries = new LongAdder(); |
| 128 |
String result = retry( |
| 129 |
5, |
| 130 |
RETRY_SLEEP_MILLIS, |
| 131 |
() -> { |
| 132 |
retries.increment(); |
| 133 |
return retries.sum() == 3 ? "got it" : null; |
| 134 |
}, |
| 135 |
null, |
| 136 |
"notHappy"); |
| 137 |
assertThat(result, equalTo("got it")); |
| 138 |
assertThat(retries.sum(), equalTo(3L)); |
| 139 |
} |
| 140 |
} |