Search in sources :

Example 1 with ValueListOutput

use of io.lettuce.core.output.ValueListOutput in project lettuce-core by lettuce-io.

the class ReactiveBackpressurePropagationUnitTests method writeCommandAndCancelInTheMiddle.

@Test
void writeCommandAndCancelInTheMiddle() throws Exception {
    Command<String, String, List<String>> lrange = new Command<>(CommandType.LRANGE, new ValueListOutput<>(StringCodec.UTF8));
    RedisPublisher<String, String, String> publisher = new RedisPublisher<>(lrange, statefulConnection, true, ImmediateEventExecutor.INSTANCE);
    CountDownLatch pressureArrived = new CountDownLatch(1);
    CountDownLatch buildPressure = new CountDownLatch(1);
    CountDownLatch waitForPressureReduced = new CountDownLatch(2);
    Disposable cancellation = Flux.from(publisher).limitRate(2).publishOn(Schedulers.single()).doOnNext(s -> {
        try {
            pressureArrived.countDown();
            buildPressure.await();
        } catch (InterruptedException e) {
        }
        waitForPressureReduced.countDown();
    }).subscribe();
    assertThat(embeddedChannel.config().isAutoRead()).isTrue();
    // produce some back pressure
    embeddedChannel.writeInbound(Unpooled.wrappedBuffer(RESP.arrayHeader(4)));
    embeddedChannel.writeInbound(Unpooled.wrappedBuffer(RESP.bulkString("one")));
    pressureArrived.await();
    assertThat(embeddedChannel.config().isAutoRead()).isTrue();
    embeddedChannel.writeInbound(Unpooled.wrappedBuffer(RESP.bulkString("two")));
    embeddedChannel.writeInbound(Unpooled.wrappedBuffer(RESP.bulkString("three")));
    assertThat(embeddedChannel.config().isAutoRead()).isFalse();
    cancellation.dispose();
    assertThat(embeddedChannel.config().isAutoRead()).isTrue();
    // allow processing
    buildPressure.countDown();
    // emit the last item
    embeddedChannel.writeInbound(Unpooled.wrappedBuffer(RESP.bulkString("four")));
    // done
    assertThat(embeddedChannel.config().isAutoRead()).isTrue();
}
Also used : Disposable(reactor.core.Disposable) ClientResources(io.lettuce.core.resource.ClientResources) BeforeEach(org.junit.jupiter.api.BeforeEach) ArgumentMatchers(org.mockito.ArgumentMatchers) Disposable(reactor.core.Disposable) StringCodec(io.lettuce.core.codec.StringCodec) CommandType(io.lettuce.core.protocol.CommandType) Mock(org.mockito.Mock) CommandLatencyCollector(io.lettuce.core.metrics.CommandLatencyCollector) StatefulConnection(io.lettuce.core.api.StatefulConnection) Command(io.lettuce.core.protocol.Command) Unpooled(io.netty.buffer.Unpooled) LocalAddress(io.netty.channel.local.LocalAddress) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) ImmediateEventExecutor(io.netty.util.concurrent.ImmediateEventExecutor) Assertions(org.assertj.core.api.Assertions) Schedulers(reactor.core.scheduler.Schedulers) Tracing(io.lettuce.core.tracing.Tracing) CommandHandler(io.lettuce.core.protocol.CommandHandler) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) Endpoint(io.lettuce.core.protocol.Endpoint) ValueListOutput(io.lettuce.core.output.ValueListOutput) Test(org.junit.jupiter.api.Test) Mockito(org.mockito.Mockito) CountDownLatch(java.util.concurrent.CountDownLatch) Flux(reactor.core.publisher.Flux) List(java.util.List) RedisCommand(io.lettuce.core.protocol.RedisCommand) Command(io.lettuce.core.protocol.Command) RedisCommand(io.lettuce.core.protocol.RedisCommand) List(java.util.List) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.jupiter.api.Test)

Example 2 with ValueListOutput

use of io.lettuce.core.output.ValueListOutput in project lettuce-core by lettuce-io.

the class ReactiveBackpressurePropagationUnitTests method writeCommand.

@Test
void writeCommand() throws Exception {
    Command<String, String, List<String>> lrange = new Command<>(CommandType.LRANGE, new ValueListOutput<>(StringCodec.UTF8));
    RedisPublisher<String, String, String> publisher = new RedisPublisher<>((Command) lrange, statefulConnection, true, ImmediateEventExecutor.INSTANCE);
    CountDownLatch pressureArrived = new CountDownLatch(1);
    CountDownLatch buildPressure = new CountDownLatch(1);
    CountDownLatch waitForPressureReduced = new CountDownLatch(2);
    CountDownLatch waitForWorkCompleted = new CountDownLatch(4);
    Flux.from(publisher).limitRate(2).publishOn(Schedulers.single()).doOnNext(s -> {
        try {
            pressureArrived.countDown();
            buildPressure.await();
        } catch (InterruptedException e) {
        }
        waitForPressureReduced.countDown();
        waitForWorkCompleted.countDown();
    }).subscribe();
    assertThat(embeddedChannel.config().isAutoRead()).isTrue();
    // produce some back pressure
    embeddedChannel.writeInbound(Unpooled.wrappedBuffer(RESP.arrayHeader(4)));
    embeddedChannel.writeInbound(Unpooled.wrappedBuffer(RESP.bulkString("one")));
    pressureArrived.await();
    assertThat(embeddedChannel.config().isAutoRead()).isTrue();
    embeddedChannel.writeInbound(Unpooled.wrappedBuffer(RESP.bulkString("two")));
    embeddedChannel.writeInbound(Unpooled.wrappedBuffer(RESP.bulkString("three")));
    assertThat(embeddedChannel.config().isAutoRead()).isFalse();
    // allow processing
    buildPressure.countDown();
    // wait until processing caught up
    waitForPressureReduced.await();
    assertThat(embeddedChannel.config().isAutoRead()).isTrue();
    // emit the last item
    embeddedChannel.writeInbound(Unpooled.wrappedBuffer(RESP.bulkString("four")));
    // done
    waitForWorkCompleted.await();
    assertThat(embeddedChannel.config().isAutoRead()).isTrue();
}
Also used : ClientResources(io.lettuce.core.resource.ClientResources) BeforeEach(org.junit.jupiter.api.BeforeEach) ArgumentMatchers(org.mockito.ArgumentMatchers) Disposable(reactor.core.Disposable) StringCodec(io.lettuce.core.codec.StringCodec) CommandType(io.lettuce.core.protocol.CommandType) Mock(org.mockito.Mock) CommandLatencyCollector(io.lettuce.core.metrics.CommandLatencyCollector) StatefulConnection(io.lettuce.core.api.StatefulConnection) Command(io.lettuce.core.protocol.Command) Unpooled(io.netty.buffer.Unpooled) LocalAddress(io.netty.channel.local.LocalAddress) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) ImmediateEventExecutor(io.netty.util.concurrent.ImmediateEventExecutor) Assertions(org.assertj.core.api.Assertions) Schedulers(reactor.core.scheduler.Schedulers) Tracing(io.lettuce.core.tracing.Tracing) CommandHandler(io.lettuce.core.protocol.CommandHandler) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) Endpoint(io.lettuce.core.protocol.Endpoint) ValueListOutput(io.lettuce.core.output.ValueListOutput) Test(org.junit.jupiter.api.Test) Mockito(org.mockito.Mockito) CountDownLatch(java.util.concurrent.CountDownLatch) Flux(reactor.core.publisher.Flux) List(java.util.List) RedisCommand(io.lettuce.core.protocol.RedisCommand) Command(io.lettuce.core.protocol.Command) RedisCommand(io.lettuce.core.protocol.RedisCommand) List(java.util.List) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.jupiter.api.Test)

Aggregations

StatefulConnection (io.lettuce.core.api.StatefulConnection)2 StringCodec (io.lettuce.core.codec.StringCodec)2 CommandLatencyCollector (io.lettuce.core.metrics.CommandLatencyCollector)2 ValueListOutput (io.lettuce.core.output.ValueListOutput)2 Command (io.lettuce.core.protocol.Command)2 CommandHandler (io.lettuce.core.protocol.CommandHandler)2 CommandType (io.lettuce.core.protocol.CommandType)2 Endpoint (io.lettuce.core.protocol.Endpoint)2 RedisCommand (io.lettuce.core.protocol.RedisCommand)2 ClientResources (io.lettuce.core.resource.ClientResources)2 Tracing (io.lettuce.core.tracing.Tracing)2 Unpooled (io.netty.buffer.Unpooled)2 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)2 LocalAddress (io.netty.channel.local.LocalAddress)2 ImmediateEventExecutor (io.netty.util.concurrent.ImmediateEventExecutor)2 List (java.util.List)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 Assertions (org.assertj.core.api.Assertions)2 BeforeEach (org.junit.jupiter.api.BeforeEach)2 Test (org.junit.jupiter.api.Test)2