Add Closeable to JedisPool.
This allows JedisPool instances to also participate in try-with-resources. Adds tests (both for JedisPool and the Jedis code itself).
This commit is contained in:
committed by
Henning Schmiedehausen
parent
ddb1870a5f
commit
dc054268fa
@@ -1,5 +1,7 @@
|
|||||||
package redis.clients.util;
|
package redis.clients.util;
|
||||||
|
|
||||||
|
import java.io.Closeable;
|
||||||
|
|
||||||
import org.apache.commons.pool2.PooledObjectFactory;
|
import org.apache.commons.pool2.PooledObjectFactory;
|
||||||
import org.apache.commons.pool2.impl.GenericObjectPool;
|
import org.apache.commons.pool2.impl.GenericObjectPool;
|
||||||
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
|
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
|
||||||
@@ -7,7 +9,7 @@ import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
|
|||||||
import redis.clients.jedis.exceptions.JedisConnectionException;
|
import redis.clients.jedis.exceptions.JedisConnectionException;
|
||||||
import redis.clients.jedis.exceptions.JedisException;
|
import redis.clients.jedis.exceptions.JedisException;
|
||||||
|
|
||||||
public abstract class Pool<T> {
|
public abstract class Pool<T> implements Closeable {
|
||||||
protected GenericObjectPool<T> internalPool;
|
protected GenericObjectPool<T> internalPool;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -17,6 +19,15 @@ public abstract class Pool<T> {
|
|||||||
public Pool() {
|
public Pool() {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void close() {
|
||||||
|
closeInternalPool();
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isClosed() {
|
||||||
|
return this.internalPool.isClosed();
|
||||||
|
}
|
||||||
|
|
||||||
public Pool(final GenericObjectPoolConfig poolConfig,
|
public Pool(final GenericObjectPoolConfig poolConfig,
|
||||||
PooledObjectFactory<T> factory) {
|
PooledObjectFactory<T> factory) {
|
||||||
initPool(poolConfig, factory);
|
initPool(poolConfig, factory);
|
||||||
@@ -81,4 +92,4 @@ public abstract class Pool<T> {
|
|||||||
throw new JedisException("Could not destroy the pool", e);
|
throw new JedisException("Could not destroy the pool", e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
39
src/test/java/redis/clients/jedis/tests/Closer.java
Normal file
39
src/test/java/redis/clients/jedis/tests/Closer.java
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
package redis.clients.jedis.tests;
|
||||||
|
|
||||||
|
import java.io.Closeable;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
class Closer implements Closeable {
|
||||||
|
private final Set<Closeable> elements = new HashSet<Closeable>();
|
||||||
|
|
||||||
|
synchronized <T extends Closeable> T register(T element) {
|
||||||
|
if (element != null) {
|
||||||
|
elements.add(element);
|
||||||
|
}
|
||||||
|
return element;
|
||||||
|
}
|
||||||
|
|
||||||
|
public synchronized void close() throws IOException {
|
||||||
|
Throwable caught = null;
|
||||||
|
|
||||||
|
for (Closeable element : elements) {
|
||||||
|
try {
|
||||||
|
element.close();
|
||||||
|
}
|
||||||
|
catch (Throwable t) {
|
||||||
|
caught = t;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
elements.clear();
|
||||||
|
|
||||||
|
if (caught != null) {
|
||||||
|
if (caught instanceof IOException) {
|
||||||
|
throw (IOException) caught;
|
||||||
|
}
|
||||||
|
throw (RuntimeException) caught;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,51 @@
|
|||||||
|
package redis.clients.jedis.tests;
|
||||||
|
|
||||||
|
import java.io.Closeable;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
import org.junit.After;
|
||||||
|
import org.junit.Assert;
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import redis.clients.jedis.Connection;
|
||||||
|
import redis.clients.jedis.exceptions.JedisConnectionException;
|
||||||
|
|
||||||
|
public class ConnectionCloseTest extends Assert {
|
||||||
|
|
||||||
|
private final Closer closer = new Closer();
|
||||||
|
|
||||||
|
private Connection client;
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void setUp() throws Exception {
|
||||||
|
client = closer.register(new Connection());
|
||||||
|
}
|
||||||
|
|
||||||
|
@After
|
||||||
|
public void tearDown() throws Exception {
|
||||||
|
closer.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test(expected = JedisConnectionException.class)
|
||||||
|
public void checkUnkownHost() {
|
||||||
|
client.setHost("someunknownhost");
|
||||||
|
client.connect();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test(expected = JedisConnectionException.class)
|
||||||
|
public void checkWrongPort() {
|
||||||
|
client.setHost("localhost");
|
||||||
|
client.setPort(55665);
|
||||||
|
client.connect();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void connectIfNotConnectedWhenSettingTimeoutInfinite() {
|
||||||
|
client.setHost("localhost");
|
||||||
|
client.setPort(6379);
|
||||||
|
client.setTimeoutInfinite();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -27,6 +27,21 @@ public class JedisPoolTest extends Assert {
|
|||||||
assertEquals("bar", jedis.get("foo"));
|
assertEquals("bar", jedis.get("foo"));
|
||||||
pool.returnResource(jedis);
|
pool.returnResource(jedis);
|
||||||
pool.destroy();
|
pool.destroy();
|
||||||
|
assertTrue(pool.isClosed());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void checkCloseableConnections() throws Exception {
|
||||||
|
Closer closer = new Closer();
|
||||||
|
JedisPool pool = closer.register(new JedisPool(new JedisPoolConfig(), hnp.getHost(),
|
||||||
|
hnp.getPort(), 2000));
|
||||||
|
Jedis jedis = pool.getResource();
|
||||||
|
jedis.auth("foobared");
|
||||||
|
jedis.set("foo", "bar");
|
||||||
|
assertEquals("bar", jedis.get("foo"));
|
||||||
|
pool.returnResource(jedis);
|
||||||
|
closer.close();
|
||||||
|
assertTrue(pool.isClosed());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -39,6 +54,7 @@ public class JedisPoolTest extends Assert {
|
|||||||
assertEquals("bar", jedis.get("foo"));
|
assertEquals("bar", jedis.get("foo"));
|
||||||
pool.returnResource(jedis);
|
pool.returnResource(jedis);
|
||||||
pool.destroy();
|
pool.destroy();
|
||||||
|
assertTrue(pool.isClosed());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -56,6 +72,7 @@ public class JedisPoolTest extends Assert {
|
|||||||
jedis.incr("foo");
|
jedis.incr("foo");
|
||||||
pool.returnResource(jedis);
|
pool.returnResource(jedis);
|
||||||
pool.destroy();
|
pool.destroy();
|
||||||
|
assertTrue(pool.isClosed());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -72,6 +89,7 @@ public class JedisPoolTest extends Assert {
|
|||||||
jedis.incr("foo");
|
jedis.incr("foo");
|
||||||
pool.returnResource(jedis);
|
pool.returnResource(jedis);
|
||||||
pool.destroy();
|
pool.destroy();
|
||||||
|
assertTrue(pool.isClosed());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test(expected = JedisConnectionException.class)
|
@Test(expected = JedisConnectionException.class)
|
||||||
@@ -99,6 +117,7 @@ public class JedisPoolTest extends Assert {
|
|||||||
jedis.set("foo", "bar");
|
jedis.set("foo", "bar");
|
||||||
pool.returnResource(jedis);
|
pool.returnResource(jedis);
|
||||||
pool.destroy();
|
pool.destroy();
|
||||||
|
assertTrue(pool.isClosed());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -110,6 +129,7 @@ public class JedisPoolTest extends Assert {
|
|||||||
assertEquals("bar", jedis0.get("foo"));
|
assertEquals("bar", jedis0.get("foo"));
|
||||||
pool0.returnResource(jedis0);
|
pool0.returnResource(jedis0);
|
||||||
pool0.destroy();
|
pool0.destroy();
|
||||||
|
assertTrue(pool0.isClosed());
|
||||||
|
|
||||||
JedisPool pool1 = new JedisPool(new JedisPoolConfig(), hnp.getHost(),
|
JedisPool pool1 = new JedisPool(new JedisPoolConfig(), hnp.getHost(),
|
||||||
hnp.getPort(), 2000, "foobared", 1);
|
hnp.getPort(), 2000, "foobared", 1);
|
||||||
@@ -117,6 +137,7 @@ public class JedisPoolTest extends Assert {
|
|||||||
assertNull(jedis1.get("foo"));
|
assertNull(jedis1.get("foo"));
|
||||||
pool1.returnResource(jedis1);
|
pool1.returnResource(jedis1);
|
||||||
pool1.destroy();
|
pool1.destroy();
|
||||||
|
assertTrue(pool1.isClosed());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -163,6 +184,7 @@ public class JedisPoolTest extends Assert {
|
|||||||
|
|
||||||
pool.returnResource(jedis1);
|
pool.returnResource(jedis1);
|
||||||
pool.destroy();
|
pool.destroy();
|
||||||
|
assertTrue(pool.isClosed());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -176,6 +198,7 @@ public class JedisPoolTest extends Assert {
|
|||||||
|
|
||||||
pool0.returnResource(jedis);
|
pool0.returnResource(jedis);
|
||||||
pool0.destroy();
|
pool0.destroy();
|
||||||
|
assertTrue(pool0.isClosed());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -197,5 +220,6 @@ public class JedisPoolTest extends Assert {
|
|||||||
assertEquals("jedis", jedis2.get("hello"));
|
assertEquals("jedis", jedis2.get("hello"));
|
||||||
pool.returnResource(jedis2);
|
pool.returnResource(jedis2);
|
||||||
pool.destroy();
|
pool.destroy();
|
||||||
|
assertTrue(pool.isClosed());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user