Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update ACL tests and modify JedisPool tests #2325

Merged
merged 9 commits into from
Dec 24, 2020
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ daemonize yes
protected-mode no
port 6379
requirepass foobared
user acljedis on allcommands allkeys >fizzbuzz
pidfile /tmp/redis1.pid
logfile /tmp/redis1.log
save ""
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/redis/clients/jedis/JedisPool.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,15 @@ public JedisPool(final GenericObjectPoolConfig poolConfig, final String host, in
this(poolConfig, host, port, timeout, password, Protocol.DEFAULT_DATABASE);
}

public JedisPool(final String host, int port, String user, final String password) {
this(new GenericObjectPoolConfig(), host, port, user, password);
}

public JedisPool(final GenericObjectPoolConfig poolConfig, final String host, int port,
String user, final String password) {
this(poolConfig, host, port, Protocol.DEFAULT_TIMEOUT, user, password, Protocol.DEFAULT_DATABASE);
}

public JedisPool(final GenericObjectPoolConfig poolConfig, final String host, int port,
int timeout, final String user, final String password) {
this(poolConfig, host, port, timeout, user, password, Protocol.DEFAULT_DATABASE);
Expand Down
92 changes: 38 additions & 54 deletions src/test/java/redis/clients/jedis/tests/JedisPoolTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import java.net.URI;
import java.net.URISyntaxException;
import java.util.concurrent.atomic.AtomicInteger;

import org.apache.commons.pool2.PooledObject;
import org.apache.commons.pool2.PooledObjectFactory;
import org.apache.commons.pool2.impl.DefaultPooledObject;
Expand All @@ -21,7 +20,6 @@
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.Transaction;
import redis.clients.jedis.exceptions.InvalidURIException;
import redis.clients.jedis.exceptions.JedisException;
import redis.clients.jedis.exceptions.JedisExhaustedPoolException;

public class JedisPoolTest {
Expand All @@ -42,56 +40,64 @@ public void checkConnections() {
@Test
public void checkCloseableConnections() throws Exception {
JedisPool pool = new JedisPool(new JedisPoolConfig(), hnp.getHost(), hnp.getPort(), 2000);
gkorland marked this conversation as resolved.
Show resolved Hide resolved
Jedis jedis = pool.getResource();
jedis.auth("foobared");
jedis.set("foo", "bar");
assertEquals("bar", jedis.get("foo"));
jedis.close();
try (Jedis jedis = pool.getResource()) {
jedis.auth("foobared");
jedis.set("foo", "bar");
assertEquals("bar", jedis.get("foo"));
}
pool.close();
assertTrue(pool.isClosed());
}

@Test
public void checkConnectionWithDefaultPort() {
JedisPool pool = new JedisPool(new JedisPoolConfig(), hnp.getHost(), hnp.getPort());
Jedis jedis = pool.getResource();
jedis.auth("foobared");
jedis.set("foo", "bar");
assertEquals("bar", jedis.get("foo"));
jedis.close();
JedisPool pool = new JedisPool(new JedisPoolConfig(), hnp.getHost());
try (Jedis jedis = pool.getResource()) {
jedis.auth("foobared");
jedis.set("foo", "bar");
assertEquals("bar", jedis.get("foo"));
}
pool.destroy();
assertTrue(pool.isClosed());
}

@Test
public void checkJedisIsReusedWhenReturned() {
public void checkResourceIsClosableAndReusable() {
GenericObjectPoolConfig config = new GenericObjectPoolConfig();
config.setMaxTotal(1);
config.setBlockWhenExhausted(false);
JedisPool pool = new JedisPool(config, hnp.getHost(), hnp.getPort(), 2000, "foobared", 0,
"closable-resuable-pool", false, null, null, null);

JedisPool pool = new JedisPool(new JedisPoolConfig(), hnp.getHost(), hnp.getPort());
Jedis jedis = pool.getResource();
sazzad16 marked this conversation as resolved.
Show resolved Hide resolved
jedis.auth("foobared");
jedis.set("foo", "0");
jedis.close();
try {
jedis.set("hello", "jedis");
} finally {
jedis.close();
}

jedis = pool.getResource();
jedis.auth("foobared");
jedis.incr("foo");
jedis.close();
pool.destroy();
assertTrue(pool.isClosed());
Jedis jedis2 = pool.getResource();
try {
assertEquals(jedis, jedis2);
assertEquals("jedis", jedis2.get("hello"));
} finally {
jedis2.close();
}
}

@Test
public void checkPoolRepairedWhenJedisIsBroken() {
JedisPool pool = new JedisPool(new JedisPoolConfig(), hnp.getHost(), hnp.getPort());
Jedis jedis = pool.getResource();
jedis.auth("foobared");
jedis.quit();
jedis.close();
try (Jedis jedis = pool.getResource()) {
jedis.auth("foobared");
jedis.set("foo", "0");
jedis.quit();
}

jedis = pool.getResource();
jedis.auth("foobared");
jedis.incr("foo");
jedis.close();
try (Jedis jedis = pool.getResource()) {
jedis.auth("foobared");
jedis.incr("foo");
}
pool.destroy();
assertTrue(pool.isClosed());
}
Expand Down Expand Up @@ -299,28 +305,6 @@ public void returnResourceShouldResetState() {
assertTrue(pool.isClosed());
}

@Test
public void checkResourceIsCloseable() {
GenericObjectPoolConfig config = new GenericObjectPoolConfig();
config.setMaxTotal(1);
config.setBlockWhenExhausted(false);
JedisPool pool = new JedisPool(config, hnp.getHost(), hnp.getPort(), 2000, "foobared");

Jedis jedis = pool.getResource();
try {
jedis.set("hello", "jedis");
} finally {
jedis.close();
}

Jedis jedis2 = pool.getResource();
try {
assertEquals(jedis, jedis2);
} finally {
jedis2.close();
}
}

@Test
public void getNumActiveIsNegativeWhenPoolIsClosed() {
JedisPool pool = new JedisPool(new JedisPoolConfig(), hnp.getHost(), hnp.getPort(), 2000,
Expand Down
Loading