add checks when server closes idle connections

This commit is contained in:
Jonathan Leibiusky
2010-11-30 12:27:22 -03:00
parent efb27de003
commit 81ec9f8af3
3 changed files with 38 additions and 18 deletions

View File

@@ -16,9 +16,11 @@
package redis.clients.util;
import redis.clients.jedis.JedisException;
import java.io.FilterInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.*;
import redis.clients.jedis.JedisException;
public class RedisInputStream extends FilterInputStream {
@@ -83,7 +85,12 @@ public class RedisInputStream extends FilterInputStream {
} catch (IOException e) {
throw new JedisException(e);
}
return sb.toString();
String reply = sb.toString();
if (reply.isEmpty()) {
throw new JedisException(
"It seems like server has closed the connection.");
}
return reply;
}
@Override

View File

@@ -6,6 +6,7 @@ import java.util.Map;
import org.junit.Test;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisException;
import redis.clients.jedis.JedisShardInfo;
import redis.clients.jedis.Protocol;
import redis.clients.jedis.tests.commands.JedisCommandTestBase;
@@ -41,4 +42,16 @@ public class JedisTest extends JedisCommandTestBase {
Jedis jedis = new Jedis(shardInfo);
jedis.get("foo");
}
@Test(expected = JedisException.class)
public void timeoutConnection() throws Exception {
jedis = new Jedis("localhost", 6379, 15000);
jedis.auth("foobared");
jedis.configSet("timeout", "1");
// we need to sleep a long time since redis check for idle connections
// every 10 seconds or so
Thread.sleep(20000);
jedis.hmget("foobar", "foo");
jedis.configSet("timeout", "300");
}
}