Add pub/sub affordances to FaultTolerantRedisCluster.

This commit is contained in:
Jon Chambers
2020-08-04 13:31:29 -04:00
committed by Jon Chambers
parent 189f8afcc9
commit 9457325119
2 changed files with 45 additions and 5 deletions

View File

@@ -5,6 +5,8 @@ import io.lettuce.core.RedisException;
import io.lettuce.core.cluster.RedisClusterClient;
import io.lettuce.core.cluster.api.StatefulRedisClusterConnection;
import io.lettuce.core.cluster.api.sync.RedisAdvancedClusterCommands;
import io.lettuce.core.cluster.pubsub.StatefulRedisClusterPubSubConnection;
import io.lettuce.core.cluster.pubsub.api.sync.RedisClusterPubSubCommands;
import org.junit.Before;
import org.junit.Test;
import org.whispersystems.textsecuregcm.configuration.CircuitBreakerConfiguration;
@@ -18,20 +20,25 @@ import static org.mockito.Mockito.when;
public class FaultTolerantRedisClusterTest {
private RedisAdvancedClusterCommands<String, String> clusterCommands;
private RedisAdvancedClusterCommands<String, String> clusterCommands;
private RedisClusterPubSubCommands<String, String> pubSubCommands;
private FaultTolerantRedisCluster faultTolerantCluster;
@SuppressWarnings("unchecked")
@Before
public void setUp() {
final RedisClusterClient clusterClient = mock(RedisClusterClient.class);
final StatefulRedisClusterConnection<String, String> clusterConnection = mock(StatefulRedisClusterConnection.class);
final RedisClusterClient clusterClient = mock(RedisClusterClient.class);
final StatefulRedisClusterConnection<String, String> clusterConnection = mock(StatefulRedisClusterConnection.class);
final StatefulRedisClusterPubSubConnection<String, String> pubSubConnection = mock(StatefulRedisClusterPubSubConnection.class);
clusterCommands = mock(RedisAdvancedClusterCommands.class);
pubSubCommands = mock(RedisClusterPubSubCommands.class);
when(clusterClient.connect()).thenReturn(clusterConnection);
when(clusterClient.connectPubSub()).thenReturn(pubSubConnection);
when(clusterConnection.sync()).thenReturn(clusterCommands);
when(pubSubConnection.sync()).thenReturn(pubSubCommands);
final CircuitBreakerConfiguration breakerConfiguration = new CircuitBreakerConfiguration();
breakerConfiguration.setFailureRateThreshold(100);
@@ -85,4 +92,19 @@ public class FaultTolerantRedisClusterTest {
assertThrows(CircuitBreakerOpenException.class,
() -> faultTolerantCluster.withWriteCluster(connection -> connection.sync().get("OH NO")));
}
@Test
public void testPubSubBreaker() {
when(pubSubCommands.publish(anyString(), anyString()))
.thenReturn(1L)
.thenThrow(new RedisException("Badness has ensued."));
assertEquals(1L, (long)faultTolerantCluster.withPubSubConnection(connection -> connection.sync().publish("channel", "message")));
assertThrows(RedisException.class,
() -> faultTolerantCluster.withPubSubConnection(connection -> connection.sync().publish("channel", "OH NO")));
assertThrows(CircuitBreakerOpenException.class,
() -> faultTolerantCluster.withPubSubConnection(connection -> connection.sync().publish("channel", "OH NO")));
}
}