Skip to content

Replace poll and assert with await for Pub/Sub tests #1978

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>2.6.0-SNAPSHOT</version>
<version>2.6.0-GH-1834-SNAPSHOT</version>

<name>Spring Data Redis</name>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@
*/
package org.springframework.data.redis.core;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

import org.springframework.data.redis.DoubleObjectFactory;
import org.springframework.data.redis.LongObjectFactory;
Expand All @@ -27,6 +29,8 @@
import org.springframework.data.redis.StringObjectFactory;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.jedis.extension.JedisConnectionFactoryExtension;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.connection.lettuce.extension.LettuceConnectionFactoryExtension;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.GenericToStringSerializer;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
Expand All @@ -47,7 +51,11 @@ abstract public class AbstractOperationsTestParams {

// DATAREDIS-241
public static Collection<Object[]> testParams() {
return testParams(JedisConnectionFactoryExtension.getConnectionFactory(RedisStanalone.class));

List<Object[]> params = new ArrayList<>();
params.addAll(testParams(LettuceConnectionFactoryExtension.getConnectionFactory(RedisStanalone.class)));
params.addAll(testParams(JedisConnectionFactoryExtension.getConnectionFactory(RedisStanalone.class)));
return params;
}

// DATAREDIS-241
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,17 @@
*/
package org.springframework.data.redis.listener;

import static org.assertj.core.api.Assertions.*;
import static org.awaitility.Awaitility.*;
import static org.junit.Assume.*;

import java.time.Duration;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.BlockingDeque;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

import org.junit.jupiter.api.AfterEach;
Expand Down Expand Up @@ -156,18 +153,7 @@ void testContainerPatternResubscribe() throws Exception {
template.convertAndSend(CHANNEL, payload1);
template.convertAndSend(ANOTHER_CHANNEL, payload2);

// anotherListener receives both messages
List<String> msgs = new ArrayList<>();
msgs.add(bag2.poll(500, TimeUnit.MILLISECONDS));
msgs.add(bag2.poll(500, TimeUnit.MILLISECONDS));

assertThat(msgs.size()).isEqualTo(2);
assertThat(msgs).contains(payload1);
assertThat(msgs).contains(payload2);
msgs.clear();

// unsubscribed adapter did not receive message
assertThat(bag.poll(500, TimeUnit.MILLISECONDS)).isNull();
await().atMost(Duration.ofSeconds(2)).until(() -> bag2.contains(payload1) && bag2.contains(payload2));

// bind original listener on another channel
container.addMessageListener(adapter, new ChannelTopic(ANOTHER_CHANNEL));
Expand All @@ -178,21 +164,10 @@ void testContainerPatternResubscribe() throws Exception {
template.convertAndSend(CHANNEL, payload1);
template.convertAndSend(ANOTHER_CHANNEL, payload2);

// original listener received only one message on another channel
msgs.clear();
msgs.add(bag.poll(500, TimeUnit.MILLISECONDS));
msgs.add(bag.poll(500, TimeUnit.MILLISECONDS));

assertThat(msgs).contains(payload2);
assertThat(msgs.contains(null)).isTrue();
await().atMost(Duration.ofSeconds(2)).until(() -> bag.contains(payload2));

// another listener receives messages on both channels
msgs.clear();
msgs.add(bag2.poll(500, TimeUnit.MILLISECONDS));
msgs.add(bag2.poll(500, TimeUnit.MILLISECONDS));
assertThat(msgs.size()).isEqualTo(2);
assertThat(msgs).contains(payload1);
assertThat(msgs).contains(payload2);
await().atMost(Duration.ofSeconds(2)).until(() -> bag2.contains(payload1) && bag2.contains(payload2));
}

@ParameterizedRedisTest
Expand Down Expand Up @@ -222,15 +197,7 @@ void testContainerChannelResubscribe() throws Exception {
template.convertAndSend(ANOTHER_CHANNEL, anotherPayload1);
template.convertAndSend(ANOTHER_CHANNEL, anotherPayload2);

Set<String> set = new LinkedHashSet<>();
set.add(bag.poll(500, TimeUnit.MILLISECONDS));
set.add(bag.poll(500, TimeUnit.MILLISECONDS));

assertThat(set.contains(payload1)).isFalse();
assertThat(set.contains(payload2)).isFalse();

assertThat(set.contains(anotherPayload1)).isTrue();
assertThat(set.contains(anotherPayload2)).isTrue();
await().atMost(Duration.ofSeconds(2)).until(() -> bag.contains(anotherPayload1) && bag.contains(anotherPayload2));
}

/**
Expand All @@ -246,8 +213,8 @@ void testInitializeContainerWithMultipleTopicsIncludingPattern() throws Exceptio

container.stop();

String uniqueChannel = "random-" + UUID.randomUUID().toString();
PubSubAwaitUtil.runAndAwaitChannelSubscription(template.getConnectionFactory(), uniqueChannel, () -> {
String uniqueChannel = "random-" + UUID.randomUUID();
PubSubAwaitUtil.runAndAwaitPatternSubscription(template.getConnectionFactory(), () -> {

container.addMessageListener(adapter,
Arrays.asList(new Topic[] { new ChannelTopic(uniqueChannel), new PatternTopic("s*") }));
Expand All @@ -256,16 +223,12 @@ void testInitializeContainerWithMultipleTopicsIncludingPattern() throws Exceptio

// timing: There's currently no other way to synchronize
// than to hope the subscribe/unsubscribe are executed within the time.
Thread.sleep(50);
Thread.sleep(250);

template.convertAndSend("somechannel", "HELLO");
template.convertAndSend(uniqueChannel, "WORLD");

Set<String> set = new LinkedHashSet<>();
set.add(bag.poll(500, TimeUnit.MILLISECONDS));
set.add(bag.poll(500, TimeUnit.MILLISECONDS));

assertThat(set).isEqualTo(new HashSet<>(Arrays.asList(new String[] { "HELLO", "WORLD" })));
await().atMost(Duration.ofSeconds(2)).until(() -> bag.contains("HELLO") && bag.contains("WORLD"));
}

private class MessageHandler {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import org.springframework.data.redis.ObjectFactory;
import org.springframework.data.redis.Person;
import org.springframework.data.redis.PersonObjectFactory;
import org.springframework.data.redis.RawObjectFactory;
import org.springframework.data.redis.StringObjectFactory;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.connection.jedis.extension.JedisConnectionFactoryExtension;
Expand All @@ -44,7 +43,6 @@ public static Collection<Object[]> testParams() {
// create Jedis Factory
ObjectFactory<String> stringFactory = new StringObjectFactory();
ObjectFactory<Person> personFactory = new PersonObjectFactory();
ObjectFactory<byte[]> rawFactory = new RawObjectFactory();

JedisConnectionFactory jedisConnFactory = JedisConnectionFactoryExtension
.getNewConnectionFactory(RedisStanalone.class);
Expand Down Expand Up @@ -78,7 +76,6 @@ public static Collection<Object[]> testParams() {
parameters.add(new Object[] { personFactory, personTemplate });
parameters.add(new Object[] { stringFactory, stringTemplateLtc });
parameters.add(new Object[] { personFactory, personTemplateLtc });
parameters.add(new Object[] { rawFactory, rawTemplateLtc });

if (clusterAvailable()) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@

import static org.assertj.core.api.Assertions.*;
import static org.assertj.core.api.Assumptions.*;
import static org.awaitility.Awaitility.*;

import java.time.Duration;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.Set;
import java.util.concurrent.BlockingDeque;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.Phaser;
Expand Down Expand Up @@ -108,7 +108,7 @@ protected void doExecute(Runnable task) {
container.start();

phaser.arriveAndAwaitAdvance();
Thread.sleep(50);
Thread.sleep(250);
}

@AfterEach
Expand All @@ -134,11 +134,7 @@ void testContainerSubscribe() throws Exception {
template.convertAndSend(CHANNEL, payload1);
template.convertAndSend(CHANNEL, payload2);

Set<T> set = new LinkedHashSet<>();
set.add((T) bag.poll(1, TimeUnit.SECONDS));
set.add((T) bag.poll(1, TimeUnit.SECONDS));

assertThat(set).contains(payload1, payload2);
await().atMost(Duration.ofSeconds(2)).until(() -> bag.contains(payload1) && bag.contains(payload2));
}

@ParameterizedRedisTest
Expand Down Expand Up @@ -192,10 +188,7 @@ void testStartListenersToNoSpecificChannelTest() throws InterruptedException {

template.convertAndSend(CHANNEL, payload);

Set<T> set = new LinkedHashSet<>();
set.add((T) bag.poll(3, TimeUnit.SECONDS));

assertThat(set).contains(payload);
await().atMost(Duration.ofSeconds(2)).until(() -> bag.contains(payload));
}

private static boolean isClusterAware(RedisConnectionFactory connectionFactory) {
Expand Down