Add a pool for sharded jedis

This commit is contained in:
Jonathan Leibiusky
2010-11-10 13:41:41 -03:00
parent 08f8432215
commit f62548931c
6 changed files with 382 additions and 184 deletions

View File

@@ -11,85 +11,85 @@ import redis.clients.jedis.JedisPool;
import redis.clients.jedis.tests.HostAndPortUtil.HostAndPort;
public class JedisPoolTest extends Assert {
private static HostAndPort hnp = HostAndPortUtil.getRedisServers().get(0);
@Test
public void checkConnections() throws TimeoutException {
JedisPool pool = new JedisPool(hnp.host, hnp.port, 2000);
pool.setResourcesNumber(10);
pool.init();
private static HostAndPort hnp = HostAndPortUtil.getRedisServers().get(0);
Jedis jedis = pool.getResource(200);
jedis.auth("foobared");
jedis.set("foo", "bar");
assertEquals("bar", jedis.get("foo"));
pool.returnResource(jedis);
pool.destroy();
}
@Test
public void checkConnections() throws TimeoutException {
JedisPool pool = new JedisPool(hnp.host, hnp.port, 2000);
pool.setResourcesNumber(10);
pool.init();
@Test
public void checkConnectionWithDefaultPort() throws TimeoutException {
JedisPool pool = new JedisPool(hnp.host, hnp.port);
pool.setResourcesNumber(10);
pool.init();
Jedis jedis = pool.getResource(200);
jedis.auth("foobared");
jedis.set("foo", "bar");
assertEquals("bar", jedis.get("foo"));
pool.returnResource(jedis);
pool.destroy();
}
Jedis jedis = pool.getResource(200);
jedis.auth("foobared");
jedis.set("foo", "bar");
assertEquals("bar", jedis.get("foo"));
pool.returnResource(jedis);
pool.destroy();
}
@Test
public void checkConnectionWithDefaultPort() throws TimeoutException {
JedisPool pool = new JedisPool(hnp.host, hnp.port);
pool.setResourcesNumber(10);
pool.init();
@Test
public void checkJedisIsReusedWhenReturned() throws TimeoutException {
JedisPool pool = new JedisPool(hnp.host, hnp.port);
pool.setResourcesNumber(1);
pool.init();
Jedis jedis = pool.getResource(200);
jedis.auth("foobared");
jedis.set("foo", "bar");
assertEquals("bar", jedis.get("foo"));
pool.returnResource(jedis);
pool.destroy();
}
Jedis jedis = pool.getResource(200);
jedis.auth("foobared");
jedis.set("foo", "0");
pool.returnResource(jedis);
@Test
public void checkJedisIsReusedWhenReturned() throws TimeoutException {
JedisPool pool = new JedisPool(hnp.host, hnp.port);
pool.setResourcesNumber(1);
pool.init();
jedis = pool.getResource(200);
jedis.auth("foobared");
jedis.incr("foo");
pool.returnResource(jedis);
pool.destroy();
}
Jedis jedis = pool.getResource(200);
jedis.auth("foobared");
jedis.set("foo", "0");
pool.returnResource(jedis);
@Test
public void checkPoolRepairedWhenJedisIsBroken() throws TimeoutException,
IOException {
JedisPool pool = new JedisPool(hnp.host, hnp.port);
pool.setResourcesNumber(1);
pool.init();
jedis = pool.getResource(200);
jedis.auth("foobared");
jedis.incr("foo");
pool.returnResource(jedis);
pool.destroy();
}
Jedis jedis = pool.getResource(200);
jedis.auth("foobared");
jedis.quit();
pool.returnBrokenResource(jedis);
@Test
public void checkPoolRepairedWhenJedisIsBroken() throws TimeoutException,
IOException {
JedisPool pool = new JedisPool(hnp.host, hnp.port);
pool.setResourcesNumber(1);
pool.init();
jedis = pool.getResource(200);
jedis.auth("foobared");
jedis.incr("foo");
pool.returnResource(jedis);
pool.destroy();
}
Jedis jedis = pool.getResource(200);
jedis.auth("foobared");
jedis.quit();
pool.returnBrokenResource(jedis);
@Test(expected = TimeoutException.class)
public void checkPoolOverflow() throws TimeoutException {
JedisPool pool = new JedisPool(hnp.host, hnp.port);
pool.setResourcesNumber(1);
pool.init();
jedis = pool.getResource(200);
jedis.auth("foobared");
jedis.incr("foo");
pool.returnResource(jedis);
pool.destroy();
}
Jedis jedis = pool.getResource(200);
jedis.auth("foobared");
jedis.set("foo", "0");
@Test(expected = TimeoutException.class)
public void checkPoolOverflow() throws TimeoutException {
JedisPool pool = new JedisPool(hnp.host, hnp.port);
pool.setResourcesNumber(1);
pool.init();
Jedis newJedis = pool.getResource(200);
newJedis.auth("foobared");
newJedis.incr("foo");
}
Jedis jedis = pool.getResource(200);
jedis.auth("foobared");
jedis.set("foo", "0");
Jedis newJedis = pool.getResource(200);
newJedis.auth("foobared");
newJedis.incr("foo");
}
}