|
| 1 | +package rocks.inspectit.ocelot.eum.server.metrics.timewindow.views; |
| 2 | + |
| 3 | +import org.junit.jupiter.api.Nested; |
| 4 | +import org.junit.jupiter.api.Test; |
| 5 | + |
| 6 | +import java.time.Duration; |
| 7 | +import java.util.stream.IntStream; |
| 8 | + |
| 9 | +import static org.assertj.core.api.Assertions.assertThat; |
| 10 | +import static org.assertj.core.api.Assertions.assertThatThrownBy; |
| 11 | + |
| 12 | +public class WindowedDoubleQueueTest { |
| 13 | + |
| 14 | + @Nested |
| 15 | + class RoundUpToPowerOfTwo { |
| 16 | + |
| 17 | + @Test |
| 18 | + public void nonPowerOfTwoValues() { |
| 19 | + assertThat(WindowedDoubleQueue.roundUpToPowerOfTwo(0)).isEqualTo(0); |
| 20 | + assertThat(WindowedDoubleQueue.roundUpToPowerOfTwo(3)).isEqualTo(4); |
| 21 | + assertThat(WindowedDoubleQueue.roundUpToPowerOfTwo(5)).isEqualTo(8); |
| 22 | + assertThat(WindowedDoubleQueue.roundUpToPowerOfTwo(7)).isEqualTo(8); |
| 23 | + assertThat(WindowedDoubleQueue.roundUpToPowerOfTwo(22)).isEqualTo(32); |
| 24 | + assertThat(WindowedDoubleQueue.roundUpToPowerOfTwo(5000)).isEqualTo(8192); |
| 25 | + } |
| 26 | + |
| 27 | + @Test |
| 28 | + public void powerOfTwoValues() { |
| 29 | + assertThat(WindowedDoubleQueue.roundUpToPowerOfTwo(2)).isEqualTo(2); |
| 30 | + assertThat(WindowedDoubleQueue.roundUpToPowerOfTwo(4)).isEqualTo(4); |
| 31 | + assertThat(WindowedDoubleQueue.roundUpToPowerOfTwo(16)).isEqualTo(16); |
| 32 | + assertThat(WindowedDoubleQueue.roundUpToPowerOfTwo(256)).isEqualTo(256); |
| 33 | + assertThat(WindowedDoubleQueue.roundUpToPowerOfTwo(8192)).isEqualTo(8192); |
| 34 | + } |
| 35 | + |
| 36 | + } |
| 37 | + |
| 38 | + @Nested |
| 39 | + public class Insert { |
| 40 | + |
| 41 | + @Test |
| 42 | + void testAlignedGrowth() { |
| 43 | + WindowedDoubleQueue queue = new WindowedDoubleQueue(Duration.ofMillis(1)); |
| 44 | + |
| 45 | + for (int i = 0; i <= WindowedDoubleQueue.MIN_CAPACITY; i++) { |
| 46 | + queue.insert(i * 100 + 1, 42); |
| 47 | + |
| 48 | + } |
| 49 | + |
| 50 | + double[] expectedResult = IntStream.range(0, WindowedDoubleQueue.MIN_CAPACITY + 1) |
| 51 | + .mapToDouble(i -> i * 100 + 1) |
| 52 | + .toArray(); |
| 53 | + double[] result = queue.copy(); |
| 54 | + assertThat(result).isEqualTo(expectedResult); |
| 55 | + |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + void testUnalignedGrowth() { |
| 60 | + WindowedDoubleQueue queue = new WindowedDoubleQueue(Duration.ofMillis(1)); |
| 61 | + |
| 62 | + queue.insert(-12345, 0); |
| 63 | + queue.insert(-12345, 0); |
| 64 | + queue.insert(-12345, 0); |
| 65 | + for (int i = 0; i <= WindowedDoubleQueue.MIN_CAPACITY; i++) { |
| 66 | + queue.removeStaleValues(1); |
| 67 | + queue.insert(i * 100 + 1, 1); |
| 68 | + |
| 69 | + } |
| 70 | + |
| 71 | + double[] expectedResult = IntStream.range(0, WindowedDoubleQueue.MIN_CAPACITY + 1) |
| 72 | + .mapToDouble(i -> i * 100 + 1) |
| 73 | + .toArray(); |
| 74 | + double[] result = queue.copy(); |
| 75 | + assertThat(result).isEqualTo(expectedResult); |
| 76 | + |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + void verifyStreamingMaintainsCapacity() { |
| 81 | + WindowedDoubleQueue queue = new WindowedDoubleQueue(Duration.ofMillis(WindowedDoubleQueue.MIN_CAPACITY)); |
| 82 | + |
| 83 | + for (int i = 0; i < WindowedDoubleQueue.MIN_CAPACITY * 10; i++) { |
| 84 | + queue.removeStaleValues(i); |
| 85 | + queue.insert(i, i); |
| 86 | + } |
| 87 | + |
| 88 | + assertThat(queue.capacity()).isEqualTo(WindowedDoubleQueue.MIN_CAPACITY); |
| 89 | + assertThat(queue.size()).isEqualTo(WindowedDoubleQueue.MIN_CAPACITY); |
| 90 | + } |
| 91 | + |
| 92 | + @Test |
| 93 | + void invalidTimestamp() { |
| 94 | + WindowedDoubleQueue queue = new WindowedDoubleQueue(Duration.ofMillis(42)); |
| 95 | + |
| 96 | + queue.insert(1.0, 10); |
| 97 | + assertThatThrownBy(() -> queue.insert(2.0, 9)).isInstanceOf(IllegalArgumentException.class); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + @Nested |
| 102 | + class RemoveStaleValues { |
| 103 | + |
| 104 | + @Test |
| 105 | + void removeAllValues() { |
| 106 | + WindowedDoubleQueue queue = new WindowedDoubleQueue(Duration.ofMillis(1)); |
| 107 | + |
| 108 | + for (int i = 0; i < WindowedDoubleQueue.MIN_CAPACITY * 100; i++) { |
| 109 | + queue.insert(i, 0); |
| 110 | + } |
| 111 | + int removed = queue.removeStaleValues(1); |
| 112 | + |
| 113 | + assertThat(removed).isEqualTo(WindowedDoubleQueue.MIN_CAPACITY * 100); |
| 114 | + assertThat(queue.capacity()).isEqualTo(WindowedDoubleQueue.MIN_CAPACITY); |
| 115 | + assertThat(queue.size()).isEqualTo(0); |
| 116 | + } |
| 117 | + |
| 118 | + @Test |
| 119 | + void removeAllExceptOneValues() { |
| 120 | + WindowedDoubleQueue queue = new WindowedDoubleQueue(Duration.ofMillis(2)); |
| 121 | + |
| 122 | + for (int i = 0; i < WindowedDoubleQueue.MIN_CAPACITY * 100; i++) { |
| 123 | + queue.insert(i, 0); |
| 124 | + } |
| 125 | + queue.insert(42, 1); |
| 126 | + int removed = queue.removeStaleValues(2); |
| 127 | + |
| 128 | + assertThat(removed).isEqualTo(WindowedDoubleQueue.MIN_CAPACITY * 100); |
| 129 | + assertThat(queue.capacity()).isEqualTo(WindowedDoubleQueue.MIN_CAPACITY); |
| 130 | + assertThat(queue.copy()).contains(42); |
| 131 | + } |
| 132 | + |
| 133 | + @Test |
| 134 | + void removeAllExceptFew() { |
| 135 | + WindowedDoubleQueue queue = new WindowedDoubleQueue(Duration.ofMillis(2)); |
| 136 | + |
| 137 | + int keepCount = WindowedDoubleQueue.MIN_CAPACITY + 1; |
| 138 | + for (int i = 0; i < WindowedDoubleQueue.MIN_CAPACITY * 100; i++) { |
| 139 | + queue.insert(-9999999, 0); |
| 140 | + } |
| 141 | + for (int i = 0; i < keepCount; i++) { |
| 142 | + queue.insert(42 + i, 1); |
| 143 | + } |
| 144 | + int removed = queue.removeStaleValues(2); |
| 145 | + |
| 146 | + double[] expectedResult = IntStream.range(0, keepCount).mapToDouble(i -> 42 + i).toArray(); |
| 147 | + assertThat(removed).isEqualTo(WindowedDoubleQueue.MIN_CAPACITY * 100); |
| 148 | + assertThat(queue.capacity()).isEqualTo(WindowedDoubleQueue.MIN_CAPACITY * 4); |
| 149 | + assertThat(queue.copy()).isEqualTo(expectedResult); |
| 150 | + } |
| 151 | + |
| 152 | + @Test |
| 153 | + void removeAllExceptFewWithOverflow() { |
| 154 | + WindowedDoubleQueue queue = new WindowedDoubleQueue(Duration.ofMillis(WindowedDoubleQueue.MIN_CAPACITY * 2)); |
| 155 | + |
| 156 | + int keepCount = WindowedDoubleQueue.MIN_CAPACITY + 1; |
| 157 | + int time = 0; |
| 158 | + |
| 159 | + for (int i = 0; i < WindowedDoubleQueue.MIN_CAPACITY * 3; i++) { |
| 160 | + queue.removeStaleValues(time); |
| 161 | + queue.insert(-9999999, time); |
| 162 | + time++; |
| 163 | + } |
| 164 | + for (int i = 0; i < keepCount; i++) { |
| 165 | + queue.removeStaleValues(time + 1); |
| 166 | + queue.insert(42 + i, time + 1); |
| 167 | + } |
| 168 | + int removed = queue.removeStaleValues(time + WindowedDoubleQueue.MIN_CAPACITY * 2); |
| 169 | + |
| 170 | + assertThat(removed).isEqualTo(WindowedDoubleQueue.MIN_CAPACITY * 3 - keepCount - 1); |
| 171 | + double[] expectedResult = IntStream.range(0, keepCount).mapToDouble(i -> 42 + i).toArray(); |
| 172 | + assertThat(queue.capacity()).isEqualTo(WindowedDoubleQueue.MIN_CAPACITY * 4); |
| 173 | + assertThat(queue.copy()).isEqualTo(expectedResult); |
| 174 | + } |
| 175 | + |
| 176 | + } |
| 177 | + |
| 178 | + @Nested |
| 179 | + class Copy { |
| 180 | + |
| 181 | + @Test |
| 182 | + void copyEmptyIntoNewBuffer() { |
| 183 | + WindowedDoubleQueue queue = new WindowedDoubleQueue(Duration.ofMillis(1)); |
| 184 | + |
| 185 | + double[] copy = queue.copy(); |
| 186 | + |
| 187 | + assertThat(copy).isEmpty(); |
| 188 | + } |
| 189 | + |
| 190 | + @Test |
| 191 | + void copyEmptyIntoExistingBuffer() { |
| 192 | + WindowedDoubleQueue queue = new WindowedDoubleQueue(Duration.ofMillis(1)); |
| 193 | + double[] buffer = new double[42]; |
| 194 | + |
| 195 | + queue.copy(buffer); |
| 196 | + |
| 197 | + assertThat(buffer).containsOnly(0); |
| 198 | + } |
| 199 | + |
| 200 | + @Test |
| 201 | + void copyValuesIntoNewBuffer() { |
| 202 | + WindowedDoubleQueue queue = new WindowedDoubleQueue(Duration.ofMillis(1)); |
| 203 | + for (int i = 0; i < 100; i++) { |
| 204 | + queue.insert(i, 0); |
| 205 | + } |
| 206 | + |
| 207 | + double[] result = queue.copy(); |
| 208 | + |
| 209 | + double[] expectedResult = IntStream.range(0, 100).mapToDouble(i -> i).toArray(); |
| 210 | + assertThat(result).isEqualTo(expectedResult); |
| 211 | + } |
| 212 | + |
| 213 | + @Test |
| 214 | + void copyValuesIntoExistingBuffer() { |
| 215 | + WindowedDoubleQueue queue = new WindowedDoubleQueue(Duration.ofMillis(1)); |
| 216 | + double[] buffer = new double[100]; |
| 217 | + for (int i = 0; i < 100; i++) { |
| 218 | + queue.insert(i, 0); |
| 219 | + } |
| 220 | + |
| 221 | + queue.copy(buffer); |
| 222 | + |
| 223 | + double[] expectedResult = IntStream.range(0, 100).mapToDouble(i -> i).toArray(); |
| 224 | + assertThat(buffer).isEqualTo(expectedResult); |
| 225 | + } |
| 226 | + |
| 227 | + @Test |
| 228 | + void copyValuesIntoTooSmallBuffer() { |
| 229 | + WindowedDoubleQueue queue = new WindowedDoubleQueue(Duration.ofMillis(1)); |
| 230 | + for (int i = 0; i < 100; i++) { |
| 231 | + queue.insert(i, 0); |
| 232 | + } |
| 233 | + |
| 234 | + assertThatThrownBy(() -> queue.copy(new double[99])).isInstanceOf(IllegalArgumentException.class); |
| 235 | + } |
| 236 | + } |
| 237 | +} |
0 commit comments