/
Issue284Test.java
75 lines (61 loc) · 2.26 KB
/
Issue284Test.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package net.jodah.failsafe.issues;
import net.jodah.failsafe.Failsafe;
import net.jodah.failsafe.Fallback;
import net.jodah.failsafe.RetryPolicy;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import static org.testng.Assert.*;
@Test
public class Issue284Test {
AtomicInteger failedAttempt;
AtomicBoolean success;
AtomicBoolean failure;
AtomicBoolean executed;
Fallback<String> fallback;
RetryPolicy<String> retryPolicy = new RetryPolicy<String>().handleResult(null)
.onFailedAttempt(e -> failedAttempt.incrementAndGet())
.onSuccess(e -> success.set(true))
.onFailure(e -> failure.set(true));
@BeforeMethod
protected void beforeMethod() {
failedAttempt = new AtomicInteger();
success = new AtomicBoolean();
failure = new AtomicBoolean();
executed = new AtomicBoolean();
}
private Fallback<String> fallbackFor(String result) {
return Fallback.of(result)
.handleResult(null)
.onFailedAttempt(e -> failedAttempt.incrementAndGet())
.onSuccess(e -> success.set(true))
.onFailure(e -> failure.set(true));
}
public void testFallbackSuccess() {
fallback = fallbackFor("hello");
String result = Failsafe.with(fallback).get(() -> null);
assertEquals(result, "hello");
assertEquals(failedAttempt.get(), 1);
assertTrue(success.get(), "Fallback should have been successful");
}
public void testFallbackFailure() {
fallback = fallbackFor(null);
String result = Failsafe.with(fallback).get(() -> null);
assertNull(result);
assertEquals(failedAttempt.get(), 1);
assertTrue(failure.get(), "Fallback should have failed");
}
public void testRetryPolicySuccess() {
String result = Failsafe.with(retryPolicy).get(() -> !executed.getAndSet(true) ? null : "hello");
assertEquals(result, "hello");
assertEquals(failedAttempt.get(), 1);
assertTrue(success.get(), "RetryPolicy should have been successful");
}
public void testRetryPolicyFailure() {
String result = Failsafe.with(retryPolicy).get(() -> null);
assertNull(result);
assertEquals(failedAttempt.get(), 3);
assertTrue(failure.get(), "RetryPolicy should have failed");
}
}