Added password to Jedis, JedisPool and ShardedJedis constructor for easier and more efficient usage
This commit is contained in:
@@ -13,12 +13,12 @@ import redis.clients.jedis.Protocol;
|
||||
public class JedisPoolTest extends Assert {
|
||||
@Test
|
||||
public void checkConnections() throws TimeoutException {
|
||||
JedisPool pool = new JedisPool("localhost", Protocol.DEFAULT_PORT, 2000);
|
||||
JedisPool pool = new JedisPool("localhost", Protocol.DEFAULT_PORT,
|
||||
2000, "foobared");
|
||||
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);
|
||||
@@ -27,12 +27,12 @@ public class JedisPoolTest extends Assert {
|
||||
|
||||
@Test
|
||||
public void checkConnectionWithDefaultPort() throws TimeoutException {
|
||||
JedisPool pool = new JedisPool("localhost");
|
||||
JedisPool pool = new JedisPool("localhost", Protocol.DEFAULT_PORT,
|
||||
2000, "foobared");
|
||||
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);
|
||||
@@ -41,17 +41,16 @@ public class JedisPoolTest extends Assert {
|
||||
|
||||
@Test
|
||||
public void checkJedisIsReusedWhenReturned() throws TimeoutException {
|
||||
JedisPool pool = new JedisPool("localhost");
|
||||
JedisPool pool = new JedisPool("localhost", Protocol.DEFAULT_PORT,
|
||||
2000, "foobared");
|
||||
pool.setResourcesNumber(1);
|
||||
pool.init();
|
||||
|
||||
Jedis jedis = pool.getResource(200);
|
||||
jedis.auth("foobared");
|
||||
jedis.set("foo", "0");
|
||||
pool.returnResource(jedis);
|
||||
|
||||
jedis = pool.getResource(200);
|
||||
jedis.auth("foobared");
|
||||
jedis.incr("foo");
|
||||
pool.returnResource(jedis);
|
||||
pool.destroy();
|
||||
@@ -60,17 +59,16 @@ public class JedisPoolTest extends Assert {
|
||||
@Test
|
||||
public void checkPoolRepairedWhenJedisIsBroken() throws TimeoutException,
|
||||
IOException {
|
||||
JedisPool pool = new JedisPool("localhost");
|
||||
JedisPool pool = new JedisPool("localhost", Protocol.DEFAULT_PORT,
|
||||
2000, "foobared");
|
||||
pool.setResourcesNumber(1);
|
||||
pool.init();
|
||||
|
||||
Jedis jedis = pool.getResource(200);
|
||||
jedis.auth("foobared");
|
||||
jedis.quit();
|
||||
pool.returnBrokenResource(jedis);
|
||||
|
||||
jedis = pool.getResource(200);
|
||||
jedis.auth("foobared");
|
||||
jedis.incr("foo");
|
||||
pool.returnResource(jedis);
|
||||
pool.destroy();
|
||||
@@ -78,16 +76,15 @@ public class JedisPoolTest extends Assert {
|
||||
|
||||
@Test(expected = TimeoutException.class)
|
||||
public void checkPoolOverflow() throws TimeoutException {
|
||||
JedisPool pool = new JedisPool("localhost");
|
||||
JedisPool pool = new JedisPool("localhost", Protocol.DEFAULT_PORT,
|
||||
2000, "foobared");
|
||||
pool.setResourcesNumber(1);
|
||||
pool.init();
|
||||
|
||||
Jedis jedis = pool.getResource(200);
|
||||
jedis.auth("foobared");
|
||||
jedis.set("foo", "0");
|
||||
|
||||
Jedis newJedis = pool.getResource(200);
|
||||
newJedis.auth("foobared");
|
||||
newJedis.incr("foo");
|
||||
}
|
||||
}
|
||||
@@ -6,8 +6,10 @@ import java.util.Map;
|
||||
import org.junit.Test;
|
||||
|
||||
import redis.clients.jedis.Jedis;
|
||||
import redis.clients.jedis.Protocol;
|
||||
import redis.clients.jedis.tests.commands.JedisCommandTestBase;
|
||||
import redis.clients.util.RedisOutputStream;
|
||||
import redis.clients.util.ShardInfo;
|
||||
|
||||
public class JedisTest extends JedisCommandTestBase {
|
||||
@Test
|
||||
@@ -31,4 +33,11 @@ public class JedisTest extends JedisCommandTestBase {
|
||||
assertEquals(hash, jedis.hgetAll("foo"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void connectWithShardInfo() {
|
||||
ShardInfo shardInfo = new ShardInfo("localhost", Protocol.DEFAULT_PORT);
|
||||
shardInfo.setPassword("foobared");
|
||||
Jedis jedis = new Jedis(shardInfo);
|
||||
jedis.get("foo");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,6 +9,7 @@ import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
import redis.clients.jedis.Jedis;
|
||||
import redis.clients.jedis.JedisPool;
|
||||
import redis.clients.jedis.Protocol;
|
||||
|
||||
public class PoolBenchmark {
|
||||
private static final int TOTAL_OPERATIONS = 100000;
|
||||
@@ -25,7 +26,7 @@ public class PoolBenchmark {
|
||||
// withoutPool();
|
||||
withPool();
|
||||
long elapsed = System.currentTimeMillis() - t;
|
||||
System.out.println(((1000 * 3 * TOTAL_OPERATIONS) / elapsed) + " ops");
|
||||
System.out.println(((1000 * 2 * TOTAL_OPERATIONS) / elapsed) + " ops");
|
||||
}
|
||||
|
||||
private static void withoutPool() throws InterruptedException {
|
||||
@@ -60,7 +61,8 @@ public class PoolBenchmark {
|
||||
}
|
||||
|
||||
private static void withPool() throws InterruptedException {
|
||||
final JedisPool pool = new JedisPool("localhost");
|
||||
final JedisPool pool = new JedisPool("localhost",
|
||||
Protocol.DEFAULT_PORT, 2000, "foobared");
|
||||
pool.setResourcesNumber(50);
|
||||
pool.setDefaultPoolWait(1000000);
|
||||
pool.init();
|
||||
@@ -73,7 +75,6 @@ public class PoolBenchmark {
|
||||
for (int i = 0; (i = ind.getAndIncrement()) < TOTAL_OPERATIONS;) {
|
||||
try {
|
||||
Jedis j = pool.getResource();
|
||||
j.auth("foobared");
|
||||
final String key = "foo" + i;
|
||||
j.set(key, key);
|
||||
j.get(key);
|
||||
|
||||
Reference in New Issue
Block a user