Skip to content

Commit

Permalink
Polishing.
Browse files Browse the repository at this point in the history
Make sure nested LettuceConnectionUnitTests are actually run by junit.

Original Pull Request: #2990
  • Loading branch information
christophstrobl committed Sep 12, 2024
1 parent fd23412 commit ab70114
Showing 1 changed file with 76 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,29 @@
*/
package org.springframework.data.redis.connection.lettuce;

import static org.assertj.core.api.Assertions.*;
import static org.mockito.Mockito.*;

import io.lettuce.core.*;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.Mockito.any;
import static org.mockito.Mockito.anyMap;
import static org.mockito.Mockito.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import io.lettuce.core.KeyScanCursor;
import io.lettuce.core.MapScanCursor;
import io.lettuce.core.RedisClient;
import io.lettuce.core.RedisFuture;
import io.lettuce.core.ScanArgs;
import io.lettuce.core.ScanCursor;
import io.lettuce.core.ScoredValue;
import io.lettuce.core.ScoredValueScanCursor;
import io.lettuce.core.ValueScanCursor;
import io.lettuce.core.XAddArgs;
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;
Expand All @@ -42,6 +61,7 @@

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.Mockito;
Expand All @@ -60,43 +80,44 @@
* @author Christoph Strobl
* @author Mark Paluch
*/
public class LettuceConnectionUnitTests {
class LettuceConnectionUnitTests {

@SuppressWarnings("rawtypes")
public static class BasicUnitTests extends AbstractConnectionUnitTestBase<RedisAsyncCommands> {
protected LettuceConnection connection;
private RedisClient clientMock;
StatefulRedisConnection<byte[], byte[]> statefulConnectionMock;
RedisAsyncCommands<byte[], byte[]> asyncCommandsMock;
RedisCommands<byte[], byte[]> commandsMock;

protected LettuceConnection connection;
private RedisClient clientMock;
StatefulRedisConnection<byte[], byte[]> statefulConnectionMock;
RedisAsyncCommands<byte[], byte[]> asyncCommandsMock;
RedisCommands<byte[], byte[]> commandsMock;
@BeforeEach
@SuppressWarnings({ "rawtypes", "unchecked" })
public void setUp() throws InvocationTargetException, IllegalAccessException {

@SuppressWarnings({ "unchecked" })
@BeforeEach
public void setUp() throws InvocationTargetException, IllegalAccessException {
clientMock = mock(RedisClient.class);
statefulConnectionMock = mock(StatefulRedisConnection.class);
when(clientMock.connect((RedisCodec) any())).thenReturn(statefulConnectionMock);

clientMock = mock(RedisClient.class);
statefulConnectionMock = mock(StatefulRedisConnection.class);
when(clientMock.connect((RedisCodec) any())).thenReturn(statefulConnectionMock);
asyncCommandsMock = Mockito.mock(RedisAsyncCommands.class, invocation -> {

asyncCommandsMock = Mockito.mock(RedisAsyncCommands.class, invocation -> {
if (invocation.getMethod().getReturnType().equals(RedisFuture.class)) {

if (invocation.getMethod().getReturnType().equals(RedisFuture.class)) {
Command<?, ?, ?> cmd = new Command<>(CommandType.PING, new StatusOutput<>(StringCodec.UTF8));
AsyncCommand<?, ?, ?> async = new AsyncCommand<>(cmd);
async.complete();

Command<?, ?, ?> cmd = new Command<>(CommandType.PING, new StatusOutput<>(StringCodec.UTF8));
AsyncCommand<?, ?, ?> async = new AsyncCommand<>(cmd);
async.complete();
return async;
}
return null;
});
commandsMock = Mockito.mock(RedisCommands.class);

return async;
}
return null;
});
commandsMock = Mockito.mock(RedisCommands.class);
when(statefulConnectionMock.async()).thenReturn(asyncCommandsMock);
when(statefulConnectionMock.sync()).thenReturn(commandsMock);
connection = new LettuceConnection(0, clientMock);
}

when(statefulConnectionMock.async()).thenReturn(asyncCommandsMock);
when(statefulConnectionMock.sync()).thenReturn(commandsMock);
connection = new LettuceConnection(0, clientMock);
}
@Nested
@SuppressWarnings({ "rawtypes", "deprecation" })
class BasicUnitTests extends AbstractConnectionUnitTestBase<RedisAsyncCommands> {

@Test // DATAREDIS-184
public void shutdownWithNullOptionsIsCalledCorrectly() {
Expand Down Expand Up @@ -178,8 +199,7 @@ void translatesUnknownExceptions() {
when(asyncCommandsMock.set(any(), any())).thenThrow(exception);
connection = new LettuceConnection(null, 0, clientMock, 1);

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

@Test // DATAREDIS-603
Expand Down Expand Up @@ -270,8 +290,8 @@ public List<byte[]> getKeys() {
sc.setCursor(cursorId);
sc.setFinished(false);

Command<byte[], byte[], KeyScanCursor<byte[]>> command = new Command<>(new LettuceConnection.CustomCommandType("SCAN"),
new ScanOutput<>(ByteArrayCodec.INSTANCE, sc) {
Command<byte[], byte[], KeyScanCursor<byte[]>> command = new Command<>(
new LettuceConnection.CustomCommandType("SCAN"), new ScanOutput<>(ByteArrayCodec.INSTANCE, sc) {
@Override
protected void setOutput(ByteBuffer bytes) {

Expand All @@ -280,10 +300,10 @@ protected void setOutput(ByteBuffer bytes) {
AsyncCommand<byte[], byte[], KeyScanCursor<byte[]>> future = new AsyncCommand<>(command);
future.complete();

when(asyncCommandsMock.scan(any(ScanCursor.class),any(ScanArgs.class))).thenReturn(future, future);
when(asyncCommandsMock.scan(any(ScanCursor.class), any(ScanArgs.class))).thenReturn(future, future);

Cursor<byte[]> cursor = connection.scan(KeyScanOptions.NONE);
cursor.next(); //initial
cursor.next(); // initial
assertThat(cursor.getCursorId()).isEqualTo(Long.parseUnsignedLong(cursorId));

cursor.next(); // fetch next
Expand All @@ -305,8 +325,8 @@ public List<byte[]> getValues() {
sc.setCursor(cursorId);
sc.setFinished(false);

Command<byte[], byte[], ValueScanCursor<byte[]>> command = new Command<>(new LettuceConnection.CustomCommandType("SSCAN"),
new ScanOutput<>(ByteArrayCodec.INSTANCE, sc) {
Command<byte[], byte[], ValueScanCursor<byte[]>> command = new Command<>(
new LettuceConnection.CustomCommandType("SSCAN"), new ScanOutput<>(ByteArrayCodec.INSTANCE, sc) {
@Override
protected void setOutput(ByteBuffer bytes) {

Expand All @@ -315,10 +335,11 @@ protected void setOutput(ByteBuffer bytes) {
AsyncCommand<byte[], byte[], ValueScanCursor<byte[]>> future = new AsyncCommand<>(command);
future.complete();

when(asyncCommandsMock.sscan(any(byte[].class), any(ScanCursor.class),any(ScanArgs.class))).thenReturn(future, future);
when(asyncCommandsMock.sscan(any(byte[].class), any(ScanCursor.class), any(ScanArgs.class))).thenReturn(future,
future);

Cursor<byte[]> cursor = connection.setCommands().sScan("key".getBytes(), KeyScanOptions.NONE);
cursor.next(); //initial
cursor.next(); // initial
assertThat(cursor.getCursorId()).isEqualTo(Long.parseUnsignedLong(cursorId));

cursor.next(); // fetch next
Expand All @@ -340,8 +361,8 @@ public List<ScoredValue<byte[]>> getValues() {
sc.setCursor(cursorId);
sc.setFinished(false);

Command<byte[], byte[], ScoredValueScanCursor<byte[]>> command = new Command<>(new LettuceConnection.CustomCommandType("ZSCAN"),
new ScanOutput<>(ByteArrayCodec.INSTANCE, sc) {
Command<byte[], byte[], ScoredValueScanCursor<byte[]>> command = new Command<>(
new LettuceConnection.CustomCommandType("ZSCAN"), new ScanOutput<>(ByteArrayCodec.INSTANCE, sc) {
@Override
protected void setOutput(ByteBuffer bytes) {

Expand All @@ -350,10 +371,11 @@ protected void setOutput(ByteBuffer bytes) {
AsyncCommand<byte[], byte[], ScoredValueScanCursor<byte[]>> future = new AsyncCommand<>(command);
future.complete();

when(asyncCommandsMock.zscan(any(byte[].class), any(ScanCursor.class),any(ScanArgs.class))).thenReturn(future, future);
when(asyncCommandsMock.zscan(any(byte[].class), any(ScanCursor.class), any(ScanArgs.class))).thenReturn(future,
future);

Cursor<Tuple> cursor = connection.zSetCommands().zScan("key".getBytes(), KeyScanOptions.NONE);
cursor.next(); //initial
cursor.next(); // initial
assertThat(cursor.getCursorId()).isEqualTo(Long.parseUnsignedLong(cursorId));

cursor.next(); // fetch next
Expand All @@ -375,8 +397,8 @@ public Map<byte[], byte[]> getMap() {
sc.setCursor(cursorId);
sc.setFinished(false);

Command<byte[], byte[], MapScanCursor<byte[], byte[]>> command = new Command<>(new LettuceConnection.CustomCommandType("HSCAN"),
new ScanOutput<>(ByteArrayCodec.INSTANCE, sc) {
Command<byte[], byte[], MapScanCursor<byte[], byte[]>> command = new Command<>(
new LettuceConnection.CustomCommandType("HSCAN"), new ScanOutput<>(ByteArrayCodec.INSTANCE, sc) {
@Override
protected void setOutput(ByteBuffer bytes) {

Expand All @@ -385,10 +407,11 @@ protected void setOutput(ByteBuffer bytes) {
AsyncCommand<byte[], byte[], MapScanCursor<byte[], byte[]>> future = new AsyncCommand<>(command);
future.complete();

when(asyncCommandsMock.hscan(any(byte[].class), any(ScanCursor.class),any(ScanArgs.class))).thenReturn(future, future);
when(asyncCommandsMock.hscan(any(byte[].class), any(ScanCursor.class), any(ScanArgs.class))).thenReturn(future,
future);

Cursor<Entry<byte[], byte[]>> cursor = connection.hashCommands().hScan("key".getBytes(), KeyScanOptions.NONE);
cursor.next(); //initial
cursor.next(); // initial
assertThat(cursor.getCursorId()).isEqualTo(Long.parseUnsignedLong(cursorId));

cursor.next(); // fetch next
Expand All @@ -399,13 +422,12 @@ protected void setOutput(ByteBuffer bytes) {

}

public static class LettucePipelineConnectionUnitTests extends BasicUnitTests {
@Nested
class LettucePipelineConnectionUnitTests extends BasicUnitTests {

@Override
@BeforeEach
public void setUp() throws InvocationTargetException, IllegalAccessException {
super.setUp();
this.connection.openPipeline();
connection.openPipeline();
}

@Test // DATAREDIS-528
Expand Down

0 comments on commit ab70114

Please sign in to comment.