Search in sources :

Example 1 with AsyncCommand

use of io.lettuce.core.protocol.AsyncCommand in project lettuce-core by lettuce-io.

the class AtLeastOnceTest method commandFailsWhenFailOnEncode.

@Test
void commandFailsWhenFailOnEncode() {
    RedisCommands<String, String> connection = client.connect().sync();
    RedisChannelWriter channelWriter = ConnectionTestUtil.getChannelWriter(connection.getStatefulConnection());
    RedisCommands<String, String> verificationConnection = client.connect().sync();
    connection.set(key, "1");
    AsyncCommand<String, String, String> working = new AsyncCommand<>(new Command<>(CommandType.INCR, new IntegerOutput(StringCodec.UTF8), new CommandArgs<>(StringCodec.UTF8).addKey(key)));
    channelWriter.write(working);
    assertThat(working.await(2, TimeUnit.SECONDS)).isTrue();
    assertThat(connection.get(key)).isEqualTo("2");
    AsyncCommand<String, String, Object> command = new AsyncCommand(new Command<>(CommandType.INCR, new IntegerOutput(StringCodec.UTF8), new CommandArgs<>(StringCodec.UTF8).addKey(key))) {

        @Override
        public void encode(ByteBuf buf) {
            throw new IllegalStateException("I want to break free");
        }
    };
    channelWriter.write(command);
    assertThat(command.await(2, TimeUnit.SECONDS)).isTrue();
    assertThat(command.isCancelled()).isFalse();
    assertThat(getException(command)).isInstanceOf(EncoderException.class);
    assertThat(verificationConnection.get(key)).isEqualTo("2");
    assertThat(ConnectionTestUtil.getStack(connection.getStatefulConnection())).isNotEmpty();
    connection.getStatefulConnection().close();
}
Also used : AsyncCommand(io.lettuce.core.protocol.AsyncCommand) IntegerOutput(io.lettuce.core.output.IntegerOutput) RedisChannelWriter(io.lettuce.core.RedisChannelWriter) ByteBuf(io.netty.buffer.ByteBuf) AbstractRedisClientTest(io.lettuce.core.AbstractRedisClientTest) Test(org.junit.jupiter.api.Test)

Example 2 with AsyncCommand

use of io.lettuce.core.protocol.AsyncCommand in project lettuce-core by lettuce-io.

the class SentinelTopologyRefreshUnitTests method bindWithSentinelRecovery.

@Test
void bindWithSentinelRecovery() {
    StatefulRedisPubSubConnection<String, String> connection2 = mock(StatefulRedisPubSubConnection.class);
    RedisPubSubAsyncCommands<String, String> async2 = mock(RedisPubSubAsyncCommands.class);
    when(connection2.async()).thenReturn(async2);
    AsyncCommand<String, String, Void> command = new AsyncCommand<>(new Command<>(CommandType.PSUBSCRIBE, null));
    command.complete();
    when(async2.psubscribe(anyString())).thenReturn(command);
    sut = new SentinelTopologyRefresh(redisClient, "mymaster", Arrays.asList(host1, host2));
    when(redisClient.connectPubSubAsync(any(StringCodec.class), eq(host2))).thenReturn(ConnectionFuture.from(null, Futures.failed(new RedisConnectionException("err")))).thenReturn(ConnectionFuture.completed(null, connection2));
    sut.bind(refreshRunnable);
    verify(redisClient).connectPubSubAsync(any(), eq(host1));
    verify(redisClient).connectPubSubAsync(any(), eq(host2));
    Map<RedisURI, StatefulRedisPubSubConnection<String, String>> connections = (Map) ReflectionTestUtils.getField(sut, "pubSubConnections");
    PubSubMessageHandler adapter = getMessageHandler();
    adapter.handle("*", "+sentinel", "sentinel c14cc895bb0479c91312cee0e0440b7d99ad367b 127.0.0.1 26380 @ mymaster 127.0.0.1 6483");
    verify(eventExecutors, times(1)).schedule(captor.capture(), anyLong(), any());
    captor.getValue().run();
    verify(redisClient, times(2)).connectPubSubAsync(any(), eq(host2));
    assertThat(connections).containsKey(host1).containsKey(host2).hasSize(2);
    verify(refreshRunnable, never()).run();
}
Also used : RedisURI(io.lettuce.core.RedisURI) AsyncCommand(io.lettuce.core.protocol.AsyncCommand) PubSubMessageHandler(io.lettuce.core.masterreplica.SentinelTopologyRefresh.PubSubMessageHandler) Mockito.anyString(org.mockito.Mockito.anyString) RedisConnectionException(io.lettuce.core.RedisConnectionException) Map(java.util.Map) StatefulRedisPubSubConnection(io.lettuce.core.pubsub.StatefulRedisPubSubConnection) Test(org.junit.jupiter.api.Test)

Example 3 with AsyncCommand

use of io.lettuce.core.protocol.AsyncCommand in project lettuce-core by lettuce-io.

the class AtMostOnceTest method commandFailsDuringDecode.

@Test
void commandFailsDuringDecode() {
    StatefulRedisConnection<String, String> connection = client.connect();
    RedisCommands<String, String> sync = connection.sync();
    RedisChannelWriter channelWriter = ConnectionTestUtil.getChannelWriter(connection);
    RedisCommands<String, String> verificationConnection = client.connect().sync();
    sync.set(key, "1");
    AsyncCommand<String, String, String> command = new AsyncCommand<>(new Command<>(CommandType.INCR, new StatusOutput<>(StringCodec.UTF8), new CommandArgs<>(StringCodec.UTF8).addKey(key)));
    channelWriter.write(command);
    assertThat(command.await(2, TimeUnit.SECONDS)).isTrue();
    assertThat(command.isCancelled()).isFalse();
    assertThat(getException(command)).isInstanceOf(UnsupportedOperationException.class);
    assertThat(verificationConnection.get(key)).isEqualTo("2");
    assertThat(sync.get(key)).isEqualTo("2");
    connection.close();
}
Also used : AsyncCommand(io.lettuce.core.protocol.AsyncCommand) RedisChannelWriter(io.lettuce.core.RedisChannelWriter) StatusOutput(io.lettuce.core.output.StatusOutput) AbstractRedisClientTest(io.lettuce.core.AbstractRedisClientTest) Test(org.junit.jupiter.api.Test)

Example 4 with AsyncCommand

use of io.lettuce.core.protocol.AsyncCommand in project lettuce-core by lettuce-io.

the class CustomCommandIntegrationTests method standaloneAsyncBatchTransaction.

@Test
void standaloneAsyncBatchTransaction() {
    RedisCommand<String, String, String> multi = new Command<>(CommandType.MULTI, new StatusOutput<>(StringCodec.UTF8));
    RedisCommand<String, String, String> set = new Command<>(CommandType.SET, new StatusOutput<>(StringCodec.UTF8), new CommandArgs<>(StringCodec.UTF8).addKey("key").add("value"));
    RedisCommand<String, String, TransactionResult> exec = new Command<>(CommandType.EXEC, null);
    AsyncCommand<String, String, String> async1 = new AsyncCommand<>(multi);
    AsyncCommand<String, String, String> async2 = new AsyncCommand<>(set);
    AsyncCommand<String, String, TransactionResult> async3 = new AsyncCommand<>(exec);
    getStandaloneConnection().dispatch(Arrays.asList(async1, async2, async3));
    assertThat(TestFutures.getOrTimeout(async1.toCompletableFuture())).isEqualTo("OK");
    assertThat(TestFutures.getOrTimeout(async2.toCompletableFuture())).isEqualTo("OK");
    TransactionResult transactionResult = TestFutures.getOrTimeout(async3.toCompletableFuture());
    assertThat(transactionResult.wasDiscarded()).isFalse();
    assertThat(transactionResult.<String>get(0)).isEqualTo("OK");
}
Also used : TransactionResult(io.lettuce.core.TransactionResult) Command(io.lettuce.core.protocol.Command) AsyncCommand(io.lettuce.core.protocol.AsyncCommand) RedisCommand(io.lettuce.core.protocol.RedisCommand) AsyncCommand(io.lettuce.core.protocol.AsyncCommand) Test(org.junit.jupiter.api.Test)

Example 5 with AsyncCommand

use of io.lettuce.core.protocol.AsyncCommand in project lettuce-core by lettuce-io.

the class AsyncExecutableCommand method dispatchCommand.

protected Object dispatchCommand(Object[] arguments, RedisCommand<Object, Object, Object> command) throws InterruptedException, java.util.concurrent.ExecutionException {
    AsyncCommand<Object, Object, Object> asyncCommand = new AsyncCommand<>(command);
    if (commandMethod.isFutureExecution()) {
        RedisCommand<Object, Object, Object> dispatched = connection.dispatch(asyncCommand);
        if (dispatched instanceof AsyncCommand) {
            return dispatched;
        }
        return asyncCommand;
    }
    connection.dispatch(asyncCommand);
    Duration timeout = connection.getTimeout();
    if (commandMethod.getParameters() instanceof ExecutionSpecificParameters) {
        ExecutionSpecificParameters executionSpecificParameters = (ExecutionSpecificParameters) commandMethod.getParameters();
        if (executionSpecificParameters.hasTimeoutIndex()) {
            Timeout timeoutArg = (Timeout) arguments[executionSpecificParameters.getTimeoutIndex()];
            if (timeoutArg != null) {
                timeout = timeoutArg.getTimeout();
            }
        }
    }
    Futures.await(timeout, asyncCommand);
    return asyncCommand.get();
}
Also used : Timeout(io.lettuce.core.dynamic.domain.Timeout) AsyncCommand(io.lettuce.core.protocol.AsyncCommand) Duration(java.time.Duration) ExecutionSpecificParameters(io.lettuce.core.dynamic.parameter.ExecutionSpecificParameters)

Aggregations

AsyncCommand (io.lettuce.core.protocol.AsyncCommand)12 Test (org.junit.jupiter.api.Test)10 AbstractRedisClientTest (io.lettuce.core.AbstractRedisClientTest)5 RedisChannelWriter (io.lettuce.core.RedisChannelWriter)5 IntegerOutput (io.lettuce.core.output.IntegerOutput)3 ByteBuf (io.netty.buffer.ByteBuf)3 RedisClusterNode (io.lettuce.core.cluster.models.partitions.RedisClusterNode)2 StatusOutput (io.lettuce.core.output.StatusOutput)2 Command (io.lettuce.core.protocol.Command)2 Tag (brave.Tag)1 Tracer (brave.Tracer)1 Tracing (brave.Tracing)1 MutableSpan (brave.handler.MutableSpan)1 CurrentTraceContext (brave.propagation.CurrentTraceContext)1 RedisConnectionException (io.lettuce.core.RedisConnectionException)1 RedisURI (io.lettuce.core.RedisURI)1 TestSupport (io.lettuce.core.TestSupport)1 TransactionResult (io.lettuce.core.TransactionResult)1 Partitions (io.lettuce.core.cluster.models.partitions.Partitions)1 CommandBatching (io.lettuce.core.dynamic.batch.CommandBatching)1