1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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.jupiter.api.Test;
25
26 import static org.eclipse.aether.named.support.Retry.retry;
27 import static org.junit.jupiter.api.Assertions.*;
28
29
30
31
32 public class RetryTest {
33 private static final long RETRY_SLEEP_MILLIS = 250L;
34
35 @Test
36 void happy() throws InterruptedException {
37 LongAdder retries = new LongAdder();
38 String result = retry(
39 1L,
40 TimeUnit.SECONDS,
41 RETRY_SLEEP_MILLIS,
42 () -> {
43 retries.increment();
44 return "happy";
45 },
46 null,
47 "notHappy");
48 assertEquals(result, "happy");
49 assertEquals(retries.sum(), 1);
50 }
51
52 @Test
53 void notHappy() throws InterruptedException {
54 LongAdder retries = new LongAdder();
55 String result = retry(
56 1L,
57 TimeUnit.SECONDS,
58 RETRY_SLEEP_MILLIS,
59 () -> {
60 retries.increment();
61 return null;
62 },
63 null,
64 "notHappy");
65 assertEquals(result, "notHappy");
66 assertTrue(retries.sum() > 1, retries.sum() + " > 1");
67 }
68
69 @Test
70 void happyAfterSomeTime() throws InterruptedException {
71 LongAdder retries = new LongAdder();
72 String result = retry(
73 1L,
74 TimeUnit.SECONDS,
75 RETRY_SLEEP_MILLIS,
76 () -> {
77 retries.increment();
78 return retries.sum() == 2 ? "got it" : null;
79 },
80 null,
81 "notHappy");
82 assertEquals(result, "got it");
83 assertEquals(retries.sum(), 2);
84 }
85
86 @Test
87 void happyFirstAttempt() throws InterruptedException {
88 LongAdder retries = new LongAdder();
89 String result = retry(
90 5,
91 RETRY_SLEEP_MILLIS,
92 () -> {
93 retries.increment();
94 return "happy";
95 },
96 null,
97 "notHappy");
98 assertEquals(result, "happy");
99 assertEquals(retries.sum(), 1);
100 }
101
102 @Test
103 void notHappyAnyAttempt() throws InterruptedException {
104 LongAdder retries = new LongAdder();
105 String result = retry(
106 5,
107 RETRY_SLEEP_MILLIS,
108 () -> {
109 retries.increment();
110 return null;
111 },
112 null,
113 "notHappy");
114 assertEquals(result, "notHappy");
115 assertEquals(retries.sum(), 5);
116 }
117
118 @Test
119 void happyAfterSomeAttempt() throws InterruptedException {
120 LongAdder retries = new LongAdder();
121 String result = retry(
122 5,
123 RETRY_SLEEP_MILLIS,
124 () -> {
125 retries.increment();
126 return retries.sum() == 3 ? "got it" : null;
127 },
128 null,
129 "notHappy");
130 assertEquals(result, "got it");
131 assertEquals(retries.sum(), 3);
132 }
133 }