Skip to content

Commit

Permalink
Propagate handshake failures to Handshake future
Browse files Browse the repository at this point in the history
We now complete the handshake future exceptionally if handshake settings do not match our expectations.
This can happen if e.g. the connection id is being sent as String instead of an integer.
  • Loading branch information
mp911de committed Nov 21, 2024
1 parent d616b16 commit d5726a5
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 6 deletions.
8 changes: 6 additions & 2 deletions src/main/java/io/lettuce/core/RedisHandshake.java
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,12 @@ private CompletionStage<?> tryHandshakeResp3(Channel channel) {
handshake.completeExceptionally(throwable);
}
} else {
onHelloResponse(settings);
handshake.complete(null);
try {
onHelloResponse(settings);
handshake.complete(null);
} catch (RuntimeException e) {
handshake.completeExceptionally(e);
}
}
});

Expand Down
41 changes: 37 additions & 4 deletions src/test/java/io/lettuce/core/RedisHandshakeUnitTests.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package io.lettuce.core;

import static io.lettuce.TestTags.UNIT_TEST;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static io.lettuce.TestTags.*;
import static java.util.concurrent.TimeUnit.*;
import static org.assertj.core.api.Assertions.*;

import java.nio.ByteBuffer;
Expand All @@ -13,12 +13,12 @@
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;

import reactor.core.publisher.Mono;
import reactor.core.publisher.Sinks;
import io.lettuce.core.output.CommandOutput;
import io.lettuce.core.protocol.AsyncCommand;
import io.lettuce.core.protocol.ProtocolVersion;
import io.netty.channel.embedded.EmbeddedChannel;
import reactor.core.publisher.Mono;
import reactor.core.publisher.Sinks;

/**
* Unit tests for {@link RedisHandshake}.
Expand Down Expand Up @@ -110,6 +110,23 @@ void handshakeFireAndForgetPostHandshake() {
assertThat(handshakeInit.toCompletableFuture().isCompletedExceptionally()).isFalse();
}

@Test
void handshakeWithInvalidResponseShouldPropagateException() {

EmbeddedChannel channel = new EmbeddedChannel(true, false);

ConnectionState state = new ConnectionState();
state.setCredentialsProvider(new StaticCredentialsProvider(null, null));
RedisHandshake handshake = new RedisHandshake(null, false, state);
CompletionStage<Void> handshakeInit = handshake.initialize(channel);

AsyncCommand<String, String, Map<String, String>> hello = channel.readOutbound();
helloStringIdResponse(hello.getOutput());
hello.complete();

assertThat(handshakeInit.toCompletableFuture().isCompletedExceptionally()).isTrue();
}

@Test
void handshakeDelayedCredentialProvider() {

Expand Down Expand Up @@ -176,6 +193,22 @@ private static void helloResponse(CommandOutput<String, String, Map<String, Stri
output.set(ByteBuffer.wrap("1.2.3".getBytes()));
}

private static void helloStringIdResponse(CommandOutput<String, String, Map<String, String>> output) {

output.multiMap(8);
output.set(ByteBuffer.wrap("id".getBytes()));
output.set(ByteBuffer.wrap("1".getBytes()));

output.set(ByteBuffer.wrap("mode".getBytes()));
output.set(ByteBuffer.wrap("master".getBytes()));

output.set(ByteBuffer.wrap("role".getBytes()));
output.set(ByteBuffer.wrap("master".getBytes()));

output.set(ByteBuffer.wrap("version".getBytes()));
output.set(ByteBuffer.wrap("1.2.3".getBytes()));
}

static class DelayedRedisCredentialsProvider implements RedisCredentialsProvider {

private final Sinks.One<RedisCredentials> credentialsSink = Sinks.one();
Expand Down

0 comments on commit d5726a5

Please sign in to comment.