|
| 1 | +package com.eternalcode.commons.delay; |
| 2 | + |
| 3 | +import org.junit.jupiter.api.Test; |
| 4 | + |
| 5 | +import java.time.Duration; |
| 6 | +import java.time.Instant; |
| 7 | +import java.util.UUID; |
| 8 | + |
| 9 | +import static java.util.concurrent.TimeUnit.MILLISECONDS; |
| 10 | +import static org.assertj.core.api.Assertions.assertThat; |
| 11 | +import static org.awaitility.Awaitility.await; |
| 12 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 13 | + |
| 14 | +class DelayTest { |
| 15 | + |
| 16 | + @Test |
| 17 | + void shouldExpireAfterDefaultDelay() { |
| 18 | + Delay<UUID> delay = Delay.withDefault(() -> Duration.ofMillis(500)); |
| 19 | + UUID key = UUID.randomUUID(); |
| 20 | + |
| 21 | + delay.markDelay(key); |
| 22 | + assertThat(delay.hasDelay(key)).isTrue(); |
| 23 | + |
| 24 | + await() |
| 25 | + .pollDelay(250, MILLISECONDS) |
| 26 | + .atMost(500, MILLISECONDS) |
| 27 | + .until(() -> delay.hasDelay(key)); |
| 28 | + |
| 29 | + await() |
| 30 | + .atMost(Duration.ofMillis(350)) // After previously await (600 ms - 900 ms) |
| 31 | + .until(() -> !delay.hasDelay(key)); |
| 32 | + } |
| 33 | + |
| 34 | + @Test |
| 35 | + void shouldDoNotExpireBeforeCustomDelay() { |
| 36 | + Delay<UUID> delay = Delay.withDefault(() -> Duration.ofMillis(500)); |
| 37 | + UUID key = UUID.randomUUID(); |
| 38 | + |
| 39 | + delay.markDelay(key, Duration.ofMillis(1000)); |
| 40 | + assertThat(delay.hasDelay(key)).isTrue(); |
| 41 | + |
| 42 | + await() |
| 43 | + .pollDelay(500, MILLISECONDS) |
| 44 | + .atMost(1000, MILLISECONDS) |
| 45 | + .until(() -> delay.hasDelay(key)); |
| 46 | + |
| 47 | + await() |
| 48 | + .atMost(600, MILLISECONDS) // After previously await (1100 ms - 1600 ms) |
| 49 | + .until(() -> !delay.hasDelay(key)); |
| 50 | + } |
| 51 | + |
| 52 | + @Test |
| 53 | + void shouldUnmarkDelay() { |
| 54 | + Delay<UUID> delay = Delay.withDefault(() -> Duration.ofMillis(500)); |
| 55 | + UUID key = UUID.randomUUID(); |
| 56 | + |
| 57 | + delay.markDelay(key); |
| 58 | + assertThat(delay.hasDelay(key)).isTrue(); |
| 59 | + |
| 60 | + delay.unmarkDelay(key); |
| 61 | + assertThat(delay.hasDelay(key)).isFalse(); |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + void shouldNotHaveDelayOnNonExistentKey() { |
| 66 | + Delay<UUID> delay = Delay.withDefault(() -> Duration.ofMillis(500)); |
| 67 | + UUID key = UUID.randomUUID(); |
| 68 | + |
| 69 | + assertThat(delay.hasDelay(key)).isFalse(); |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + void shouldReturnCorrectRemainingTime() { |
| 74 | + Delay<UUID> delay = Delay.withDefault(() -> Duration.ofMillis(500)); |
| 75 | + UUID key = UUID.randomUUID(); |
| 76 | + |
| 77 | + delay.markDelay(key, Duration.ofMillis(1000)); |
| 78 | + |
| 79 | + // Immediately after marking, remaining time should be close to the full delay |
| 80 | + assertThat(delay.getRemaining(key)) |
| 81 | + .isCloseTo(Duration.ofMillis(1000), Duration.ofMillis(150)); |
| 82 | + |
| 83 | + // Wait for some time |
| 84 | + await() |
| 85 | + .pollDelay(400, MILLISECONDS) |
| 86 | + .atMost(550, MILLISECONDS) |
| 87 | + .untilAsserted(() -> { |
| 88 | + // After 400ms, remaining time should be less than the original |
| 89 | + assertThat(delay.getRemaining(key)).isLessThan(Duration.ofMillis(1000).minus(Duration.ofMillis(300))); |
| 90 | + }); |
| 91 | + |
| 92 | + await() |
| 93 | + .atMost(Duration.ofMillis(1000).plus(Duration.ofMillis(150))) |
| 94 | + .until(() -> !delay.hasDelay(key)); |
| 95 | + |
| 96 | + // After expiration, remaining time should be negative |
| 97 | + assertThat(delay.getRemaining(key)).isZero(); |
| 98 | + } |
| 99 | + |
| 100 | + @Test |
| 101 | + void shouldHandleMultipleKeysIndependently() { |
| 102 | + Delay<UUID> delay = Delay.withDefault(() -> Duration.ofMillis(500)); |
| 103 | + UUID shortTimeKey = UUID.randomUUID(); // 500ms |
| 104 | + UUID longTimeKey = UUID.randomUUID(); // 1000ms |
| 105 | + |
| 106 | + delay.markDelay(shortTimeKey); |
| 107 | + delay.markDelay(longTimeKey, Duration.ofMillis(1000)); |
| 108 | + |
| 109 | + assertThat(delay.hasDelay(shortTimeKey)).isTrue(); |
| 110 | + assertThat(delay.hasDelay(longTimeKey)).isTrue(); |
| 111 | + |
| 112 | + // Wait for the first key to expire |
| 113 | + await() |
| 114 | + .atMost(Duration.ofMillis(500).plus(Duration.ofMillis(150))) |
| 115 | + .until(() -> !delay.hasDelay(shortTimeKey)); |
| 116 | + |
| 117 | + // After first key expires, second should still be active |
| 118 | + assertThat(delay.hasDelay(shortTimeKey)).isFalse(); |
| 119 | + assertThat(delay.hasDelay(longTimeKey)).isTrue(); |
| 120 | + |
| 121 | + // Wait for the second key to expire |
| 122 | + await() |
| 123 | + .atMost(Duration.ofMillis(1000)) |
| 124 | + .until(() -> !delay.hasDelay(longTimeKey)); |
| 125 | + |
| 126 | + assertThat(delay.hasDelay(longTimeKey)).isFalse(); |
| 127 | + } |
| 128 | + |
| 129 | + @Test |
| 130 | + void testExpireAfterCreate_withOverflow_shouldReturnMaxValue() { |
| 131 | + Delay.InstantExpiry<String> expiry = new Delay.InstantExpiry<>(); |
| 132 | + Instant farFuture = Instant.now().plus(Duration.ofDays(1000000000)); |
| 133 | + |
| 134 | + long result = expiry.expireAfterCreate("key", farFuture, 0); |
| 135 | + |
| 136 | + assertEquals(Long.MAX_VALUE, result); |
| 137 | + } |
| 138 | + |
| 139 | + @Test |
| 140 | + void testExpireAfterCreate_withOverflow_shouldReturnMinValue() { |
| 141 | + Delay.InstantExpiry<String> expiry = new Delay.InstantExpiry<>(); |
| 142 | + Instant farPast = Instant.now().minus(Duration.ofDays(1000000000)); |
| 143 | + |
| 144 | + long result = expiry.expireAfterCreate("key", farPast, 0); |
| 145 | + |
| 146 | + assertEquals(Long.MIN_VALUE, result); |
| 147 | + } |
| 148 | + |
| 149 | + @Test |
| 150 | + void testSuperLargeDelay() { |
| 151 | + Delay<UUID> delay = Delay.withDefault(() -> Duration.ofDays(1000000000)); |
| 152 | + UUID key = UUID.randomUUID(); |
| 153 | + |
| 154 | + delay.markDelay(key); |
| 155 | + assertThat(delay.hasDelay(key)).isTrue(); |
| 156 | + |
| 157 | + await() |
| 158 | + .atMost(Duration.ofSeconds(1)) |
| 159 | + .until(() -> delay.hasDelay(key)); |
| 160 | + |
| 161 | + // Even after waiting, the delay should still be active due to the large duration |
| 162 | + assertThat(delay.hasDelay(key)).isTrue(); |
| 163 | + } |
| 164 | +} |
0 commit comments