Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit0a92e5d

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 <ivo.gaydazhiev@redis.com>
1 parent2325e21 commit0a92e5d

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
packageredis.clients.jedis;
22

33
importjava.net.URI;
4+
importjava.util.function.Consumer;
5+
importjava.util.function.Function;
46
importjavax.net.ssl.HostnameVerifier;
57
importjavax.net.ssl.SSLParameters;
68
importjavax.net.ssl.SSLSocketFactory;
@@ -392,4 +394,17 @@ public void returnResource(final Jedis resource) {
392394
}
393395
}
394396
}
397+
398+
publicvoidwithResource(Consumer<Jedis>consumer) {
399+
try (Jedisjedis =this.getResource()) {
400+
consumer.accept(jedis);
401+
}
402+
}
403+
404+
public <K>KwithResourceGet(Function<Jedis,K>function) {
405+
try (Jedisjedis =this.getResource()) {
406+
returnfunction.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
importorg.apache.commons.pool2.impl.DefaultPooledObject;
1010
importorg.apache.commons.pool2.impl.GenericObjectPoolConfig;
1111

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

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

3438
privatestaticfinalEndpointConfigendpointStandalone1 =HostAndPorts.getRedisEndpoint("standalone1");
3539

40+
privateStringtestKey;
41+
privateStringtestValue;
42+
43+
@BeforeEach
44+
publicvoidsetUpTestKey(TestInfotestInfo) {
45+
testKey =testInfo.getDisplayName() +"-key";
46+
testValue =testInfo.getDisplayName() +"-value";
47+
}
48+
3649
@Test
3750
publicvoidcheckConnections() {
3851
JedisPoolpool =newJedisPool(newJedisPoolConfig(),endpointStandalone0.getHost(),endpointStandalone0.getPort(),2000);
@@ -462,4 +475,64 @@ public void testResetValidCredentials() {
462475
}
463476
}
464477
}
478+
479+
@Test
480+
publicvoidtestWithResource() {
481+
try (JedisPoolpool =newJedisPool(newJedisPoolConfig(),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+
publicvoidtestWithResourceReturnsConnectionToPool() {
496+
try (JedisPoolpool =newJedisPool(newJedisPoolConfig(),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+
publicvoidtestWithResourceGet() {
510+
try (JedisPoolpool =newJedisPool(newJedisPoolConfig(),endpointStandalone0.getHostAndPort(),
511+
endpointStandalone0.getClientConfigBuilder().build())) {
512+
513+
Stringresult =pool.withResourceGet(jedis -> {
514+
jedis.set(testKey,testValue);
515+
returnjedis.get(testKey);
516+
});
517+
518+
assertEquals(testValue,result);
519+
}
520+
}
521+
522+
@Test
523+
publicvoidtestWithResourceGetReturnsConnectionToPool() {
524+
try (JedisPoolpool =newJedisPool(newJedisPoolConfig(),endpointStandalone0.getHostAndPort(),
525+
endpointStandalone0.getClientConfigBuilder().build())) {
526+
527+
Stringresult =pool.withResourceGet(jedis -> {
528+
assertThat(pool.getNumActive(),equalTo(1));
529+
jedis.set("foo","bar");
530+
returnjedis.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+
packageredis.clients.jedis;
2+
3+
importorg.apache.commons.pool2.PooledObjectFactory;
4+
importorg.apache.commons.pool2.impl.DefaultPooledObject;
5+
importorg.apache.commons.pool2.impl.GenericObjectPoolConfig;
6+
importorg.junit.jupiter.api.AfterEach;
7+
importorg.junit.jupiter.api.BeforeEach;
8+
importorg.junit.jupiter.api.Test;
9+
10+
importstaticorg.junit.jupiter.api.Assertions.assertEquals;
11+
importstaticorg.mockito.Mockito.*;
12+
13+
publicclassJedisPoolUnitTest {
14+
15+
privateJedisPoolpool;
16+
privateJedismockJedis;
17+
18+
@BeforeEach
19+
publicvoidsetUp()throwsException {
20+
mockJedis =mock(Jedis.class);
21+
PooledObjectFactory<Jedis>mockFactory =mock(PooledObjectFactory.class);
22+
23+
when(mockFactory.makeObject()).thenReturn(newDefaultPooledObject<>(mockJedis));
24+
25+
GenericObjectPoolConfig<Jedis>config =newGenericObjectPoolConfig<>();
26+
config.setMaxTotal(1);
27+
pool =spy(newJedisPool(config,mockFactory));
28+
29+
}
30+
31+
@AfterEach
32+
publicvoidtearDown() {
33+
if (pool !=null && !pool.isClosed()) {
34+
pool.close();
35+
}
36+
}
37+
38+
@Test
39+
publicvoidtestWithResourceClosesConnection() {
40+
pool.withResource(jedis ->assertEquals(mockJedis,jedis));
41+
42+
verify(mockJedis,times(1)).close();
43+
}
44+
45+
@Test
46+
publicvoidtestWithResourceGetClosesConnection() {
47+
Stringresult =pool.withResourceGet(jedis ->"test-result");
48+
49+
verify(mockJedis,times(1)).close();
50+
}
51+
52+
@Test
53+
publicvoidtestWithResourceGetReturnsResult() {
54+
when(mockJedis.get(eq("test-key"))).thenReturn("test-result");
55+
Stringresult =pool.withResourceGet(jedis ->jedis.get("test-key"));
56+
57+
verify(mockJedis,times(1)).close();
58+
}
59+
60+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp