package redis.clients.jedis.tests;

import org.apache.commons.pool.impl.GenericObjectPool;
import org.apache.commons.pool.impl.GenericObjectPool.Config;
import org.junit.Assert;
import org.junit.Test;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisException;
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 Exception {
        JedisPool pool = new JedisPool(new Config(), hnp.host, hnp.port, 2000);
        Jedis jedis = pool.getResource();
        jedis.auth("foobared");
        jedis.set("foo", "bar");
        assertEquals("bar", jedis.get("foo"));
        pool.returnResource(jedis);
        pool.destroy();
    }

    @Test
    public void checkConnectionWithDefaultPort() throws Exception {
        JedisPool pool = new JedisPool(new Config(), hnp.host, hnp.port);
        Jedis jedis = pool.getResource();
        jedis.auth("foobared");
        jedis.set("foo", "bar");
        assertEquals("bar", jedis.get("foo"));
        pool.returnResource(jedis);
        pool.destroy();
    }

    @Test
    public void checkJedisIsReusedWhenReturned() throws Exception {
        JedisPool pool = new JedisPool(new Config(), hnp.host, hnp.port);
        Jedis jedis = pool.getResource();
        jedis.auth("foobared");
        jedis.set("foo", "0");
        pool.returnResource(jedis);

        jedis = pool.getResource();
        jedis.auth("foobared");
        jedis.incr("foo");
        pool.returnResource(jedis);
        pool.destroy();
    }

    @Test
    public void checkPoolRepairedWhenJedisIsBroken() throws Exception {
        JedisPool pool = new JedisPool(new Config(), hnp.host, hnp.port);
        Jedis jedis = pool.getResource();
        jedis.auth("foobared");
        jedis.quit();
        pool.returnBrokenResource(jedis);

        jedis = pool.getResource();
        jedis.auth("foobared");
        jedis.incr("foo");
        pool.returnResource(jedis);
        pool.destroy();
    }

    @Test(expected = JedisException.class)
    public void checkPoolOverflow() {
        Config config = new Config();
        config.maxActive = 1;
        config.whenExhaustedAction = GenericObjectPool.WHEN_EXHAUSTED_FAIL;
        JedisPool pool = new JedisPool(config, hnp.host, hnp.port);
        Jedis jedis = pool.getResource();
        jedis.auth("foobared");
        jedis.set("foo", "0");

        Jedis newJedis = pool.getResource();
        newJedis.auth("foobared");
        newJedis.incr("foo");
    }
}
@@ -306,24 +306,68 @@
 .JedisPool;%0A
+import redis.clients.jedis.JedisPoolConfig;%0A
 import redis
@@ -416,16 +416,16 @@
 ndPort;%0A
-
 %0Apublic 
@@ -640,32 +640,41 @@
 w JedisPool(new 
+JedisPool
 Config(), hnp.ho
@@ -668,32 +668,48 @@
 fig(), hnp.host,
+%0A               
  hnp.port, 2000)
@@ -1044,32 +1044,41 @@
 w JedisPool(new 
+JedisPool
 Config(), hnp.ho
@@ -1072,32 +1072,48 @@
 fig(), hnp.host,
+%0A               
  hnp.port);%0A    
@@ -1442,32 +1442,41 @@
 w JedisPool(new 
+JedisPool
 Config(), hnp.ho
@@ -1470,32 +1470,48 @@
 fig(), hnp.host,
+%0A               
  hnp.port);%0A    
@@ -1931,24 +1931,33 @@
 disPool(new 
+JedisPool
 Config(), hn
@@ -1955,32 +1955,48 @@
 fig(), hnp.host,
+%0A               
  hnp.port);%0A    

test use the new jedis pool config