Merge branch 'pool-closeable-support' of https://github.com/HeartSaVioR/jedis into HeartSaVioR-pool-closeable-support
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
package redis.clients.util;
|
||||
|
||||
import java.io.Closeable;
|
||||
|
||||
import org.apache.commons.pool2.PooledObjectFactory;
|
||||
import org.apache.commons.pool2.impl.GenericObjectPool;
|
||||
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.JedisException;
|
||||
|
||||
public abstract class Pool<T> {
|
||||
public abstract class Pool<T> implements Closeable {
|
||||
protected GenericObjectPool<T> internalPool;
|
||||
|
||||
/**
|
||||
@@ -17,6 +19,15 @@ public abstract class Pool<T> {
|
||||
public Pool() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
closeInternalPool();
|
||||
}
|
||||
|
||||
public boolean isClosed() {
|
||||
return this.internalPool.isClosed();
|
||||
}
|
||||
|
||||
public Pool(final GenericObjectPoolConfig poolConfig,
|
||||
PooledObjectFactory<T> factory) {
|
||||
initPool(poolConfig, factory);
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
package redis.clients.jedis.tests;
|
||||
|
||||
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 Connection client;
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
client = new Connection();
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() throws Exception {
|
||||
client.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,20 @@ public class JedisPoolTest extends Assert {
|
||||
assertEquals("bar", jedis.get("foo"));
|
||||
pool.returnResource(jedis);
|
||||
pool.destroy();
|
||||
assertTrue(pool.isClosed());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void checkCloseableConnections() throws Exception {
|
||||
JedisPool pool = 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);
|
||||
pool.close();
|
||||
assertTrue(pool.isClosed());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -39,6 +53,7 @@ public class JedisPoolTest extends Assert {
|
||||
assertEquals("bar", jedis.get("foo"));
|
||||
pool.returnResource(jedis);
|
||||
pool.destroy();
|
||||
assertTrue(pool.isClosed());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -56,6 +71,7 @@ public class JedisPoolTest extends Assert {
|
||||
jedis.incr("foo");
|
||||
pool.returnResource(jedis);
|
||||
pool.destroy();
|
||||
assertTrue(pool.isClosed());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -72,6 +88,7 @@ public class JedisPoolTest extends Assert {
|
||||
jedis.incr("foo");
|
||||
pool.returnResource(jedis);
|
||||
pool.destroy();
|
||||
assertTrue(pool.isClosed());
|
||||
}
|
||||
|
||||
@Test(expected = JedisConnectionException.class)
|
||||
@@ -99,6 +116,7 @@ public class JedisPoolTest extends Assert {
|
||||
jedis.set("foo", "bar");
|
||||
pool.returnResource(jedis);
|
||||
pool.destroy();
|
||||
assertTrue(pool.isClosed());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -110,6 +128,7 @@ public class JedisPoolTest extends Assert {
|
||||
assertEquals("bar", jedis0.get("foo"));
|
||||
pool0.returnResource(jedis0);
|
||||
pool0.destroy();
|
||||
assertTrue(pool0.isClosed());
|
||||
|
||||
JedisPool pool1 = new JedisPool(new JedisPoolConfig(), hnp.getHost(),
|
||||
hnp.getPort(), 2000, "foobared", 1);
|
||||
@@ -117,6 +136,7 @@ public class JedisPoolTest extends Assert {
|
||||
assertNull(jedis1.get("foo"));
|
||||
pool1.returnResource(jedis1);
|
||||
pool1.destroy();
|
||||
assertTrue(pool1.isClosed());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -163,6 +183,7 @@ public class JedisPoolTest extends Assert {
|
||||
|
||||
pool.returnResource(jedis1);
|
||||
pool.destroy();
|
||||
assertTrue(pool.isClosed());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -176,6 +197,7 @@ public class JedisPoolTest extends Assert {
|
||||
|
||||
pool0.returnResource(jedis);
|
||||
pool0.destroy();
|
||||
assertTrue(pool0.isClosed());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -204,6 +226,7 @@ public class JedisPoolTest extends Assert {
|
||||
}
|
||||
|
||||
pool.destroy();
|
||||
assertTrue(pool.isClosed());
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -40,6 +40,21 @@ public class JedisSentinelPoolTest extends JedisTestBase {
|
||||
sentinelJedis1 = new Jedis(sentinel1.getHost(), sentinel1.getPort());
|
||||
sentinelJedis2 = new Jedis(sentinel2.getHost(), sentinel2.getPort());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void checkCloseableConnections() throws Exception {
|
||||
GenericObjectPoolConfig config = new GenericObjectPoolConfig();
|
||||
|
||||
JedisSentinelPool pool = new JedisSentinelPool(
|
||||
MASTER_NAME, sentinels, config, 1000, "foobared", 2);
|
||||
Jedis jedis = pool.getResource();
|
||||
jedis.auth("foobared");
|
||||
jedis.set("foo", "bar");
|
||||
assertEquals("bar", jedis.get("foo"));
|
||||
pool.returnResource(jedis);
|
||||
pool.close();
|
||||
assertTrue(pool.isClosed());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void ensureSafeTwiceFailover() throws InterruptedException {
|
||||
|
||||
@@ -55,6 +55,18 @@ public class ShardedJedisPoolTest extends Assert {
|
||||
pool.destroy();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void checkCloseableConnections() throws Exception {
|
||||
ShardedJedisPool pool = new ShardedJedisPool(
|
||||
new GenericObjectPoolConfig(), shards);
|
||||
ShardedJedis jedis = pool.getResource();
|
||||
jedis.set("foo", "bar");
|
||||
assertEquals("bar", jedis.get("foo"));
|
||||
pool.returnResource(jedis);
|
||||
pool.close();
|
||||
assertTrue(pool.isClosed());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void checkConnectionWithDefaultPort() {
|
||||
ShardedJedisPool pool = new ShardedJedisPool(
|
||||
|
||||
Reference in New Issue
Block a user