Files
jlibredis/src/test/java/redis/clients/jedis/tests/utils/JedisSentinelTestUtil.java
Nelson Rodrigues 9013078d40 Fix race condition in JedisSentinelPoolTest
The test was issuing the failover command and only afterwards
connecting to the pub-sub channel to receive failover notifications.
If the failover occurred fast enought the pub-sub listener would never
get the notification.

Run the failover command on a separate Jedis connection after we're
absolutely sure that we're subscribed to the pub-sub channel.
2014-07-28 10:56:21 -07:00

63 lines
1.7 KiB
Java

package redis.clients.jedis.tests.utils;
import java.util.concurrent.atomic.AtomicReference;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPubSub;
import redis.clients.jedis.tests.utils.FailoverAbortedException;
public class JedisSentinelTestUtil {
public static HostAndPort waitForNewPromotedMaster(final String masterName,
final Jedis sentinelJedis, final Jedis commandJedis)
throws InterruptedException {
final AtomicReference<String> newmaster = new AtomicReference<String>(
"");
sentinelJedis.psubscribe(new JedisPubSub() {
@Override
public void onMessage(String channel, String message) {
}
@Override
public void onPMessage(String pattern, String channel,
String message) {
if (channel.equals("+switch-master")) {
newmaster.set(message);
punsubscribe();
} else if (channel.startsWith("-failover-abort")) {
punsubscribe();
throw new FailoverAbortedException("Unfortunately sentinel cannot failover... reason(channel) : " +
channel + " / message : " + message);
}
}
@Override
public void onSubscribe(String channel, int subscribedChannels) {
}
@Override
public void onUnsubscribe(String channel, int subscribedChannels) {
}
@Override
public void onPUnsubscribe(String pattern, int subscribedChannels) {
}
@Override
public void onPSubscribe(String pattern, int subscribedChannels) {
commandJedis.sentinelFailover(masterName);
}
}, "*");
String[] chunks = newmaster.get().split(" ");
HostAndPort newMaster = new HostAndPort(chunks[3],
Integer.parseInt(chunks[4]));
return newMaster;
}
}