package redis.clients.jedis.tests;

import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Set;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.Pipeline;
import redis.clients.jedis.PipelineBlock;
import redis.clients.jedis.Response;
import redis.clients.jedis.exceptions.JedisDataException;
import redis.clients.jedis.tests.HostAndPortUtil.HostAndPort;

public class PipeliningTest extends Assert {
    private static HostAndPort hnp = HostAndPortUtil.getRedisServers().get(0);

    private Jedis jedis;

    @Before
    public void setUp() throws Exception {
        jedis = new Jedis(hnp.host, hnp.port, 500);
        jedis.connect();
        jedis.auth("foobared");
        jedis.flushAll();
    }

    @Test
    public void pipeline() throws UnsupportedEncodingException {
        List<Object> results = jedis.pipelined(new PipelineBlock() {
            public void execute() {
                set("foo", "bar");
                get("foo");
            }
        });

        assertEquals(2, results.size());
        assertEquals("OK", results.get(0));
        assertEquals("bar", results.get(1));

        Pipeline p = jedis.pipelined();
        p.set("foo", "bar");
        p.get("foo");
        results = p.sync();

        assertEquals(2, results.size());
        assertEquals("OK", results.get(0));
        assertEquals("bar", results.get(1));

    }

    @Test
    public void pipelineResponse() {
        jedis.set("string", "foo");
        jedis.lpush("list", "foo");
        jedis.hset("hash", "foo", "bar");
        jedis.zadd("zset", 1, "foo");
        jedis.sadd("set", "foo");

        Pipeline p = jedis.pipelined();
        Response<String> string = p.get("string");
        Response<String> list = p.lpop("list");
        Response<String> hash = p.hget("hash", "foo");
        Response<Set<String>> zset = p.zrange("zset", 0, -1);
        Response<String> set = p.spop("set");
        p.sync();

        assertEquals("foo", string.get());
        assertEquals("foo", list.get());
        assertEquals("bar", hash.get());
        assertEquals("foo", zset.get().iterator().next());
        assertEquals("foo", set.get());
    }

    @Test(expected = JedisDataException.class)
    public void pipelineResponseWithinPipeline() {
        jedis.set("string", "foo");

        Pipeline p = jedis.pipelined();
        Response<String> string = p.get("string");
        string.get();
        p.sync();
    }
}

@@ -97,16 +97,38 @@
 l.List;%0A
+import java.util.Map;%0A
 import j
@@ -366,16 +366,50 @@
 sponse;%0A
+import redis.clients.jedis.Tuple;%0A
 import r
@@ -2076,238 +2076,1096 @@
     
-p.sync();%0A%0A        assertEquals(%22foo%22, string.get());%0A        assertEquals(%22foo%22, list.get());%0A        assertEquals(%22bar%22, hash.get());%0A        assertEquals(%22foo%22, zset.get().iterator().next());%0A        assertEquals(%22foo%22, set.get
+Response%3CBoolean%3E blist = p.exists(%22list%22);%0A        Response%3CDouble%3E zincrby = p.zincrby(%22zset%22, 1, %22foo%22);%0A        Response%3CLong%3E zcard = p.zcard(%22zset%22);%0A        p.lpush(%22list%22, %22bar%22);%0A        Response%3CList%3CString%3E%3E lrange = p.lrange(%22list%22, 0, -1);%0A        Response%3CMap%3CString, String%3E%3E hgetAll = p.hgetAll(%22hash%22);%0A        p.sadd(%22set%22, %22foo%22);%0A        Response%3CSet%3CString%3E%3E smembers = p.smembers(%22set%22);%0A        Response%3CSet%3CTuple%3E%3E zrangeWithScores = p.zrangeWithScores(%22zset%22, 0,%0A                -1);%0A        p.sync();%0A%0A        assertEquals(%22foo%22, string.get());%0A        assertEquals(%22foo%22, list.get());%0A        assertEquals(%22bar%22, hash.get());%0A        assertEquals(%22foo%22, zset.get().iterator().next());%0A        assertEquals(%22foo%22, set.get());%0A        assertEquals(false, blist.get());%0A        assertEquals(new Double(2), zincrby.get());%0A        assertEquals(new Long(1), zcard.get());%0A        assertEquals(1, lrange.get().size());%0A        assertNotNull(hgetAll.get().get(%22foo%22));%0A        assertEquals(1, smembers.get().size());%0A        assertEquals(1, zrangeWithScores.get().size
 ());
@@ -3445,9 +3445,8 @@
 %0A    %7D%0A%7D
-%0A

add more tests to pipeline repsonse