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();
}
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();
}
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();
}
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");
}
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();
}
Aggregations