From 31f86375d29d1b04c291af6e2040b54f8d33fe47 Mon Sep 17 00:00:00 2001 From: oscar-besga-panel Date: Sat, 2 Sep 2023 15:51:50 +0200 Subject: [PATCH 1/3] Helper methods to use a Jedis object --- src/main/java/redis/clients/jedis/JedisPool.java | 15 +++++++++++++++ .../java/redis/clients/jedis/JedisPooled.java | 15 +++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/src/main/java/redis/clients/jedis/JedisPool.java b/src/main/java/redis/clients/jedis/JedisPool.java index 3cc9aad107..e7fcbc09d8 100644 --- a/src/main/java/redis/clients/jedis/JedisPool.java +++ b/src/main/java/redis/clients/jedis/JedisPool.java @@ -1,6 +1,8 @@ package redis.clients.jedis; import java.net.URI; +import java.util.function.Consumer; +import java.util.function.Function; import javax.net.ssl.HostnameVerifier; import javax.net.ssl.SSLParameters; import javax.net.ssl.SSLSocketFactory; @@ -392,4 +394,17 @@ public void returnResource(final Jedis resource) { } } } + + void withJedisDo(Consumer consumer) { + try (Jedis jedis = getResource()) { + consumer.accept(jedis); + } + } + + K withJedisGet(Function function) { + try (Jedis jedis = getResource()) { + return function.apply(jedis); + } + } + } diff --git a/src/main/java/redis/clients/jedis/JedisPooled.java b/src/main/java/redis/clients/jedis/JedisPooled.java index bc751527ca..e77a498728 100644 --- a/src/main/java/redis/clients/jedis/JedisPooled.java +++ b/src/main/java/redis/clients/jedis/JedisPooled.java @@ -1,6 +1,8 @@ package redis.clients.jedis; import java.net.URI; +import java.util.function.Consumer; +import java.util.function.Function; import javax.net.ssl.HostnameVerifier; import javax.net.ssl.SSLParameters; import javax.net.ssl.SSLSocketFactory; @@ -401,4 +403,17 @@ public final Pool getPool() { public Pipeline pipelined() { return (Pipeline) super.pipelined(); } + + void withJedisDo(Consumer consumer) { + try (Connection connection = getPool().getResource()) { + consumer.accept(new Jedis(connection)); + } + } + + K withJedisGet(Function function) { + try (Connection connection = getPool().getResource()) { + return function.apply(new Jedis(connection)); + } + } + } From 64c14bd3e79f1421e43fb772d2550a17004a043b Mon Sep 17 00:00:00 2001 From: oscar-besga-panel Date: Sun, 3 Sep 2023 15:20:57 +0200 Subject: [PATCH 2/3] improve methods add tests --- .../java/redis/clients/jedis/JedisPool.java | 4 +- .../java/redis/clients/jedis/JedisPooled.java | 16 ++++--- .../redis/clients/jedis/JedisPoolTest.java | 30 +++++++++++++ .../redis/clients/jedis/JedisPooledTest.java | 42 +++++++++++++++++-- 4 files changed, 81 insertions(+), 11 deletions(-) diff --git a/src/main/java/redis/clients/jedis/JedisPool.java b/src/main/java/redis/clients/jedis/JedisPool.java index e7fcbc09d8..7f04109eae 100644 --- a/src/main/java/redis/clients/jedis/JedisPool.java +++ b/src/main/java/redis/clients/jedis/JedisPool.java @@ -395,13 +395,13 @@ public void returnResource(final Jedis resource) { } } - void withJedisDo(Consumer consumer) { + public void withJedisDo(Consumer consumer) { try (Jedis jedis = getResource()) { consumer.accept(jedis); } } - K withJedisGet(Function function) { + public K withJedisGet(Function function) { try (Jedis jedis = getResource()) { return function.apply(jedis); } diff --git a/src/main/java/redis/clients/jedis/JedisPooled.java b/src/main/java/redis/clients/jedis/JedisPooled.java index e77a498728..1f2b5db55b 100644 --- a/src/main/java/redis/clients/jedis/JedisPooled.java +++ b/src/main/java/redis/clients/jedis/JedisPooled.java @@ -404,15 +404,19 @@ public Pipeline pipelined() { return (Pipeline) super.pipelined(); } - void withJedisDo(Consumer consumer) { - try (Connection connection = getPool().getResource()) { - consumer.accept(new Jedis(connection)); + public Jedis newJedis() { + return new Jedis(getPool().getResource()); + } + + public void withJedisDo(Consumer consumer) { + try (Jedis jedis = newJedis()) { + consumer.accept(jedis); } } - K withJedisGet(Function function) { - try (Connection connection = getPool().getResource()) { - return function.apply(new Jedis(connection)); + public K withJedisGet(Function function) { + try (Jedis jedis = newJedis()) { + return function.apply(jedis); } } diff --git a/src/test/java/redis/clients/jedis/JedisPoolTest.java b/src/test/java/redis/clients/jedis/JedisPoolTest.java index a256abcae0..2d24331d8e 100644 --- a/src/test/java/redis/clients/jedis/JedisPoolTest.java +++ b/src/test/java/redis/clients/jedis/JedisPoolTest.java @@ -1,5 +1,8 @@ package redis.clients.jedis; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.notNullValue; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertSame; @@ -8,6 +11,7 @@ import java.net.URI; import java.net.URISyntaxException; +import java.util.List; import java.util.concurrent.atomic.AtomicInteger; import org.apache.commons.pool2.PooledObject; import org.apache.commons.pool2.PooledObjectFactory; @@ -453,4 +457,30 @@ public void testResetValidCredentials() { } } } + + @Test + public void testWithJedisDo() { + HostAndPort hpTest = HostAndPorts.getRedisServers().get(0); + try (JedisPool pool = new JedisPool(new JedisPoolConfig(), hpTest.getHost(), hpTest.getPort())) { + pool.withJedisDo(jedis -> { + assertThat(jedis.getClient().getHostAndPort().getHost(), equalTo(hpTest.getHost())); + assertThat(jedis.getClient().getHostAndPort().getPort(), equalTo(hpTest.getPort())); + assertThat(jedis.time(), notNullValue()); + }); + } + } + + @Test + public void testWithJedisGet() { + HostAndPort hpTest = HostAndPorts.getRedisServers().get(0); + try (JedisPool pool = new JedisPool(new JedisPoolConfig(), hpTest.getHost(), hpTest.getPort())) { + List result = pool.withJedisGet(jedis -> { + assertThat(jedis.getClient().getHostAndPort().getHost(), equalTo(hpTest.getHost())); + assertThat(jedis.getClient().getHostAndPort().getPort(), equalTo(hpTest.getPort())); + return jedis.time(); + }); + assertThat(result,notNullValue()); + } + } + } diff --git a/src/test/java/redis/clients/jedis/JedisPooledTest.java b/src/test/java/redis/clients/jedis/JedisPooledTest.java index 6036062f5b..27e0d146b0 100644 --- a/src/test/java/redis/clients/jedis/JedisPooledTest.java +++ b/src/test/java/redis/clients/jedis/JedisPooledTest.java @@ -1,15 +1,14 @@ package redis.clients.jedis; import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.anything; -import static org.hamcrest.Matchers.equalTo; -import static org.hamcrest.Matchers.greaterThanOrEqualTo; +import static org.hamcrest.Matchers.*; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; import java.net.URI; import java.net.URISyntaxException; +import java.util.List; import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicInteger; import org.apache.commons.pool2.impl.GenericObjectPoolConfig; @@ -255,4 +254,41 @@ public void cleanUp() { assertThat(cleanupCount.get(), equalTo(1)); } } + + @Test + public void testNewJedis() { + HostAndPort hostAndPortTest = HostAndPorts.getRedisServers().get(0); + try (JedisPooled pool = new JedisPooled(hostAndPortTest)) { + Jedis jedis = pool.newJedis(); + assertThat(jedis.getClient().getHostAndPort().getHost(), equalTo(hostAndPortTest.getHost())); + assertThat(jedis.getClient().getHostAndPort().getPort(), equalTo(hostAndPortTest.getPort())); + assertThat(jedis.time(), notNullValue()); + } + } + + @Test + public void testWithJedisDo() { + HostAndPort hostAndPortTest = HostAndPorts.getRedisServers().get(0); + try (JedisPooled pool = new JedisPooled(hostAndPortTest)) { + pool.withJedisDo(jedis -> { + assertThat(jedis.getClient().getHostAndPort().getHost(), equalTo(hostAndPortTest.getHost())); + assertThat(jedis.getClient().getHostAndPort().getPort(), equalTo(hostAndPortTest.getPort())); + assertThat(jedis.time(), notNullValue()); + }); + } + } + + @Test + public void testWithJedisGet() { + HostAndPort hostAndPortTest = HostAndPorts.getRedisServers().get(0); + try (JedisPooled pool = new JedisPooled(hostAndPortTest)) { + List result = pool.withJedisGet(jedis -> { + assertThat(jedis.getClient().getHostAndPort().getHost(), equalTo(hostAndPortTest.getHost())); + assertThat(jedis.getClient().getHostAndPort().getPort(), equalTo(hostAndPortTest.getPort())); + return jedis.time(); + }); + assertThat(result,notNullValue()); + } + } + } From 993f16d0aafdf41b23606ed491e18000a2d3a73f Mon Sep 17 00:00:00 2001 From: oscar-besga-panel Date: Sun, 3 Sep 2023 23:32:54 +0200 Subject: [PATCH 3/3] fix tests --- .../redis/clients/jedis/JedisPoolTest.java | 16 +++++++------- .../redis/clients/jedis/JedisPooledTest.java | 21 ++++++++----------- 2 files changed, 17 insertions(+), 20 deletions(-) diff --git a/src/test/java/redis/clients/jedis/JedisPoolTest.java b/src/test/java/redis/clients/jedis/JedisPoolTest.java index 2d24331d8e..5484e6658a 100644 --- a/src/test/java/redis/clients/jedis/JedisPoolTest.java +++ b/src/test/java/redis/clients/jedis/JedisPoolTest.java @@ -460,11 +460,11 @@ public void testResetValidCredentials() { @Test public void testWithJedisDo() { - HostAndPort hpTest = HostAndPorts.getRedisServers().get(0); - try (JedisPool pool = new JedisPool(new JedisPoolConfig(), hpTest.getHost(), hpTest.getPort())) { + try (JedisPool pool = new JedisPool(new JedisPoolConfig(), hnp.getHost(), hnp.getPort())) { pool.withJedisDo(jedis -> { - assertThat(jedis.getClient().getHostAndPort().getHost(), equalTo(hpTest.getHost())); - assertThat(jedis.getClient().getHostAndPort().getPort(), equalTo(hpTest.getPort())); + jedis.auth("foobared"); + assertThat(jedis.getClient().getHostAndPort().getHost(), equalTo(hnp.getHost())); + assertThat(jedis.getClient().getHostAndPort().getPort(), equalTo(hnp.getPort())); assertThat(jedis.time(), notNullValue()); }); } @@ -472,11 +472,11 @@ public void testWithJedisDo() { @Test public void testWithJedisGet() { - HostAndPort hpTest = HostAndPorts.getRedisServers().get(0); - try (JedisPool pool = new JedisPool(new JedisPoolConfig(), hpTest.getHost(), hpTest.getPort())) { + try (JedisPool pool = new JedisPool(new JedisPoolConfig(), hnp.getHost(), hnp.getPort())) { List result = pool.withJedisGet(jedis -> { - assertThat(jedis.getClient().getHostAndPort().getHost(), equalTo(hpTest.getHost())); - assertThat(jedis.getClient().getHostAndPort().getPort(), equalTo(hpTest.getPort())); + jedis.auth("foobared"); + assertThat(jedis.getClient().getHostAndPort().getHost(), equalTo(hnp.getHost())); + assertThat(jedis.getClient().getHostAndPort().getPort(), equalTo(hnp.getPort())); return jedis.time(); }); assertThat(result,notNullValue()); diff --git a/src/test/java/redis/clients/jedis/JedisPooledTest.java b/src/test/java/redis/clients/jedis/JedisPooledTest.java index 27e0d146b0..c8ce3585f9 100644 --- a/src/test/java/redis/clients/jedis/JedisPooledTest.java +++ b/src/test/java/redis/clients/jedis/JedisPooledTest.java @@ -257,22 +257,20 @@ public void cleanUp() { @Test public void testNewJedis() { - HostAndPort hostAndPortTest = HostAndPorts.getRedisServers().get(0); - try (JedisPooled pool = new JedisPooled(hostAndPortTest)) { + try (JedisPooled pool = new JedisPooled(hnp)) { Jedis jedis = pool.newJedis(); - assertThat(jedis.getClient().getHostAndPort().getHost(), equalTo(hostAndPortTest.getHost())); - assertThat(jedis.getClient().getHostAndPort().getPort(), equalTo(hostAndPortTest.getPort())); + assertThat(jedis.getClient().getHostAndPort().getHost(), equalTo(hnp.getHost())); + assertThat(jedis.getClient().getHostAndPort().getPort(), equalTo(hnp.getPort())); assertThat(jedis.time(), notNullValue()); } } @Test public void testWithJedisDo() { - HostAndPort hostAndPortTest = HostAndPorts.getRedisServers().get(0); - try (JedisPooled pool = new JedisPooled(hostAndPortTest)) { + try (JedisPooled pool = new JedisPooled(hnp)) { pool.withJedisDo(jedis -> { - assertThat(jedis.getClient().getHostAndPort().getHost(), equalTo(hostAndPortTest.getHost())); - assertThat(jedis.getClient().getHostAndPort().getPort(), equalTo(hostAndPortTest.getPort())); + assertThat(jedis.getClient().getHostAndPort().getHost(), equalTo(hnp.getHost())); + assertThat(jedis.getClient().getHostAndPort().getPort(), equalTo(hnp.getPort())); assertThat(jedis.time(), notNullValue()); }); } @@ -280,11 +278,10 @@ public void testWithJedisDo() { @Test public void testWithJedisGet() { - HostAndPort hostAndPortTest = HostAndPorts.getRedisServers().get(0); - try (JedisPooled pool = new JedisPooled(hostAndPortTest)) { + try (JedisPooled pool = new JedisPooled(hnp)) { List result = pool.withJedisGet(jedis -> { - assertThat(jedis.getClient().getHostAndPort().getHost(), equalTo(hostAndPortTest.getHost())); - assertThat(jedis.getClient().getHostAndPort().getPort(), equalTo(hostAndPortTest.getPort())); + assertThat(jedis.getClient().getHostAndPort().getHost(), equalTo(hnp.getHost())); + assertThat(jedis.getClient().getHostAndPort().getPort(), equalTo(hnp.getPort())); return jedis.time(); }); assertThat(result,notNullValue());