Skip to content

Commit 77222d6

Browse files
yanhom1314claude
andcommitted
test: add unit tests for LimitedUniformReservoir
Co-Authored-By: Claude <noreply@anthropic.com>
1 parent ca5d198 commit 77222d6

1 file changed

Lines changed: 134 additions & 0 deletions

File tree

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.dromara.dynamictp.test.core.metric;
19+
20+
import com.codahale.metrics.Snapshot;
21+
import org.dromara.dynamictp.core.metric.LimitedUniformReservoir;
22+
import org.junit.jupiter.api.Test;
23+
24+
import static org.junit.jupiter.api.Assertions.assertEquals;
25+
import static org.junit.jupiter.api.Assertions.assertTrue;
26+
27+
/**
28+
* LimitedUniformReservoir test
29+
*
30+
* @author yanhom
31+
* @since 1.2.2
32+
*/
33+
class LimitedUniformReservoirTest {
34+
35+
@Test
36+
void testInitialState() {
37+
LimitedUniformReservoir reservoir = new LimitedUniformReservoir();
38+
assertEquals(0, reservoir.size());
39+
}
40+
41+
@Test
42+
void testUpdateIncrementsSize() {
43+
LimitedUniformReservoir reservoir = new LimitedUniformReservoir();
44+
reservoir.update(100);
45+
assertEquals(1, reservoir.size());
46+
47+
reservoir.update(200);
48+
assertEquals(2, reservoir.size());
49+
}
50+
51+
@Test
52+
void testSizeCappedAtDefaultSize() {
53+
LimitedUniformReservoir reservoir = new LimitedUniformReservoir();
54+
for (int i = 0; i < 5000; i++) {
55+
reservoir.update(i);
56+
}
57+
// Should be capped at 4096
58+
assertEquals(4096, reservoir.size());
59+
}
60+
61+
@Test
62+
void testGetSnapshot() {
63+
LimitedUniformReservoir reservoir = new LimitedUniformReservoir();
64+
reservoir.update(10);
65+
reservoir.update(20);
66+
reservoir.update(30);
67+
68+
Snapshot snapshot = reservoir.getSnapshot();
69+
assertEquals(3, snapshot.size());
70+
assertEquals(20.0, snapshot.getMedian(), 1.0);
71+
}
72+
73+
@Test
74+
void testGetSnapshotPercentiles() {
75+
LimitedUniformReservoir reservoir = new LimitedUniformReservoir();
76+
for (int i = 1; i <= 100; i++) {
77+
reservoir.update(i);
78+
}
79+
80+
Snapshot snapshot = reservoir.getSnapshot();
81+
assertEquals(100, snapshot.size());
82+
assertTrue(snapshot.getMedian() >= 40 && snapshot.getMedian() <= 60);
83+
assertTrue(snapshot.get95thPercentile() >= 90);
84+
assertTrue(snapshot.get99thPercentile() >= 95);
85+
}
86+
87+
@Test
88+
void testReset() {
89+
LimitedUniformReservoir reservoir = new LimitedUniformReservoir();
90+
reservoir.update(100);
91+
reservoir.update(200);
92+
assertEquals(2, reservoir.size());
93+
94+
reservoir.reset();
95+
assertEquals(0, reservoir.size());
96+
}
97+
98+
@Test
99+
void testResetAndReuse() {
100+
LimitedUniformReservoir reservoir = new LimitedUniformReservoir();
101+
reservoir.update(100);
102+
reservoir.reset();
103+
reservoir.update(42);
104+
105+
Snapshot snapshot = reservoir.getSnapshot();
106+
assertEquals(1, snapshot.size());
107+
assertEquals(42, snapshot.getMedian(), 0.001);
108+
}
109+
110+
@Test
111+
void testConcurrentUpdates() throws InterruptedException {
112+
LimitedUniformReservoir reservoir = new LimitedUniformReservoir();
113+
int threadCount = 10;
114+
int updatesPerThread = 500;
115+
Thread[] threads = new Thread[threadCount];
116+
117+
for (int i = 0; i < threadCount; i++) {
118+
threads[i] = new Thread(() -> {
119+
for (int j = 0; j < updatesPerThread; j++) {
120+
reservoir.update(j);
121+
}
122+
});
123+
threads[i].start();
124+
}
125+
126+
for (Thread t : threads) {
127+
t.join();
128+
}
129+
130+
// All 5000 updates done, size should be capped at 4096
131+
assertTrue(reservoir.size() <= 4096);
132+
assertTrue(reservoir.size() > 0);
133+
}
134+
}

0 commit comments

Comments
 (0)