Skip to content

Commit

Permalink
Fix broken unit tests.
Browse files Browse the repository at this point in the history
Closes #2720
  • Loading branch information
mp911de committed Sep 28, 2023
1 parent ff0503c commit 49c6637
Showing 1 changed file with 40 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,24 @@
import io.lettuce.core.XClaimArgs;
import io.lettuce.core.api.StatefulRedisConnection;
import io.lettuce.core.api.async.RedisAsyncCommands;
import io.lettuce.core.api.sync.RedisCommands;
import io.lettuce.core.codec.ByteArrayCodec;
import io.lettuce.core.codec.RedisCodec;
import io.lettuce.core.codec.StringCodec;
import io.lettuce.core.output.StatusOutput;
import io.lettuce.core.protocol.AsyncCommand;
import io.lettuce.core.protocol.Command;
import io.lettuce.core.protocol.CommandArgs;
import io.lettuce.core.protocol.CommandType;

import java.lang.reflect.InvocationTargetException;
import java.time.Duration;
import java.util.Collections;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;

import org.mockito.Mockito;
import org.springframework.dao.InvalidDataAccessResourceUsageException;
import org.springframework.data.redis.connection.AbstractConnectionUnitTestBase;
import org.springframework.data.redis.connection.RedisServerCommands.ShutdownOption;
Expand All @@ -61,7 +63,6 @@ public static class BasicUnitTests extends AbstractConnectionUnitTestBase<RedisA
private RedisClient clientMock;
StatefulRedisConnection<byte[], byte[]> statefulConnectionMock;
RedisAsyncCommands<byte[], byte[]> asyncCommandsMock;
RedisCommands syncCommandsMock;

@SuppressWarnings({ "unchecked" })
@BeforeEach
Expand All @@ -71,48 +72,57 @@ public void setUp() throws InvocationTargetException, IllegalAccessException {
statefulConnectionMock = mock(StatefulRedisConnection.class);
when(clientMock.connect((RedisCodec) any())).thenReturn(statefulConnectionMock);

asyncCommandsMock = getNativeRedisConnectionMock();
syncCommandsMock = mock(RedisCommands.class);
asyncCommandsMock = Mockito.mock(RedisAsyncCommands.class, invocation -> {

if (invocation.getMethod().getReturnType().equals(RedisFuture.class)) {

Command<?, ?, ?> cmd = new Command<>(CommandType.PING, new StatusOutput<>(StringCodec.UTF8));
AsyncCommand<?, ?, ?> async = new AsyncCommand<>(cmd);
async.complete();

return async;
}
return null;
});

when(statefulConnectionMock.async()).thenReturn(getNativeRedisConnectionMock());
when(statefulConnectionMock.sync()).thenReturn(syncCommandsMock);
when(statefulConnectionMock.async()).thenReturn(asyncCommandsMock);
connection = new LettuceConnection(0, clientMock);
}

@Test // DATAREDIS-184
public void shutdownWithNullOptionsIsCalledCorrectly() {

connection.shutdown(null);
verify(syncCommandsMock, times(1)).shutdown(true);
verify(asyncCommandsMock).shutdown(true);
}

@Test // DATAREDIS-184
public void shutdownWithNosaveOptionIsCalledCorrectly() {

connection.shutdown(ShutdownOption.NOSAVE);
verify(syncCommandsMock, times(1)).shutdown(false);
verify(asyncCommandsMock).shutdown(false);
}

@Test // DATAREDIS-184
public void shutdownWithSaveOptionIsCalledCorrectly() {

connection.shutdown(ShutdownOption.SAVE);
verify(syncCommandsMock, times(1)).shutdown(true);
verify(asyncCommandsMock).shutdown(true);
}

@Test // DATAREDIS-267
public void killClientShouldDelegateCallCorrectly() {

String ipPort = "127.0.0.1:1001";
connection.killClient("127.0.0.1", 1001);
verify(syncCommandsMock, times(1)).clientKill(eq(ipPort));
verify(asyncCommandsMock).clientKill(eq(ipPort));
}

@Test // DATAREDIS-270
public void getClientNameShouldSendRequestCorrectly() {

connection.getClientName();
verify(syncCommandsMock, times(1)).clientGetname();
verify(asyncCommandsMock).clientGetname();
}

@Test // DATAREDIS-277
Expand All @@ -124,14 +134,14 @@ void replicaOfShouldThrowExectpionWhenCalledForNullHost() {
public void replicaOfShouldBeSentCorrectly() {

connection.replicaOf("127.0.0.1", 1001);
verify(syncCommandsMock, times(1)).slaveof(eq("127.0.0.1"), eq(1001));
verify(asyncCommandsMock).slaveof(eq("127.0.0.1"), eq(1001));
}

@Test // DATAREDIS-277
public void replicaOfNoOneShouldBeSentCorrectly() {

connection.replicaOfNoOne();
verify(syncCommandsMock, times(1)).slaveofNoOne();
verify(asyncCommandsMock).slaveofNoOne();
}

@Test // DATAREDIS-348
Expand All @@ -147,32 +157,31 @@ void dbIndexShouldBeSetWhenObtainingConnection() {
connection.select(1);
connection.getNativeConnection();

verify(syncCommandsMock, times(1)).select(1);
verify(asyncCommandsMock).dispatch(eq(CommandType.SELECT), any(), any());
}

@Test // DATAREDIS-603
void translatesUnknownExceptions() {

IllegalArgumentException exception = new IllegalArgumentException("Aw, snap");

when(syncCommandsMock.set(any(), any())).thenThrow(exception);
when(asyncCommandsMock.set(any(), any())).thenThrow(exception);
connection = new LettuceConnection(null, 0, clientMock, 1);

assertThatThrownBy(() -> connection.set("foo".getBytes(), "bar".getBytes()))
.hasMessageContaining(exception.getMessage()).hasRootCause(exception);
.hasRootCause(exception);
}

@Test // DATAREDIS-603
void translatesPipelineUnknownExceptions() throws Exception {
void translatesPipelineUnknownExceptions() {

IllegalArgumentException exception = new IllegalArgumentException("Aw, snap");

when(asyncCommandsMock.set(any(byte[].class), any(byte[].class))).thenThrow(exception);
connection = new LettuceConnection(null, 0, clientMock, 1);
connection.openPipeline();

assertThatThrownBy(() -> connection.set("foo".getBytes(), "bar".getBytes()))
.hasMessageContaining(exception.getMessage()).hasRootCause(exception);
assertThatThrownBy(() -> connection.set("foo".getBytes(), "bar".getBytes())).hasRootCause(exception);
}

@Test // DATAREDIS-1122
Expand All @@ -182,11 +191,7 @@ void xaddShouldHonorMaxlen() {

connection.streamCommands().xAdd(record, XAddOptions.maxlen(100));
ArgumentCaptor<XAddArgs> args = ArgumentCaptor.forClass(XAddArgs.class);
if (connection.isPipelined()) {
verify(asyncCommandsMock, times(1)).xadd(any(), args.capture(), anyMap());
} else {
verify(syncCommandsMock, times(1)).xadd(any(), args.capture(), anyMap());
}
verify(asyncCommandsMock).xadd(any(), args.capture(), anyMap());

assertThat(args.getValue()).extracting("maxlen").isEqualTo(100L);
}
Expand All @@ -198,11 +203,7 @@ void xClaimShouldNotAddJustIdFlagToArgs() {
XClaimOptions.minIdle(Duration.ofMillis(100)).ids("1-1"));
ArgumentCaptor<XClaimArgs> args = ArgumentCaptor.forClass(XClaimArgs.class);

if (connection.isPipelined()) {
verify(asyncCommandsMock).xclaim(any(), any(), args.capture(), any());
} else {
verify(syncCommandsMock).xclaim(any(), any(), args.capture(), any());
}
verify(asyncCommandsMock).xclaim(any(), any(), args.capture(), any());

assertThat(ReflectionTestUtils.getField(args.getValue(), "justid")).isEqualTo(false);
}
Expand All @@ -214,11 +215,7 @@ void xClaimJustIdShouldAddJustIdFlagToArgs() {
XClaimOptions.minIdle(Duration.ofMillis(100)).ids("1-1"));
ArgumentCaptor<XClaimArgs> args = ArgumentCaptor.forClass(XClaimArgs.class);

if (connection.isPipelined()) {
verify(asyncCommandsMock).xclaim(any(), any(), args.capture(), any());
} else {
verify(syncCommandsMock).xclaim(any(), any(), args.capture(), any());
}
verify(asyncCommandsMock).xclaim(any(), any(), args.capture(), any());

assertThat(ReflectionTestUtils.getField(args.getValue(), "justid")).isEqualTo(true);
}
Expand All @@ -245,11 +242,7 @@ void xaddShouldHonorNoMkStream() {

connection.streamCommands().xAdd(record, XAddOptions.makeNoStream());
ArgumentCaptor<XAddArgs> args = ArgumentCaptor.forClass(XAddArgs.class);
if (connection.isPipelined()) {
verify(asyncCommandsMock, times(1)).xadd(any(), args.capture(), anyMap());
} else {
verify(syncCommandsMock, times(1)).xadd(any(), args.capture(), anyMap());
}
verify(asyncCommandsMock).xadd(any(), args.capture(), anyMap());

assertThat(args.getValue()).extracting("nomkstream").isEqualTo(true);
}
Expand All @@ -266,53 +259,50 @@ public void setUp() throws InvocationTargetException, IllegalAccessException {
}

@Test // DATAREDIS-528
@Disabled("SHUTDOWN not supported in pipeline")
public void shutdownWithSaveOptionIsCalledCorrectly() {

connection.shutdown(ShutdownOption.SAVE);
verify(asyncCommandsMock, times(1)).shutdown(true);
}

@Test // DATAREDIS-528
@Disabled("SHUTDOWN not supported in pipeline")
public void shutdownWithNosaveOptionIsCalledCorrectly() {

connection.shutdown(ShutdownOption.NOSAVE);
verify(asyncCommandsMock, times(1)).shutdown(false);
}

@Test // DATAREDIS-528
public void replicaOfShouldBeSentCorrectly() {

connection.replicaOf("127.0.0.1", 1001);
verify(asyncCommandsMock, times(1)).slaveof(eq("127.0.0.1"), eq(1001));
verify(asyncCommandsMock).slaveof(eq("127.0.0.1"), eq(1001));
}

@Test // DATAREDIS-528
@Disabled("SHUTDOWN not supported in pipeline")
public void shutdownWithNullOptionsIsCalledCorrectly() {

connection.shutdown(null);
verify(asyncCommandsMock, times(1)).shutdown(true);
}

@Test // DATAREDIS-528
public void killClientShouldDelegateCallCorrectly() {

String ipPort = "127.0.0.1:1001";
connection.killClient("127.0.0.1", 1001);
verify(asyncCommandsMock, times(1)).clientKill(eq(ipPort));
verify(asyncCommandsMock).clientKill(eq(ipPort));
}

@Test // DATAREDIS-528
public void replicaOfNoOneShouldBeSentCorrectly() {

connection.replicaOfNoOne();
verify(asyncCommandsMock, times(1)).slaveofNoOne();
verify(asyncCommandsMock).slaveofNoOne();
}

@Test // DATAREDIS-528
public void getClientNameShouldSendRequestCorrectly() {

connection.getClientName();
verify(asyncCommandsMock, times(1)).clientGetname();
verify(asyncCommandsMock).clientGetname();
}
}
}

0 comments on commit 49c6637

Please sign in to comment.