Skip to content

Commit

Permalink
improve methods add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
oscar-besga-panel committed Sep 3, 2023
1 parent 31f8637 commit 64c14bd
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 11 deletions.
4 changes: 2 additions & 2 deletions src/main/java/redis/clients/jedis/JedisPool.java
Original file line number Diff line number Diff line change
Expand Up @@ -395,13 +395,13 @@ public void returnResource(final Jedis resource) {
}
}

void withJedisDo(Consumer<Jedis> consumer) {
public void withJedisDo(Consumer<Jedis> consumer) {
try (Jedis jedis = getResource()) {
consumer.accept(jedis);
}
}

<K> K withJedisGet(Function<Jedis, K> function) {
public <K> K withJedisGet(Function<Jedis, K> function) {
try (Jedis jedis = getResource()) {
return function.apply(jedis);
}
Expand Down
16 changes: 10 additions & 6 deletions src/main/java/redis/clients/jedis/JedisPooled.java
Original file line number Diff line number Diff line change
Expand Up @@ -404,15 +404,19 @@ public Pipeline pipelined() {
return (Pipeline) super.pipelined();
}

void withJedisDo(Consumer<Jedis> consumer) {
try (Connection connection = getPool().getResource()) {
consumer.accept(new Jedis(connection));
public Jedis newJedis() {
return new Jedis(getPool().getResource());
}

public void withJedisDo(Consumer<Jedis> consumer) {
try (Jedis jedis = newJedis()) {
consumer.accept(jedis);
}
}

<K> K withJedisGet(Function<Jedis, K> function) {
try (Connection connection = getPool().getResource()) {
return function.apply(new Jedis(connection));
public <K> K withJedisGet(Function<Jedis, K> function) {
try (Jedis jedis = newJedis()) {
return function.apply(jedis);
}
}

Expand Down
30 changes: 30 additions & 0 deletions src/test/java/redis/clients/jedis/JedisPoolTest.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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<String> 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());
}
}

}
42 changes: 39 additions & 3 deletions src/test/java/redis/clients/jedis/JedisPooledTest.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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<String> 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());
}
}

}

0 comments on commit 64c14bd

Please sign in to comment.