@Test(expected = JedisConnectionException.class) public void initializeWithNotAvailableSentinelsShouldThrowException() { Set<String> wrongSentinels = new HashSet<String>(); wrongSentinels.add(new HostAndPort("localhost", 65432).toString()); wrongSentinels.add(new HostAndPort("localhost", 65431).toString()); JedisSentinelPool pool = new JedisSentinelPool(MASTER_NAME, wrongSentinels); pool.destroy(); }
@Test(expected = JedisException.class) public void initializeWithNotMonitoredMasterNameShouldThrowException() { final String wrongMasterName = "wrongMasterName"; JedisSentinelPool pool = new JedisSentinelPool(wrongMasterName, sentinels); pool.destroy(); }
@Test public void checkCloseableConnections() throws Exception { GenericObjectPoolConfig config = new GenericObjectPoolConfig(); JedisSentinelPool pool = new JedisSentinelPool(MASTER_NAME, sentinels, config, 1000, "foobared", 2); Jedis jedis = pool.getResource(); jedis.auth("foobared"); jedis.set("foo", "bar"); assertEquals("bar", jedis.get("foo")); pool.returnResource(jedis); pool.close(); assertTrue(pool.isClosed()); }
@Test public void ensureSafeTwiceFailover() throws InterruptedException { JedisSentinelPool pool = new JedisSentinelPool(MASTER_NAME, sentinels, new GenericObjectPoolConfig(), 2000, "foobared", 2); forceFailover(pool); // after failover sentinel needs a bit of time to stabilize before a new // failover Thread.sleep(100); forceFailover(pool); // you can test failover as much as possible }
@Test public void returningBorrowedInstanceBeforeFailoverShouldNotAffectBorrowing() throws InterruptedException { final JedisSentinelPool pool = new JedisSentinelPool(MASTER_NAME, sentinels, new GenericObjectPoolConfig(), 2000, "foobared", 2); Jedis borrowed = pool.getResource(); forceFailover(pool); Thread.sleep(1000); // returns instance which was borrowed before failover borrowed.close(); final AtomicBoolean isBorrowed = new AtomicBoolean(false); Thread t = new Thread(new Runnable() { @Override public void run() { pool.getResource(); isBorrowed.set(true); } }); t.start(); // wait for 5 secs t.join(5000); assertTrue(isBorrowed.get()); }
private void waitForJedisSentinelPoolRecognizeNewMaster(JedisSentinelPool pool, HostAndPort newMaster) throws InterruptedException {
while (true) { HostAndPort currentHostMaster = pool.getCurrentHostMaster(); if (newMaster.equals(currentHostMaster)) break; System.out.println("JedisSentinelPool's master is not yet changed, sleep..."); Thread.sleep(100); } }