Search in sources :

Example 1 with ReactiveRedisConnection

use of org.springframework.data.redis.connection.ReactiveRedisConnection in project redisson by redisson.

the class RedissonSubscribeReactiveTest method testPubSub.

@Test
public void testPubSub() {
    RedissonConnectionFactory factory = new RedissonConnectionFactory(redisson);
    AtomicLong counter = new AtomicLong();
    ReactiveStringRedisTemplate template = new ReactiveStringRedisTemplate(factory);
    template.listenTo(ChannelTopic.of("test")).flatMap(message -> {
        counter.incrementAndGet();
        return Mono.empty();
    }).subscribe();
    for (int i = 0; i < 40; i++) {
        ReactiveRedisConnection connection = factory.getReactiveConnection();
        connection.pubSubCommands().publish(ByteBuffer.wrap("test".getBytes()), ByteBuffer.wrap("msg".getBytes())).block();
    }
    Awaitility.await().atMost(Duration.ONE_SECOND).untilAsserted(() -> {
        assertThat(counter.get()).isEqualTo(40);
    });
}
Also used : Arrays(java.util.Arrays) Duration(org.awaitility.Duration) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Test(org.junit.Test) Mono(reactor.core.publisher.Mono) AtomicReference(java.util.concurrent.atomic.AtomicReference) ByteBuffer(java.nio.ByteBuffer) ReactiveStringRedisTemplate(org.springframework.data.redis.core.ReactiveStringRedisTemplate) AtomicLong(java.util.concurrent.atomic.AtomicLong) ChannelTopic(org.springframework.data.redis.listener.ChannelTopic) ReactiveSubscription(org.springframework.data.redis.connection.ReactiveSubscription) ReactiveRedisConnection(org.springframework.data.redis.connection.ReactiveRedisConnection) Awaitility(org.awaitility.Awaitility) AtomicLong(java.util.concurrent.atomic.AtomicLong) ReactiveRedisConnection(org.springframework.data.redis.connection.ReactiveRedisConnection) ReactiveStringRedisTemplate(org.springframework.data.redis.core.ReactiveStringRedisTemplate) Test(org.junit.Test)

Example 2 with ReactiveRedisConnection

use of org.springframework.data.redis.connection.ReactiveRedisConnection in project redisson by redisson.

the class RedissonSubscribeReactiveTest method testUnSubscribe.

@Test
public void testUnSubscribe() {
    RedissonConnectionFactory factory = new RedissonConnectionFactory(redisson);
    ReactiveRedisConnection connection = factory.getReactiveConnection();
    Mono<ReactiveSubscription> s = connection.pubSubCommands().createSubscription();
    AtomicReference<byte[]> msg = new AtomicReference<byte[]>();
    ReactiveSubscription ss = s.block();
    ss.subscribe(ByteBuffer.wrap("test".getBytes())).block();
    ss.receive().doOnEach(message -> {
        msg.set(message.get().getMessage().array());
    }).subscribe();
    connection.pubSubCommands().publish(ByteBuffer.wrap("test".getBytes()), ByteBuffer.wrap("msg".getBytes())).block();
    Awaitility.await().atMost(Duration.ONE_SECOND).until(() -> Arrays.equals("msg".getBytes(), msg.get()));
    ss.unsubscribe();
    connection.pubSubCommands().publish(ByteBuffer.wrap("test".getBytes()), ByteBuffer.wrap("msg".getBytes())).block();
}
Also used : Arrays(java.util.Arrays) Duration(org.awaitility.Duration) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Test(org.junit.Test) Mono(reactor.core.publisher.Mono) AtomicReference(java.util.concurrent.atomic.AtomicReference) ByteBuffer(java.nio.ByteBuffer) ReactiveStringRedisTemplate(org.springframework.data.redis.core.ReactiveStringRedisTemplate) AtomicLong(java.util.concurrent.atomic.AtomicLong) ChannelTopic(org.springframework.data.redis.listener.ChannelTopic) ReactiveSubscription(org.springframework.data.redis.connection.ReactiveSubscription) ReactiveRedisConnection(org.springframework.data.redis.connection.ReactiveRedisConnection) Awaitility(org.awaitility.Awaitility) ReactiveRedisConnection(org.springframework.data.redis.connection.ReactiveRedisConnection) ReactiveSubscription(org.springframework.data.redis.connection.ReactiveSubscription) AtomicReference(java.util.concurrent.atomic.AtomicReference) Test(org.junit.Test)

Example 3 with ReactiveRedisConnection

use of org.springframework.data.redis.connection.ReactiveRedisConnection in project spring-boot by spring-projects.

the class RedisReactiveHealthIndicatorTests method redisCommandIsDown.

@Test
void redisCommandIsDown() {
    ReactiveServerCommands commands = mock(ReactiveServerCommands.class);
    given(commands.info("server")).willReturn(Mono.error(new RedisConnectionFailureException("Connection failed")));
    ReactiveRedisConnection redisConnection = mock(ReactiveRedisConnection.class);
    given(redisConnection.closeLater()).willReturn(Mono.empty());
    RedisReactiveHealthIndicator healthIndicator = createHealthIndicator(redisConnection, commands);
    Mono<Health> health = healthIndicator.health();
    StepVerifier.create(health).consumeNextWith((h) -> assertThat(h.getStatus()).isEqualTo(Status.DOWN)).verifyComplete();
    then(redisConnection).should().closeLater();
}
Also used : Status(org.springframework.boot.actuate.health.Status) Properties(java.util.Properties) StepVerifier(reactor.test.StepVerifier) RedisConnectionFailureException(org.springframework.data.redis.RedisConnectionFailureException) ReactiveServerCommands(org.springframework.data.redis.connection.ReactiveServerCommands) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ReactiveRedisClusterConnection(org.springframework.data.redis.connection.ReactiveRedisClusterConnection) BDDMockito.then(org.mockito.BDDMockito.then) Mono(reactor.core.publisher.Mono) Health(org.springframework.boot.actuate.health.Health) Test(org.junit.jupiter.api.Test) RedisConnectionException(io.lettuce.core.RedisConnectionException) BDDMockito.given(org.mockito.BDDMockito.given) ReactiveRedisConnection(org.springframework.data.redis.connection.ReactiveRedisConnection) ReactiveRedisConnectionFactory(org.springframework.data.redis.connection.ReactiveRedisConnectionFactory) ClusterInfo(org.springframework.data.redis.connection.ClusterInfo) Mockito.mock(org.mockito.Mockito.mock) Health(org.springframework.boot.actuate.health.Health) ReactiveRedisConnection(org.springframework.data.redis.connection.ReactiveRedisConnection) RedisConnectionFailureException(org.springframework.data.redis.RedisConnectionFailureException) ReactiveServerCommands(org.springframework.data.redis.connection.ReactiveServerCommands) Test(org.junit.jupiter.api.Test)

Example 4 with ReactiveRedisConnection

use of org.springframework.data.redis.connection.ReactiveRedisConnection in project spring-boot by spring-projects.

the class RedisReactiveHealthIndicatorTests method redisIsUp.

@Test
void redisIsUp() {
    Properties info = new Properties();
    info.put("redis_version", "2.8.9");
    ReactiveRedisConnection redisConnection = mock(ReactiveRedisConnection.class);
    given(redisConnection.closeLater()).willReturn(Mono.empty());
    ReactiveServerCommands commands = mock(ReactiveServerCommands.class);
    given(commands.info("server")).willReturn(Mono.just(info));
    RedisReactiveHealthIndicator healthIndicator = createHealthIndicator(redisConnection, commands);
    Mono<Health> health = healthIndicator.health();
    StepVerifier.create(health).consumeNextWith((h) -> {
        assertThat(h.getStatus()).isEqualTo(Status.UP);
        assertThat(h.getDetails()).containsOnlyKeys("version");
        assertThat(h.getDetails().get("version")).isEqualTo("2.8.9");
    }).verifyComplete();
    then(redisConnection).should().closeLater();
}
Also used : Status(org.springframework.boot.actuate.health.Status) Properties(java.util.Properties) StepVerifier(reactor.test.StepVerifier) RedisConnectionFailureException(org.springframework.data.redis.RedisConnectionFailureException) ReactiveServerCommands(org.springframework.data.redis.connection.ReactiveServerCommands) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ReactiveRedisClusterConnection(org.springframework.data.redis.connection.ReactiveRedisClusterConnection) BDDMockito.then(org.mockito.BDDMockito.then) Mono(reactor.core.publisher.Mono) Health(org.springframework.boot.actuate.health.Health) Test(org.junit.jupiter.api.Test) RedisConnectionException(io.lettuce.core.RedisConnectionException) BDDMockito.given(org.mockito.BDDMockito.given) ReactiveRedisConnection(org.springframework.data.redis.connection.ReactiveRedisConnection) ReactiveRedisConnectionFactory(org.springframework.data.redis.connection.ReactiveRedisConnectionFactory) ClusterInfo(org.springframework.data.redis.connection.ClusterInfo) Mockito.mock(org.mockito.Mockito.mock) Health(org.springframework.boot.actuate.health.Health) ReactiveRedisConnection(org.springframework.data.redis.connection.ReactiveRedisConnection) Properties(java.util.Properties) ReactiveServerCommands(org.springframework.data.redis.connection.ReactiveServerCommands) Test(org.junit.jupiter.api.Test)

Example 5 with ReactiveRedisConnection

use of org.springframework.data.redis.connection.ReactiveRedisConnection in project redisson by redisson.

the class RedissonSubscribeReactiveTest method testTemplate.

@Test
public void testTemplate() {
    RedissonConnectionFactory factory = new RedissonConnectionFactory(redisson);
    AtomicLong counter = new AtomicLong();
    ReactiveStringRedisTemplate template = new ReactiveStringRedisTemplate(factory);
    template.listenTo(ChannelTopic.of("test")).flatMap(message -> {
        counter.incrementAndGet();
        return Mono.empty();
    }).subscribe();
    template.listenTo(ChannelTopic.of("test2")).flatMap(message -> {
        counter.incrementAndGet();
        return Mono.empty();
    }).subscribe();
    ReactiveRedisConnection connection = factory.getReactiveConnection();
    connection.pubSubCommands().publish(ByteBuffer.wrap("test".getBytes()), ByteBuffer.wrap("msg".getBytes())).block();
    Awaitility.await().atMost(Duration.ONE_SECOND).until(() -> counter.get() == 1);
}
Also used : Arrays(java.util.Arrays) Duration(org.awaitility.Duration) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Test(org.junit.Test) Mono(reactor.core.publisher.Mono) AtomicReference(java.util.concurrent.atomic.AtomicReference) ByteBuffer(java.nio.ByteBuffer) ReactiveStringRedisTemplate(org.springframework.data.redis.core.ReactiveStringRedisTemplate) AtomicLong(java.util.concurrent.atomic.AtomicLong) ChannelTopic(org.springframework.data.redis.listener.ChannelTopic) ReactiveSubscription(org.springframework.data.redis.connection.ReactiveSubscription) ReactiveRedisConnection(org.springframework.data.redis.connection.ReactiveRedisConnection) Awaitility(org.awaitility.Awaitility) AtomicLong(java.util.concurrent.atomic.AtomicLong) ReactiveRedisConnection(org.springframework.data.redis.connection.ReactiveRedisConnection) ReactiveStringRedisTemplate(org.springframework.data.redis.core.ReactiveStringRedisTemplate) Test(org.junit.Test)

Aggregations

Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)6 ReactiveRedisConnection (org.springframework.data.redis.connection.ReactiveRedisConnection)6 Mono (reactor.core.publisher.Mono)6 ByteBuffer (java.nio.ByteBuffer)4 Arrays (java.util.Arrays)4 AtomicLong (java.util.concurrent.atomic.AtomicLong)4 AtomicReference (java.util.concurrent.atomic.AtomicReference)4 Awaitility (org.awaitility.Awaitility)4 Duration (org.awaitility.Duration)4 Test (org.junit.Test)4 ReactiveSubscription (org.springframework.data.redis.connection.ReactiveSubscription)4 ReactiveStringRedisTemplate (org.springframework.data.redis.core.ReactiveStringRedisTemplate)4 ChannelTopic (org.springframework.data.redis.listener.ChannelTopic)4 RedisConnectionException (io.lettuce.core.RedisConnectionException)2 Properties (java.util.Properties)2 Test (org.junit.jupiter.api.Test)2 BDDMockito.given (org.mockito.BDDMockito.given)2 BDDMockito.then (org.mockito.BDDMockito.then)2 Mockito.mock (org.mockito.Mockito.mock)2 Health (org.springframework.boot.actuate.health.Health)2