1 package org.eclipse.aether.named;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.util.concurrent.TimeUnit;
23 import java.util.concurrent.atomic.LongAdder;
24
25 import org.junit.Rule;
26 import org.junit.Test;
27 import org.junit.rules.TestName;
28
29 import static org.eclipse.aether.named.support.Retry.retry;
30 import static org.hamcrest.MatcherAssert.assertThat;
31 import static org.hamcrest.Matchers.equalTo;
32 import static org.hamcrest.Matchers.greaterThan;
33
34
35
36
37 public class RetryTest
38 {
39 private static final long RETRY_SLEEP_MILLIS = 250L;
40
41 @Rule
42 public TestName testName = new TestName();
43
44 @Test
45 public void happy() throws InterruptedException
46 {
47 LongAdder retries = new LongAdder();
48 String result = retry( 1L, TimeUnit.SECONDS, RETRY_SLEEP_MILLIS, () -> { retries.increment(); return "happy"; }, null, "notHappy" );
49 assertThat( result, equalTo( "happy" ) );
50 assertThat( retries.sum(), equalTo( 1L ) );
51 }
52
53 @Test
54 public void notHappy() throws InterruptedException
55 {
56 LongAdder retries = new LongAdder();
57 String result = retry( 1L, TimeUnit.SECONDS, RETRY_SLEEP_MILLIS, () -> { retries.increment(); return null; }, null, "notHappy" );
58 assertThat( result, equalTo( "notHappy" ) );
59 assertThat( retries.sum(), greaterThan( 1L ) );
60 }
61
62 @Test
63 public void happyOnSomeAttempt() throws InterruptedException
64 {
65 LongAdder retries = new LongAdder();
66 String result = retry( 1L, TimeUnit.SECONDS, RETRY_SLEEP_MILLIS, () -> { retries.increment(); return retries.sum() == 2 ? "got it" : null; }, null, "notHappy" );
67 assertThat( result, equalTo( "got it" ) );
68 assertThat( retries.sum(), equalTo( 2L ) );
69 }
70 }