-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added a test with a Redis instance being killed
- Loading branch information
Showing
2 changed files
with
86 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
src/test/java/redis/clients/jedis/tests/UnavailableConnectionTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |