Skip to content

Commit

Permalink
Added a test with a Redis instance being killed
Browse files Browse the repository at this point in the history
  • Loading branch information
sazzad16 committed Feb 7, 2021
1 parent 1f93b18 commit ab9ed11
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 0 deletions.
14 changes: 14 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,17 @@ save ""
appendonly no
endef

# UNAVAILABLE REDIS NODES
define REDIS_UNAVAILABLE_CONF
daemonize yes
protected-mode no
port 6400
pidfile /tmp/redis_unavailable.pid
logfile /tmp/redis_unavailable.log
save ""
appendonly no
endef

#STUNNEL
define STUNNEL_CONF
cert = src/test/resources/private.pem
Expand Down Expand Up @@ -278,6 +289,7 @@ export REDIS_CLUSTER_NODE3_CONF
export REDIS_CLUSTER_NODE4_CONF
export REDIS_CLUSTER_NODE5_CONF
export REDIS_UDS
export REDIS_UNAVAILABLE_CONF
export STUNNEL_CONF
export STUNNEL_BIN

Expand Down Expand Up @@ -309,6 +321,7 @@ start: stunnel cleanup
echo "$$REDIS_CLUSTER_NODE4_CONF" | redis-server -
echo "$$REDIS_CLUSTER_NODE5_CONF" | redis-server -
echo "$$REDIS_UDS" | redis-server -
echo "$$REDIS_UNAVAILABLE_CONF" | redis-server -

cleanup:
- rm -vf /tmp/redis_cluster_node*.conf 2>/dev/null
Expand Down Expand Up @@ -338,6 +351,7 @@ stop:
kill `cat /tmp/redis_cluster_node5.pid` || true
kill `cat /tmp/redis_uds.pid` || true
kill `cat /tmp/stunnel.pid` || true
# kill `cat /tmp/redis_unavailable.pid` || true
rm -f /tmp/sentinel1.conf
rm -f /tmp/sentinel2.conf
rm -f /tmp/sentinel3.conf
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package redis.clients.jedis.tests;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.junit.BeforeClass;
import org.junit.Test;

import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.exceptions.JedisConnectionException;

public class UnavailableConnectionTest {

private static final HostAndPort unavailableHostAndPort = new HostAndPort("localhost", 6400);

@BeforeClass
public static void setup() {
setupAvoidQuitInDestroyObject();

try (Jedis j = new Jedis(unavailableHostAndPort)) {
j.shutdown();
}
}

public static void cleanup() {
cleanupAvoidQuitInDestroyObject();
}

private static JedisPool poolForBrokenJedis1;
private static Thread threadForBrokenJedis1;
private static Jedis brokenJedis1;

public static void setupAvoidQuitInDestroyObject() {
GenericObjectPoolConfig<Jedis> config = new GenericObjectPoolConfig<>();
config.setMaxTotal(1);
poolForBrokenJedis1 = new JedisPool(config, unavailableHostAndPort.getHost(), unavailableHostAndPort.getPort());
brokenJedis1 = poolForBrokenJedis1.getResource();
threadForBrokenJedis1 = new Thread(new Runnable() {
@Override
public void run() {
brokenJedis1.blpop(0, "broken-key-1");
}
});
threadForBrokenJedis1.start();
}

@Test(timeout = 5000)
public void testAvoidQuitInDestroyObjectForBrokenConnection() throws InterruptedException {
threadForBrokenJedis1.join();
assertFalse(threadForBrokenJedis1.isAlive());
assertTrue(brokenJedis1.isBroken());
brokenJedis1.close(); // we need capture/mock to test this properly

try {
poolForBrokenJedis1.getResource();
fail("Should not get connection from pool");
} catch(Exception ex) {
assertEquals(JedisConnectionException.class, ex.getClass());
assertEquals(JedisConnectionException.class, ex.getCause().getClass());
assertEquals(java.net.ConnectException.class, ex.getCause().getCause().getClass());
}
}

public static void cleanupAvoidQuitInDestroyObject() {
poolForBrokenJedis1.close();
}
}

0 comments on commit ab9ed11

Please sign in to comment.