Implements Closeable to Pooled Jedis & ShardedJedis
* Implement Closeable from Jedis, ShardedJedis with Pooled ** resources from JedisPool, JedisSentinelPool, ShardedJedis, ShardedJedisPool * Connection class : check whether Jedis Connection is broken ** when it's time to throw JedisConnectionException, mark Connection to broken
This commit is contained in:
@@ -187,15 +187,45 @@ public class JedisPoolTest extends Assert {
|
||||
2000, "foobared");
|
||||
|
||||
Jedis jedis = pool.getResource();
|
||||
jedis.set("hello", "jedis");
|
||||
Transaction t = jedis.multi();
|
||||
t.set("hello", "world");
|
||||
pool.returnResource(jedis);
|
||||
try {
|
||||
jedis.set("hello", "jedis");
|
||||
Transaction t = jedis.multi();
|
||||
t.set("hello", "world");
|
||||
} finally {
|
||||
jedis.close();
|
||||
}
|
||||
|
||||
Jedis jedis2 = pool.getResource();
|
||||
assertTrue(jedis == jedis2);
|
||||
assertEquals("jedis", jedis2.get("hello"));
|
||||
pool.returnResource(jedis2);
|
||||
try {
|
||||
assertTrue(jedis == jedis2);
|
||||
assertEquals("jedis", jedis2.get("hello"));
|
||||
} finally {
|
||||
jedis2.close();
|
||||
}
|
||||
|
||||
pool.destroy();
|
||||
}
|
||||
}
|
||||
|
||||
@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();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -79,6 +79,29 @@ public class JedisSentinelPoolTest extends JedisTestBase {
|
||||
pool.destroy();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void checkResourceIsCloseable() {
|
||||
GenericObjectPoolConfig config = new GenericObjectPoolConfig();
|
||||
config.setMaxTotal(1);
|
||||
config.setBlockWhenExhausted(false);
|
||||
JedisSentinelPool pool = new JedisSentinelPool(MASTER_NAME, sentinels,
|
||||
config, 1000, "foobared", 2);
|
||||
|
||||
Jedis jedis = pool.getResource();
|
||||
try {
|
||||
jedis.set("hello", "jedis");
|
||||
} finally {
|
||||
jedis.close();
|
||||
}
|
||||
|
||||
Jedis jedis2 = pool.getResource();
|
||||
try {
|
||||
assertEquals(jedis, jedis2);
|
||||
} finally {
|
||||
jedis2.close();
|
||||
}
|
||||
}
|
||||
|
||||
private void forceFailover(JedisSentinelPool pool)
|
||||
throws InterruptedException {
|
||||
|
||||
@@ -14,6 +14,7 @@ import redis.clients.jedis.HostAndPort;
|
||||
import redis.clients.jedis.Jedis;
|
||||
import redis.clients.jedis.JedisShardInfo;
|
||||
import redis.clients.jedis.ShardedJedis;
|
||||
import redis.clients.jedis.ShardedJedisPipeline;
|
||||
import redis.clients.jedis.ShardedJedisPool;
|
||||
import redis.clients.jedis.exceptions.JedisConnectionException;
|
||||
|
||||
@@ -233,4 +234,69 @@ public class ShardedJedisPoolTest extends Assert {
|
||||
assertEquals("PONG", jedis.ping());
|
||||
assertEquals("bar", jedis.get("foo"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void returnResourceShouldResetState() throws URISyntaxException {
|
||||
GenericObjectPoolConfig config = new GenericObjectPoolConfig();
|
||||
config.setMaxTotal(1);
|
||||
config.setBlockWhenExhausted(false);
|
||||
|
||||
List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>();
|
||||
shards.add(new JedisShardInfo(new URI(
|
||||
"redis://:foobared@localhost:6380")));
|
||||
shards.add(new JedisShardInfo(new URI(
|
||||
"redis://:foobared@localhost:6379")));
|
||||
|
||||
ShardedJedisPool pool = new ShardedJedisPool(config, shards);
|
||||
|
||||
ShardedJedis jedis = pool.getResource();
|
||||
jedis.set("pipelined", String.valueOf(0));
|
||||
jedis.set("pipelined2", String.valueOf(0));
|
||||
|
||||
ShardedJedisPipeline pipeline = jedis.pipelined();
|
||||
|
||||
pipeline.incr("pipelined");
|
||||
pipeline.incr("pipelined2");
|
||||
|
||||
jedis.resetState();
|
||||
|
||||
pipeline = jedis.pipelined();
|
||||
pipeline.incr("pipelined");
|
||||
pipeline.incr("pipelined2");
|
||||
List<Object> results = pipeline.syncAndReturnAll();
|
||||
|
||||
assertEquals(2, results.size());
|
||||
pool.returnResource(jedis);
|
||||
pool.destroy();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void checkResourceIsCloseable() throws URISyntaxException {
|
||||
GenericObjectPoolConfig config = new GenericObjectPoolConfig();
|
||||
config.setMaxTotal(1);
|
||||
config.setBlockWhenExhausted(false);
|
||||
|
||||
List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>();
|
||||
shards.add(new JedisShardInfo(new URI(
|
||||
"redis://:foobared@localhost:6380")));
|
||||
shards.add(new JedisShardInfo(new URI(
|
||||
"redis://:foobared@localhost:6379")));
|
||||
|
||||
ShardedJedisPool pool = new ShardedJedisPool(config, shards);
|
||||
|
||||
ShardedJedis jedis = pool.getResource();
|
||||
try {
|
||||
jedis.set("hello", "jedis");
|
||||
} finally {
|
||||
jedis.close();
|
||||
}
|
||||
|
||||
ShardedJedis jedis2 = pool.getResource();
|
||||
try {
|
||||
assertEquals(jedis, jedis2);
|
||||
} finally {
|
||||
jedis2.close();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -302,4 +302,25 @@ public class ShardedJedisTest extends Assert {
|
||||
assertEquals(jedisShardInfo.getName(), jedisShardInfo2.getName());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void checkCloseable() {
|
||||
List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>();
|
||||
shards.add(new JedisShardInfo(redis1.getHost(), redis1.getPort()));
|
||||
shards.add(new JedisShardInfo(redis2.getHost(), redis2.getPort()));
|
||||
shards.get(0).setPassword("foobared");
|
||||
shards.get(1).setPassword("foobared");
|
||||
|
||||
ShardedJedis jedisShard = new ShardedJedis(shards);
|
||||
try {
|
||||
jedisShard.set("shard_closeable", "true");
|
||||
} finally {
|
||||
jedisShard.close();
|
||||
}
|
||||
|
||||
for (Jedis jedis : jedisShard.getAllShards()) {
|
||||
assertTrue(!jedis.isConnected());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user