-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDelayTest.java
More file actions
164 lines (124 loc) · 5.32 KB
/
DelayTest.java
File metadata and controls
164 lines (124 loc) · 5.32 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package com.eternalcode.commons.delay;
import org.junit.jupiter.api.Test;
import java.time.Duration;
import java.time.Instant;
import java.util.UUID;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static org.assertj.core.api.Assertions.assertThat;
import static org.awaitility.Awaitility.await;
import static org.junit.jupiter.api.Assertions.assertEquals;
class DelayTest {
@Test
void shouldExpireAfterDefaultDelay() {
Delay<UUID> delay = Delay.withDefault(() -> Duration.ofMillis(500));
UUID key = UUID.randomUUID();
delay.markDelay(key);
assertThat(delay.hasDelay(key)).isTrue();
await()
.pollDelay(250, MILLISECONDS)
.atMost(500, MILLISECONDS)
.until(() -> delay.hasDelay(key));
await()
.atMost(Duration.ofMillis(350)) // After previously await (600 ms - 900 ms)
.until(() -> !delay.hasDelay(key));
}
@Test
void shouldDoNotExpireBeforeCustomDelay() {
Delay<UUID> delay = Delay.withDefault(() -> Duration.ofMillis(500));
UUID key = UUID.randomUUID();
delay.markDelay(key, Duration.ofMillis(1000));
assertThat(delay.hasDelay(key)).isTrue();
await()
.pollDelay(500, MILLISECONDS)
.atMost(1000, MILLISECONDS)
.until(() -> delay.hasDelay(key));
await()
.atMost(600, MILLISECONDS) // After previously await (1100 ms - 1600 ms)
.until(() -> !delay.hasDelay(key));
}
@Test
void shouldUnmarkDelay() {
Delay<UUID> delay = Delay.withDefault(() -> Duration.ofMillis(500));
UUID key = UUID.randomUUID();
delay.markDelay(key);
assertThat(delay.hasDelay(key)).isTrue();
delay.unmarkDelay(key);
assertThat(delay.hasDelay(key)).isFalse();
}
@Test
void shouldNotHaveDelayOnNonExistentKey() {
Delay<UUID> delay = Delay.withDefault(() -> Duration.ofMillis(500));
UUID key = UUID.randomUUID();
assertThat(delay.hasDelay(key)).isFalse();
}
@Test
void shouldReturnCorrectRemainingTime() {
Delay<UUID> delay = Delay.withDefault(() -> Duration.ofMillis(500));
UUID key = UUID.randomUUID();
delay.markDelay(key, Duration.ofMillis(1000));
// Immediately after marking, remaining time should be close to the full delay
assertThat(delay.getRemaining(key))
.isCloseTo(Duration.ofMillis(1000), Duration.ofMillis(150));
// Wait for some time
await()
.pollDelay(400, MILLISECONDS)
.atMost(550, MILLISECONDS)
.untilAsserted(() -> {
// After 400ms, remaining time should be less than the original
assertThat(delay.getRemaining(key)).isLessThan(Duration.ofMillis(1000).minus(Duration.ofMillis(300)));
});
await()
.atMost(Duration.ofMillis(1000).plus(Duration.ofMillis(150)))
.until(() -> !delay.hasDelay(key));
// After expiration, remaining time should be negative
assertThat(delay.getRemaining(key)).isZero();
}
@Test
void shouldHandleMultipleKeysIndependently() {
Delay<UUID> delay = Delay.withDefault(() -> Duration.ofMillis(500));
UUID shortTimeKey = UUID.randomUUID(); // 500ms
UUID longTimeKey = UUID.randomUUID(); // 1000ms
delay.markDelay(shortTimeKey);
delay.markDelay(longTimeKey, Duration.ofMillis(1000));
assertThat(delay.hasDelay(shortTimeKey)).isTrue();
assertThat(delay.hasDelay(longTimeKey)).isTrue();
// Wait for the first key to expire
await()
.atMost(Duration.ofMillis(500).plus(Duration.ofMillis(150)))
.until(() -> !delay.hasDelay(shortTimeKey));
// After first key expires, second should still be active
assertThat(delay.hasDelay(shortTimeKey)).isFalse();
assertThat(delay.hasDelay(longTimeKey)).isTrue();
// Wait for the second key to expire
await()
.atMost(Duration.ofMillis(1000))
.until(() -> !delay.hasDelay(longTimeKey));
assertThat(delay.hasDelay(longTimeKey)).isFalse();
}
@Test
void testExpireAfterCreate_withOverflow_shouldReturnMaxValue() {
Delay.InstantExpiry<String> expiry = new Delay.InstantExpiry<>();
Instant farFuture = Instant.now().plus(Duration.ofDays(1000000000));
long result = expiry.expireAfterCreate("key", farFuture, 0);
assertEquals(Long.MAX_VALUE, result);
}
@Test
void testExpireAfterCreate_withOverflow_shouldReturnMinValue() {
Delay.InstantExpiry<String> expiry = new Delay.InstantExpiry<>();
Instant farPast = Instant.now().minus(Duration.ofDays(1000000000));
long result = expiry.expireAfterCreate("key", farPast, 0);
assertEquals(Long.MIN_VALUE, result);
}
@Test
void testSuperLargeDelay() {
Delay<UUID> delay = Delay.withDefault(() -> Duration.ofDays(1000000000));
UUID key = UUID.randomUUID();
delay.markDelay(key);
assertThat(delay.hasDelay(key)).isTrue();
await()
.atMost(Duration.ofSeconds(1))
.until(() -> delay.hasDelay(key));
// Even after waiting, the delay should still be active due to the large duration
assertThat(delay.hasDelay(key)).isTrue();
}
}