-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathEventuallyConsistentTest.java
More file actions
66 lines (58 loc) · 2.36 KB
/
EventuallyConsistentTest.java
File metadata and controls
66 lines (58 loc) · 2.36 KB
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
package example;
import org.junit.ComparisonFailure;
import org.junit.Test;
import java.time.Duration;
import java.time.Instant;
import static example.EventuallyConsistent.eventually;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.fail;
public class EventuallyConsistentTest {
@Test
public void testEventuallySucceeds() {
final Instant start = Instant.now();
try {
eventually(Duration.ofSeconds(10), () -> {
// Expected result (true) is returned only after 3 seconds
final boolean success = Instant.now().isAfter(start.plus(Duration.ofSeconds(3))) ? true : false;
// This will fail exceptionally (in the form of a ComparisonFailure)
// as long as the result doesn't have the expected value yet
assertThat(success).isEqualTo(true);
// This could also be a call to a database, webservice, you-name-it, that takes a while to be updated
// For example:
//
// Response response = myWebservice.retrieveSomething(id);
// assertThat(response.getSomething().getName()).isEqualTo("foo");
//
// Just make sure that you retrieve the value within this command (not outside otherwise it will not be retried)
});
} catch (Throwable t) {
t.printStackTrace();
fail("Should have succeeded after ~ 3 seconds");
}
}
@Test
public void testNeverSucceeds() {
try {
eventually(Duration.ofSeconds(2), () -> {
// Will always be false, while true is expected at one point in time
final boolean success = false;
assertThat(success).isEqualTo(true);
});
fail("Should never have succeeded");
} catch (ComparisonFailure cf) {
assertThat(cf).hasMessage("expected:<[tru]e> but was:<[fals]e>");
}
}
@Test
public void testSucceedsRightAway() {
try {
eventually(Duration.ofSeconds(2), () -> {
final boolean success = true;
assertThat(success).isEqualTo(true);
});
} catch (Throwable t) {
t.printStackTrace();
fail("Should have succeeded right away");
}
}
}