Skip to content

Commit 5ea0772

Browse files
committed
DATAREDIS-976 - Add Pub/Sub tests for Redis Cluster.
Original pull request: #450.
1 parent 27585c5 commit 5ea0772

File tree

3 files changed

+70
-17
lines changed

3 files changed

+70
-17
lines changed

src/main/java/org/springframework/data/redis/connection/lettuce/LettuceClusterConnection.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@
2222
import io.lettuce.core.cluster.SlotHash;
2323
import io.lettuce.core.cluster.api.StatefulRedisClusterConnection;
2424
import io.lettuce.core.cluster.api.sync.RedisClusterCommands;
25-
import io.lettuce.core.cluster.models.partitions.Partitions;
26-
import io.lettuce.core.cluster.pubsub.StatefulRedisClusterPubSubConnection;
2725
import lombok.RequiredArgsConstructor;
2826

2927
import java.time.Duration;
@@ -55,6 +53,9 @@
5553
import org.springframework.util.ObjectUtils;
5654

5755
/**
56+
* {@code RedisClusterConnection} implementation on top of <a href="https://github.com/mp911de/lettuce">Lettuce</a>
57+
* Redis client.
58+
*
5859
* @author Christoph Strobl
5960
* @author Mark Paluch
6061
* @since 1.7

src/test/java/org/springframework/data/redis/listener/PubSubTestParams.java

+49-4
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,24 @@
1515
*/
1616
package org.springframework.data.redis.listener;
1717

18-
import java.util.Arrays;
18+
import java.util.ArrayList;
1919
import java.util.Collection;
2020

21+
import org.junit.runners.model.Statement;
22+
2123
import org.springframework.data.redis.ObjectFactory;
2224
import org.springframework.data.redis.Person;
2325
import org.springframework.data.redis.PersonObjectFactory;
2426
import org.springframework.data.redis.RawObjectFactory;
2527
import org.springframework.data.redis.SettingsUtils;
2628
import org.springframework.data.redis.StringObjectFactory;
29+
import org.springframework.data.redis.connection.RedisClusterConfiguration;
2730
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
2831
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
2932
import org.springframework.data.redis.connection.lettuce.LettuceTestClientResources;
3033
import org.springframework.data.redis.core.RedisTemplate;
3134
import org.springframework.data.redis.core.StringRedisTemplate;
35+
import org.springframework.data.redis.test.util.RedisClusterRule;
3236

3337
/**
3438
* @author Costin Leau
@@ -76,8 +80,49 @@ public static Collection<Object[]> testParams() {
7680
rawTemplateLtc.setConnectionFactory(lettuceConnFactory);
7781
rawTemplateLtc.afterPropertiesSet();
7882

79-
return Arrays.asList(new Object[][] { { stringFactory, stringTemplate }, { personFactory, personTemplate },
80-
{ rawFactory, rawTemplate }, { stringFactory, stringTemplateLtc }, { personFactory, personTemplateLtc },
81-
{ rawFactory, rawTemplateLtc } });
83+
Collection<Object[]> parameters = new ArrayList<>();
84+
parameters.add(new Object[] { stringFactory, stringTemplate });
85+
parameters.add(new Object[] { personFactory, personTemplate });
86+
parameters.add(new Object[] { stringFactory, stringTemplateLtc });
87+
parameters.add(new Object[] { personFactory, personTemplateLtc });
88+
parameters.add(new Object[] { rawFactory, rawTemplateLtc });
89+
90+
if (clusterAvailable()) {
91+
92+
RedisClusterConfiguration configuration = new RedisClusterConfiguration().clusterNode("127.0.0.1", 7379);
93+
94+
// add Jedis
95+
JedisConnectionFactory jedisClusterFactory = new JedisConnectionFactory(configuration);
96+
jedisClusterFactory.afterPropertiesSet();
97+
98+
RedisTemplate<String, String> jedisClusterStringTemplate = new StringRedisTemplate(jedisClusterFactory);
99+
100+
// add Lettuce
101+
LettuceConnectionFactory lettuceClusterFactory = new LettuceConnectionFactory(configuration);
102+
lettuceClusterFactory.setClientResources(LettuceTestClientResources.getSharedClientResources());
103+
lettuceClusterFactory.afterPropertiesSet();
104+
105+
RedisTemplate<String, String> lettuceClusterStringTemplate = new StringRedisTemplate(lettuceClusterFactory);
106+
107+
parameters.add(new Object[] { stringFactory, jedisClusterStringTemplate });
108+
parameters.add(new Object[] { stringFactory, lettuceClusterStringTemplate });
109+
}
110+
111+
return parameters;
112+
}
113+
114+
private static boolean clusterAvailable() {
115+
116+
try {
117+
new RedisClusterRule().apply(new Statement() {
118+
@Override
119+
public void evaluate() {
120+
121+
}
122+
}, null).evaluate();
123+
} catch (Throwable throwable) {
124+
return false;
125+
}
126+
return true;
82127
}
83128
}

src/test/java/org/springframework/data/redis/listener/PubSubTests.java

+18-11
Original file line numberDiff line numberDiff line change
@@ -30,32 +30,31 @@
3030
import org.junit.After;
3131
import org.junit.AfterClass;
3232
import org.junit.Before;
33-
import org.junit.BeforeClass;
34-
import org.junit.Rule;
3533
import org.junit.Test;
3634
import org.junit.runner.RunWith;
3735
import org.junit.runners.Parameterized;
3836
import org.junit.runners.Parameterized.Parameters;
37+
3938
import org.springframework.core.task.SimpleAsyncTaskExecutor;
4039
import org.springframework.core.task.SyncTaskExecutor;
4140
import org.springframework.data.redis.ConnectionFactoryTracker;
4241
import org.springframework.data.redis.ObjectFactory;
43-
import org.springframework.data.redis.RedisTestProfileValueSource;
42+
import org.springframework.data.redis.connection.RedisConnectionFactory;
43+
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
44+
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
4445
import org.springframework.data.redis.core.RedisTemplate;
4546
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;
46-
import org.springframework.data.redis.test.util.RedisSentinelRule;
4747

4848
/**
4949
* Base test class for PubSub integration tests
5050
*
5151
* @author Costin Leau
5252
* @author Jennifer Hickey
53+
* @author Mark Paluch
5354
*/
5455
@RunWith(Parameterized.class)
5556
public class PubSubTests<T> {
5657

57-
public @Rule RedisSentinelRule sentinelRule = RedisSentinelRule.withDefaultConfig().sentinelsDisabled();
58-
5958
private static final String CHANNEL = "pubsub::test";
6059

6160
protected RedisMessageListenerContainer container;
@@ -73,11 +72,6 @@ public void handleMessage(Object message) {
7372

7473
private final MessageListenerAdapter adapter = new MessageListenerAdapter(handler);
7574

76-
@BeforeClass
77-
public static void shouldRun() {
78-
assumeTrue(RedisTestProfileValueSource.matches("runLongTests", "true"));
79-
}
80-
8175
@Before
8276
public void setUp() throws Exception {
8377
bag.clear();
@@ -178,6 +172,9 @@ public void testStartNoListeners() {
178172
@SuppressWarnings("unchecked")
179173
@Test // DATAREDIS-251
180174
public void testStartListenersToNoSpecificChannelTest() throws InterruptedException {
175+
176+
assumeTrue(isClusterAware(template.getConnectionFactory()));
177+
181178
container.removeMessageListener(adapter, new ChannelTopic(CHANNEL));
182179
container.addMessageListener(adapter, Arrays.asList(new PatternTopic("*")));
183180
container.start();
@@ -193,4 +190,14 @@ public void testStartListenersToNoSpecificChannelTest() throws InterruptedExcept
193190

194191
assertThat(set, hasItems(payload));
195192
}
193+
194+
private static boolean isClusterAware(RedisConnectionFactory connectionFactory) {
195+
196+
if (connectionFactory instanceof LettuceConnectionFactory) {
197+
return ((LettuceConnectionFactory) connectionFactory).isClusterAware();
198+
} else if (connectionFactory instanceof JedisConnectionFactory) {
199+
return ((JedisConnectionFactory) connectionFactory).isRedisClusterAware();
200+
}
201+
return false;
202+
}
196203
}

0 commit comments

Comments
 (0)