Skip to content

Commit

Permalink
Polishing
Browse files Browse the repository at this point in the history
Improve SLOWLOG test resiliency, add assumption in case the build is slower to not impact our test.

Improve test synchronization to reduce test failures.
  • Loading branch information
mp911de committed Dec 19, 2017
1 parent f81db76 commit 14680d6
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 79 deletions.
29 changes: 17 additions & 12 deletions src/test/java/com/lambdaworks/SslTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,15 @@

import java.io.File;
import java.security.cert.CertificateException;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.ExecutionException;

import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.test.util.ReflectionTestUtils;

import com.lambdaworks.redis.*;
import com.lambdaworks.redis.api.StatefulRedisConnection;
Expand Down Expand Up @@ -245,22 +247,25 @@ public void pubSubSslAndBreakConnection() throws Exception {
redisClient.setOptions(ClientOptions.builder().suspendReconnectOnProtocolFailure(true).build());

RedisPubSubAsyncCommands<String, String> connection = redisClient.connectPubSub(redisURI).async();
connection.subscribe("c1").get();
connection.subscribe("c2").get();
Thread.sleep(200);

RedisPubSubAsyncCommands<String, String> connection2 = redisClient.connectPubSub(redisURI).async();

assertThat(connection2.pubsubChannels().get()).contains("c1", "c2");

redisURI.setVerifyPeer(true);
connection.subscribe("c1");
connection.subscribe("c2");

connection.quit();
Thread.sleep(500);
Wait.untilTrue(() -> connection2.pubsubChannels().get().containsAll(Arrays.asList("c1", "c2"))).waitOrTimeout();

try {
connection.quit().get();
} catch (Exception e) {
}

List<String> future = connection2.pubsubChannels().get();
assertThat(future).doesNotContain("c1", "c2");

RedisFuture<List<String>> future = connection2.pubsubChannels();
assertThat(future.get()).doesNotContain("c1", "c2");
assertThat(future.isDone()).isEqualTo(true);
RedisChannelHandler channelHandler = (RedisChannelHandler) connection.getStatefulConnection();
RedisChannelWriter channelWriter = channelHandler.getChannelWriter();
Wait.untilNotEquals(null, () -> ReflectionTestUtils.getField(channelWriter, "connectionError")).waitOrTimeout();

RedisFuture<List<String>> defectFuture = connection.pubsubChannels();

Expand All @@ -274,7 +279,7 @@ public void pubSubSslAndBreakConnection() throws Exception {
assertThat(e).hasRootCauseInstanceOf(CertificateException.class);
}

assertThat(defectFuture.isDone()).isEqualTo(true);
assertThat(defectFuture).isDone();

connection.close();
connection2.close();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2011-2016 the original author or authors.
* Copyright 2011-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -59,7 +59,7 @@ public class AbstractClusterTest extends AbstractTest {
public ClusterRule clusterRule = new ClusterRule(clusterClient, port1, port2, port3, port4);

@BeforeClass
public static void setupClusterClient() throws Exception {
public static void setupClusterClient() {
clusterClient = RedisClusterClient.create(LettuceLists.unmodifiableList(RedisURI.Builder.redis(host, port1).build()));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2011-2016 the original author or authors.
* Copyright 2011-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -31,6 +31,7 @@
import org.springframework.test.util.ReflectionTestUtils;

import com.lambdaworks.TestClientResources;
import com.lambdaworks.Wait;
import com.lambdaworks.redis.*;
import com.lambdaworks.redis.api.StatefulConnection;
import com.lambdaworks.redis.api.StatefulRedisConnection;
Expand Down Expand Up @@ -63,8 +64,10 @@ public class RedisClusterClientTest extends AbstractClusterTest {
protected RedisAdvancedClusterCommands<String, String> sync;

@BeforeClass
public static void setupClient() throws Exception {
public static void setupClient() {

setupClusterClient();

client = RedisClient.create(TestClientResources.get(), RedisURI.Builder.redis(host, port1).build());
clusterClient = RedisClusterClient.create(TestClientResources.get(),
Collections.singletonList(RedisURI.Builder.redis(host, port1)
Expand All @@ -79,7 +82,7 @@ public static void shutdownClient() {
}

@Before
public void before() throws Exception {
public void before() {

clusterClient.setOptions(ClusterClientOptions.create());
clusterRule.getClusterClient().reloadPartitions();
Expand All @@ -99,7 +102,7 @@ public void before() throws Exception {
}

@After
public void after() throws Exception {
public void after() {
sync.close();
redis1.close();

Expand All @@ -110,21 +113,21 @@ public void after() throws Exception {
}

@Test
public void statefulConnectionFromSync() throws Exception {
public void statefulConnectionFromSync() {
RedisAdvancedClusterConnection<String, String> sync = clusterClient.connectCluster();
assertThat(sync.getStatefulConnection().sync()).isSameAs(sync);
sync.close();
}

@Test
public void statefulConnectionFromAsync() throws Exception {
public void statefulConnectionFromAsync() {
RedisAsyncConnection<String, String> async = client.connectAsync();
assertThat(async.getStatefulConnection().async()).isSameAs(async);
async.close();
}

@Test
public void shouldApplyTimeoutOnRegularConnection() throws Exception {
public void shouldApplyTimeoutOnRegularConnection() {

clusterClient.setDefaultTimeout(1, TimeUnit.MINUTES);

Expand All @@ -137,7 +140,7 @@ public void shouldApplyTimeoutOnRegularConnection() throws Exception {
}

@Test
public void shouldApplyTimeoutOnRegularConnectionUsingCodec() throws Exception {
public void shouldApplyTimeoutOnRegularConnectionUsingCodec() {

clusterClient.setDefaultTimeout(1, TimeUnit.MINUTES);

Expand All @@ -150,7 +153,7 @@ public void shouldApplyTimeoutOnRegularConnectionUsingCodec() throws Exception {
}

@Test
public void shouldApplyTimeoutOnPubSubConnection() throws Exception {
public void shouldApplyTimeoutOnPubSubConnection() {

clusterClient.setDefaultTimeout(1, TimeUnit.MINUTES);

Expand All @@ -161,7 +164,7 @@ public void shouldApplyTimeoutOnPubSubConnection() throws Exception {
}

@Test
public void shouldApplyTimeoutOnPubSubConnectionUsingCodec() throws Exception {
public void shouldApplyTimeoutOnPubSubConnectionUsingCodec() {

clusterClient.setDefaultTimeout(1, TimeUnit.MINUTES);

Expand All @@ -177,38 +180,39 @@ public void clusterConnectionShouldSetClientName() throws Exception {
StatefulRedisClusterConnection<String, String> connection = clusterClient.connect();

assertThat(connection.sync().clientGetname()).isEqualTo("my-client");
Thread.sleep(10);
connection.sync().quit();
Wait.untilTrue(connection::isOpen).waitOrTimeout();
assertThat(connection.sync().clientGetname()).isEqualTo("my-client");

StatefulRedisConnection<String, String> nodeConnection = connection.getConnection(connection.getPartitions()
.getPartition(0).getNodeId());
assertThat(nodeConnection.sync().clientGetname()).isEqualTo("my-client");
nodeConnection.sync().quit();
assertThat(nodeConnection.sync().clientGetname()).isEqualTo("my-client");

connection.close();
}

@Test
public void pubSubclusterConnectionShouldSetClientName() throws Exception {
public void pubSubclusterConnectionShouldSetClientName() throws InterruptedException {

StatefulRedisClusterPubSubConnection<String, String> connection = clusterClient.connectPubSub();

assertThat(connection.sync().clientGetname()).isEqualTo("my-client");
Thread.sleep(10);
connection.sync().quit();
Wait.untilTrue(connection::isOpen).waitOrTimeout();

assertThat(connection.sync().clientGetname()).isEqualTo("my-client");

StatefulRedisConnection<String, String> nodeConnection = connection.getConnection(connection.getPartitions()
.getPartition(0).getNodeId());
assertThat(nodeConnection.sync().clientGetname()).isEqualTo("my-client");
nodeConnection.sync().quit();
assertThat(nodeConnection.sync().clientGetname()).isEqualTo("my-client");

connection.close();
}

@Test
public void reloadPartitions() throws Exception {
public void reloadPartitions() {
assertThat(clusterClient.getPartitions()).hasSize(4);

assertThat(clusterClient.getPartitions().getPartition(0).getUri());
Expand Down Expand Up @@ -257,7 +261,6 @@ public void testClusteredOperations() throws Exception {
assertThat(setD.get()).isEqualTo("OK");

connection.close();

}

@Test
Expand Down Expand Up @@ -370,7 +373,7 @@ public void testClusterRedirectionLimit() throws Exception {
}

@Test(expected = RedisException.class)
public void closeConnection() throws Exception {
public void closeConnection() {

try (RedisAdvancedClusterCommands<String, String> connection = clusterClient.connect().sync()) {

Expand Down Expand Up @@ -431,7 +434,7 @@ public void clusterAuthPingBeforeConnect() throws Exception {
}

@Test(expected = RedisException.class)
public void clusterNeedsAuthButNotSupplied() throws Exception {
public void clusterNeedsAuthButNotSupplied() {

RedisClusterClient clusterClient = RedisClusterClient.create(TestClientResources.get(),
RedisURI.Builder.redis(TestSettings.host(), port7).build());
Expand All @@ -446,7 +449,7 @@ public void clusterNeedsAuthButNotSupplied() throws Exception {
}

@Test
public void noClusterNodeAvailable() throws Exception {
public void noClusterNodeAvailable() {

RedisClusterClient clusterClient = RedisClusterClient.create(TestClientResources.get(),
RedisURI.Builder.redis(host, 40400).build());
Expand All @@ -459,7 +462,7 @@ public void noClusterNodeAvailable() throws Exception {
}

@Test
public void getClusterNodeConnection() throws Exception {
public void getClusterNodeConnection() {

RedisClusterNode redis1Node = getOwnPartition(redissync2);

Expand All @@ -471,7 +474,7 @@ public void getClusterNodeConnection() throws Exception {
}

@Test
public void operateOnNodeConnection() throws Exception {
public void operateOnNodeConnection() {

sync.set(KEY_A, value);
sync.set(KEY_B, "d");
Expand All @@ -498,7 +501,7 @@ public void testStatefulConnection() throws Exception {
}

@Test(expected = RedisException.class)
public void getButNoPartitionForSlothash() throws Exception {
public void getButNoPartitionForSlothash() {

for (RedisClusterNode redisClusterNode : clusterClient.getPartitions()) {
redisClusterNode.setSlots(new ArrayList<>());
Expand Down Expand Up @@ -537,7 +540,7 @@ public void readOnlyOnCluster() throws Exception {
}

@Test
public void getKeysInSlot() throws Exception {
public void getKeysInSlot() {

sync.set(KEY_A, value);
sync.set(KEY_B, value);
Expand All @@ -551,7 +554,7 @@ public void getKeysInSlot() throws Exception {
}

@Test
public void countKeysInSlot() throws Exception {
public void countKeysInSlot() {

sync.set(KEY_A, value);
sync.set(KEY_B, value);
Expand All @@ -569,24 +572,24 @@ public void countKeysInSlot() throws Exception {
}

@Test
public void testClusterCountFailureReports() throws Exception {
public void testClusterCountFailureReports() {
RedisClusterNode ownPartition = getOwnPartition(redissync1);
assertThat(redissync1.clusterCountFailureReports(ownPartition.getNodeId())).isGreaterThanOrEqualTo(0);
}

@Test
public void testClusterKeyslot() throws Exception {
public void testClusterKeyslot() {
assertThat(redissync1.clusterKeyslot(KEY_A)).isEqualTo(SLOT_A);
assertThat(SlotHash.getSlot(KEY_A)).isEqualTo(SLOT_A);
}

@Test
public void testClusterSaveconfig() throws Exception {
public void testClusterSaveconfig() {
assertThat(redissync1.clusterSaveconfig()).isEqualTo("OK");
}

@Test
public void testClusterSetConfigEpoch() throws Exception {
public void testClusterSetConfigEpoch() {
try {
redissync1.clusterSetConfigEpoch(1L);
} catch (RedisException e) {
Expand All @@ -595,7 +598,7 @@ public void testClusterSetConfigEpoch() throws Exception {
}

@Test
public void testReadFrom() throws Exception {
public void testReadFrom() {
StatefulRedisClusterConnection<String, String> statefulConnection = sync.getStatefulConnection();

assertThat(statefulConnection.getReadFrom()).isEqualTo(ReadFrom.MASTER);
Expand All @@ -605,12 +608,12 @@ public void testReadFrom() throws Exception {
}

@Test(expected = IllegalArgumentException.class)
public void testReadFromNull() throws Exception {
public void testReadFromNull() {
sync.getStatefulConnection().setReadFrom(null);
}

@Test
public void testPfmerge() throws Exception {
public void testPfmerge() {
RedisAdvancedClusterConnection<String, String> connection = clusterClient.connectCluster();

assertThat(SlotHash.getSlot("key2660")).isEqualTo(SlotHash.getSlot("key7112")).isEqualTo(SlotHash.getSlot("key8885"));
Expand Down
Loading

0 comments on commit 14680d6

Please sign in to comment.