Skip to content

Commit 0a92e5d

Browse files
Add helpers to Jedis pool (#4366)
* Add helpers to Jedis pool * Test for helpers to Jedis pool * amedn method and test * Clean up + unit test - Add unit test to verify connection is returned to the pool --------- Co-authored-by: ggivo <[email protected]>
1 parent 2325e21 commit 0a92e5d

File tree

4 files changed

+149
-0
lines changed

4 files changed

+149
-0
lines changed

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -546,6 +546,7 @@
546546
<include>**/*CommandFlags*.java</include>
547547
<include>**/*CompareCondition*.java</include>
548548
<include>**/*MSetExParams*.java</include>
549+
<include>**/*UnitTest.java</include>
549550
</includes>
550551
</configuration>
551552
<executions>

src/main/java/redis/clients/jedis/JedisPool.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package redis.clients.jedis;
22

33
import java.net.URI;
4+
import java.util.function.Consumer;
5+
import java.util.function.Function;
46
import javax.net.ssl.HostnameVerifier;
57
import javax.net.ssl.SSLParameters;
68
import javax.net.ssl.SSLSocketFactory;
@@ -392,4 +394,17 @@ public void returnResource(final Jedis resource) {
392394
}
393395
}
394396
}
397+
398+
public void withResource(Consumer<Jedis> consumer) {
399+
try (Jedis jedis = this.getResource()) {
400+
consumer.accept(jedis);
401+
}
402+
}
403+
404+
public <K> K withResourceGet(Function<Jedis, K> function) {
405+
try (Jedis jedis = this.getResource()) {
406+
return function.apply(jedis);
407+
}
408+
}
409+
395410
}

src/test/java/redis/clients/jedis/JedisPoolTest.java

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,19 @@
99
import org.apache.commons.pool2.impl.DefaultPooledObject;
1010
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
1111

12+
import org.junit.jupiter.api.BeforeEach;
1213
import org.junit.jupiter.api.Tag;
1314
import org.junit.jupiter.api.Test;
15+
import org.junit.jupiter.api.TestInfo;
1416
import redis.clients.jedis.exceptions.InvalidURIException;
1517
import redis.clients.jedis.exceptions.JedisAccessControlException;
1618
import redis.clients.jedis.exceptions.JedisConnectionException;
1719
import redis.clients.jedis.exceptions.JedisException;
1820

1921
import static java.util.concurrent.TimeUnit.MILLISECONDS;
2022
import static org.awaitility.Awaitility.await;
23+
import static org.hamcrest.MatcherAssert.assertThat;
24+
import static org.hamcrest.Matchers.equalTo;
2125
import static org.junit.jupiter.api.Assertions.assertEquals;
2226
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
2327
import static org.junit.jupiter.api.Assertions.assertNull;
@@ -33,6 +37,15 @@ public class JedisPoolTest {
3337

3438
private static final EndpointConfig endpointStandalone1 = HostAndPorts.getRedisEndpoint("standalone1");
3539

40+
private String testKey;
41+
private String testValue;
42+
43+
@BeforeEach
44+
public void setUpTestKey(TestInfo testInfo) {
45+
testKey = testInfo.getDisplayName() + "-key";
46+
testValue = testInfo.getDisplayName() + "-value";
47+
}
48+
3649
@Test
3750
public void checkConnections() {
3851
JedisPool pool = new JedisPool(new JedisPoolConfig(), endpointStandalone0.getHost(), endpointStandalone0.getPort(), 2000);
@@ -462,4 +475,64 @@ public void testResetValidCredentials() {
462475
}
463476
}
464477
}
478+
479+
@Test
480+
public void testWithResource() {
481+
try (JedisPool pool = new JedisPool(new JedisPoolConfig(), endpointStandalone0.getHostAndPort(),
482+
endpointStandalone0.getClientConfigBuilder().build())) {
483+
484+
pool.withResource(jedis -> {
485+
jedis.set(testKey, testValue);
486+
});
487+
488+
pool.withResource(jedis -> {
489+
assertEquals(testValue, jedis.get(testKey));
490+
});
491+
}
492+
}
493+
494+
@Test
495+
public void testWithResourceReturnsConnectionToPool() {
496+
try (JedisPool pool = new JedisPool(new JedisPoolConfig(), endpointStandalone0.getHostAndPort(),
497+
endpointStandalone0.getClientConfigBuilder().build())) {
498+
499+
pool.withResource(jedis -> {
500+
assertThat(pool.getNumActive(), equalTo(1));
501+
jedis.set("foo", "bar");
502+
});
503+
504+
assertThat(pool.getNumActive(), equalTo(0));
505+
}
506+
}
507+
508+
@Test
509+
public void testWithResourceGet() {
510+
try (JedisPool pool = new JedisPool(new JedisPoolConfig(), endpointStandalone0.getHostAndPort(),
511+
endpointStandalone0.getClientConfigBuilder().build())) {
512+
513+
String result = pool.withResourceGet(jedis -> {
514+
jedis.set(testKey, testValue);
515+
return jedis.get(testKey);
516+
});
517+
518+
assertEquals(testValue, result);
519+
}
520+
}
521+
522+
@Test
523+
public void testWithResourceGetReturnsConnectionToPool() {
524+
try (JedisPool pool = new JedisPool(new JedisPoolConfig(), endpointStandalone0.getHostAndPort(),
525+
endpointStandalone0.getClientConfigBuilder().build())) {
526+
527+
String result = pool.withResourceGet(jedis -> {
528+
assertThat(pool.getNumActive(), equalTo(1));
529+
jedis.set("foo", "bar");
530+
return jedis.get("foo");
531+
});
532+
533+
assertThat(result, equalTo("bar"));
534+
assertThat(pool.getNumActive(), equalTo(0));
535+
}
536+
}
537+
465538
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package redis.clients.jedis;
2+
3+
import org.apache.commons.pool2.PooledObjectFactory;
4+
import org.apache.commons.pool2.impl.DefaultPooledObject;
5+
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
6+
import org.junit.jupiter.api.AfterEach;
7+
import org.junit.jupiter.api.BeforeEach;
8+
import org.junit.jupiter.api.Test;
9+
10+
import static org.junit.jupiter.api.Assertions.assertEquals;
11+
import static org.mockito.Mockito.*;
12+
13+
public class JedisPoolUnitTest {
14+
15+
private JedisPool pool;
16+
private Jedis mockJedis;
17+
18+
@BeforeEach
19+
public void setUp() throws Exception {
20+
mockJedis = mock(Jedis.class);
21+
PooledObjectFactory<Jedis> mockFactory = mock(PooledObjectFactory.class);
22+
23+
when(mockFactory.makeObject()).thenReturn(new DefaultPooledObject<>(mockJedis));
24+
25+
GenericObjectPoolConfig<Jedis> config = new GenericObjectPoolConfig<>();
26+
config.setMaxTotal(1);
27+
pool = spy(new JedisPool(config, mockFactory));
28+
29+
}
30+
31+
@AfterEach
32+
public void tearDown() {
33+
if (pool != null && !pool.isClosed()) {
34+
pool.close();
35+
}
36+
}
37+
38+
@Test
39+
public void testWithResourceClosesConnection() {
40+
pool.withResource(jedis -> assertEquals(mockJedis, jedis));
41+
42+
verify(mockJedis, times(1)).close();
43+
}
44+
45+
@Test
46+
public void testWithResourceGetClosesConnection() {
47+
String result = pool.withResourceGet(jedis -> "test-result");
48+
49+
verify(mockJedis, times(1)).close();
50+
}
51+
52+
@Test
53+
public void testWithResourceGetReturnsResult() {
54+
when(mockJedis.get(eq("test-key"))).thenReturn("test-result");
55+
String result = pool.withResourceGet(jedis -> jedis.get("test-key"));
56+
57+
verify(mockJedis, times(1)).close();
58+
}
59+
60+
}

0 commit comments

Comments
 (0)