Skip to content

Commit 039c5da

Browse files
Add JedisPoolConfig and ConnectionPoolConfig with tests
Signed-off-by: Shivansh Soni <s.soni.shivansh@gmail.com>
1 parent dfe29fc commit 039c5da

4 files changed

Lines changed: 194 additions & 2 deletions

File tree

src/main/java/io/valkey/ConnectionPoolConfig.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,26 @@
55

66
public class ConnectionPoolConfig extends GenericObjectPoolConfig<Connection> {
77

8+
private static final int DEFAULT_MIN_IDLE = 16;
9+
810
public ConnectionPoolConfig() {
11+
this(DEFAULT_MIN_IDLE);
12+
}
13+
14+
public ConnectionPoolConfig(int minIdle) {
15+
if (minIdle < 1) {
16+
throw new IllegalArgumentException(
17+
"minIdle must be at least 1, got: " + minIdle);
18+
}
19+
920
// defaults to make your life with connection pool easier :)
1021
setTestWhileIdle(true);
1122
setMinEvictableIdleTime(Duration.ofMillis(60000));
1223
setTimeBetweenEvictionRuns(Duration.ofMillis(30000));
1324
setNumTestsPerEvictionRun(-1);
25+
26+
setMinIdle(minIdle);
27+
setMaxIdle(2 * minIdle);
28+
setMaxTotal(2 * minIdle);
1429
}
15-
}
30+
}

src/main/java/io/valkey/JedisPoolConfig.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,26 @@
55

66
public class JedisPoolConfig extends GenericObjectPoolConfig<Jedis> {
77

8+
private static final int DEFAULT_MIN_IDLE = 16;
9+
810
public JedisPoolConfig() {
11+
this(DEFAULT_MIN_IDLE);
12+
}
13+
14+
public JedisPoolConfig(int minIdle) {
15+
if (minIdle < 1) {
16+
throw new IllegalArgumentException(
17+
"minIdle must be at least 1, got: " + minIdle);
18+
}
19+
920
// defaults to make your life with connection pool easier :)
1021
setTestWhileIdle(true);
1122
setMinEvictableIdleTime(Duration.ofMillis(60000));
1223
setTimeBetweenEvictionRuns(Duration.ofMillis(30000));
1324
setNumTestsPerEvictionRun(-1);
25+
26+
setMinIdle(minIdle);
27+
setMaxIdle(2 * minIdle);
28+
setMaxTotal(2 * minIdle);
1429
}
15-
}
30+
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package io.valkey;
2+
3+
import static org.junit.Assert.assertEquals;
4+
import static org.junit.Assert.assertTrue;
5+
import org.junit.Test;
6+
7+
public class ConnectionPoolConfigTest {
8+
9+
@Test
10+
public void testDefaultConstructor() {
11+
ConnectionPoolConfig config = new ConnectionPoolConfig();
12+
13+
// Verify performance defaults
14+
assertEquals("Default minIdle should be 16", 16, config.getMinIdle());
15+
assertEquals("Default maxIdle should be 32", 32, config.getMaxIdle());
16+
assertEquals("Default maxTotal should be 32", 32, config.getMaxTotal());
17+
18+
// Verify other defaults are still set
19+
assertTrue("testWhileIdle should be true", config.getTestWhileIdle());
20+
assertEquals("minEvictableIdleTime should be 60000ms",
21+
60000, config.getMinEvictableIdleTime().toMillis());
22+
assertEquals("timeBetweenEvictionRuns should be 30000ms",
23+
30000, config.getTimeBetweenEvictionRuns().toMillis());
24+
assertEquals("numTestsPerEvictionRun should be -1",
25+
-1, config.getNumTestsPerEvictionRun());
26+
}
27+
28+
@Test
29+
public void testClusterOptimizedConstructor() {
30+
int minIdle = 10;
31+
ConnectionPoolConfig config = new ConnectionPoolConfig(minIdle);
32+
33+
assertEquals("MinIdle should be 10", 10, config.getMinIdle());
34+
assertEquals("MaxIdle should be 20", 20, config.getMaxIdle());
35+
assertEquals("MaxTotal should be 20", 20, config.getMaxTotal());
36+
}
37+
38+
@Test
39+
public void testCustomMinIdle() {
40+
ConnectionPoolConfig config = new ConnectionPoolConfig(8);
41+
42+
assertEquals("MinIdle should be 8", 8, config.getMinIdle());
43+
assertEquals("MaxIdle should be 16", 16, config.getMaxIdle());
44+
assertEquals("MaxTotal should be 16", 16, config.getMaxTotal());
45+
}
46+
47+
@Test
48+
public void testLargeMinIdle() {
49+
ConnectionPoolConfig config = new ConnectionPoolConfig(50);
50+
51+
assertEquals("MinIdle should be 50", 50, config.getMinIdle());
52+
assertEquals("MaxIdle should be 100", 100, config.getMaxIdle());
53+
assertEquals("MaxTotal should be 100", 100, config.getMaxTotal());
54+
}
55+
56+
@Test(expected = IllegalArgumentException.class)
57+
public void testZeroMinIdle() {
58+
new ConnectionPoolConfig(0);
59+
}
60+
61+
@Test(expected = IllegalArgumentException.class)
62+
public void testNegativeMinIdle() {
63+
new ConnectionPoolConfig(-5);
64+
}
65+
66+
@Test
67+
public void testRatioConsistency() {
68+
// Test the 2x ratio is maintained across various values
69+
int[] testValues = { 1, 5, 8, 10, 16, 20, 32, 50, 100 };
70+
71+
for (int minIdle : testValues) {
72+
ConnectionPoolConfig config = new ConnectionPoolConfig(minIdle);
73+
assertEquals("maxIdle should be 2x minIdle for value " + minIdle,
74+
minIdle * 2, config.getMaxIdle());
75+
assertEquals("maxTotal should be 2x minIdle for value " + minIdle,
76+
minIdle * 2, config.getMaxTotal());
77+
assertEquals("maxIdle should equal maxTotal for value " + minIdle,
78+
config.getMaxIdle(), config.getMaxTotal());
79+
}
80+
}
81+
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package io.valkey;
2+
3+
import static org.junit.Assert.assertEquals;
4+
import static org.junit.Assert.assertTrue;
5+
import org.junit.Test;
6+
7+
public class JedisPoolConfigTest {
8+
9+
@Test
10+
public void testDefaultConstructor() {
11+
JedisPoolConfig config = new JedisPoolConfig();
12+
13+
// Verify performance defaults
14+
assertEquals("Default minIdle should be 16", 16, config.getMinIdle());
15+
assertEquals("Default maxIdle should be 32", 32, config.getMaxIdle());
16+
assertEquals("Default maxTotal should be 32", 32, config.getMaxTotal());
17+
18+
// Verify other defaults are still set
19+
assertTrue("testWhileIdle should be true", config.getTestWhileIdle());
20+
assertEquals("minEvictableIdleTime should be 60000ms",
21+
60000, config.getMinEvictableIdleTime().toMillis());
22+
assertEquals("timeBetweenEvictionRuns should be 30000ms",
23+
30000, config.getTimeBetweenEvictionRuns().toMillis());
24+
assertEquals("numTestsPerEvictionRun should be -1",
25+
-1, config.getNumTestsPerEvictionRun());
26+
}
27+
28+
@Test
29+
public void testOptimizedConstructor() {
30+
int minIdle = 16;
31+
JedisPoolConfig config = new JedisPoolConfig(minIdle);
32+
33+
assertEquals("MinIdle should match input", minIdle, config.getMinIdle());
34+
assertEquals("MaxIdle should be 2x MinIdle", 32, config.getMaxIdle());
35+
assertEquals("MaxTotal should be 2x MinIdle", 32, config.getMaxTotal());
36+
}
37+
38+
@Test
39+
public void testCustomMinIdle() {
40+
JedisPoolConfig config = new JedisPoolConfig(10);
41+
42+
assertEquals("MinIdle should be 10", 10, config.getMinIdle());
43+
assertEquals("MaxIdle should be 20", 20, config.getMaxIdle());
44+
assertEquals("MaxTotal should be 20", 20, config.getMaxTotal());
45+
}
46+
47+
@Test
48+
public void testLargeMinIdle() {
49+
JedisPoolConfig config = new JedisPoolConfig(50);
50+
51+
assertEquals("MinIdle should be 50", 50, config.getMinIdle());
52+
assertEquals("MaxIdle should be 100", 100, config.getMaxIdle());
53+
assertEquals("MaxTotal should be 100", 100, config.getMaxTotal());
54+
}
55+
56+
@Test(expected = IllegalArgumentException.class)
57+
public void testZeroMinIdle() {
58+
new JedisPoolConfig(0);
59+
}
60+
61+
@Test(expected = IllegalArgumentException.class)
62+
public void testNegativeMinIdle() {
63+
new JedisPoolConfig(-5);
64+
}
65+
66+
@Test
67+
public void testRatioConsistency() {
68+
// Test the 2x ratio is maintained across various values
69+
int[] testValues = { 1, 5, 8, 10, 16, 20, 32, 50, 100 };
70+
71+
for (int minIdle : testValues) {
72+
JedisPoolConfig config = new JedisPoolConfig(minIdle);
73+
assertEquals("maxIdle should be 2x minIdle for value " + minIdle,
74+
minIdle * 2, config.getMaxIdle());
75+
assertEquals("maxTotal should be 2x minIdle for value " + minIdle,
76+
minIdle * 2, config.getMaxTotal());
77+
assertEquals("maxIdle should equal maxTotal for value " + minIdle,
78+
config.getMaxIdle(), config.getMaxTotal());
79+
}
80+
}
81+
}

0 commit comments

Comments
 (0)