Skip to content

Commit 9d212f8

Browse files
committed
Align Jedis restore TTL parameter with long type.
Signed-off-by: Dongliang Xie <dragonfsky@gmail.com>
1 parent c5c984a commit 9d212f8

2 files changed

Lines changed: 67 additions & 20 deletions

File tree

src/main/java/org/springframework/data/redis/connection/jedis/JedisKeyCommands.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
* @author ihaohong
6363
* @author Yordan Tsintsov
6464
* @author Tihomir Mateev
65+
* @author Dongliang Xie
6566
* @since 2.0
6667
*/
6768
@NullUnmarked
@@ -409,16 +410,12 @@ public void restore(byte @NonNull [] key, long ttlInMillis, byte @NonNull [] ser
409410
if (replace) {
410411

411412
connection.invokeStatus().just(KeyBinaryCommands::restore, KeyPipelineBinaryCommands::restore, key,
412-
(int) ttlInMillis, serializedValue, RestoreParams.restoreParams().replace());
413+
ttlInMillis, serializedValue, RestoreParams.restoreParams().replace());
413414
return;
414415
}
415416

416-
if (ttlInMillis > Integer.MAX_VALUE) {
417-
throw new IllegalArgumentException("TtlInMillis must be less than Integer.MAX_VALUE for restore in Jedis");
418-
}
419-
420417
connection.invokeStatus().just(KeyBinaryCommands::restore, KeyPipelineBinaryCommands::restore, key,
421-
(int) ttlInMillis, serializedValue);
418+
ttlInMillis, serializedValue);
422419
}
423420

424421
@Override

src/test/java/org/springframework/data/redis/connection/jedis/JedisConnectionUnitTests.java

Lines changed: 64 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,18 @@
1818
import static org.assertj.core.api.Assertions.*;
1919
import static org.mockito.Mockito.*;
2020

21+
import redis.clients.jedis.CommandArguments;
2122
import redis.clients.jedis.CommandObject;
2223
import redis.clients.jedis.Connection;
2324
import redis.clients.jedis.Jedis;
25+
import redis.clients.jedis.args.Rawable;
2426

2527
import java.io.IOException;
2628

2729
import org.junit.jupiter.api.BeforeEach;
2830
import org.junit.jupiter.api.Disabled;
2931
import org.junit.jupiter.api.Nested;
3032
import org.junit.jupiter.api.Test;
31-
import org.mockito.ArgumentCaptor;
3233
import org.springframework.dao.InvalidDataAccessApiUsageException;
3334
import org.springframework.dao.InvalidDataAccessResourceUsageException;
3435
import org.springframework.data.redis.connection.AbstractConnectionUnitTestBase;
@@ -43,6 +44,7 @@
4344
*
4445
* @author Christoph Strobl
4546
* @author Tihomir Mateev
47+
* @author Dongliang Xie
4648
*/
4749
class JedisConnectionUnitTests {
4850

@@ -61,23 +63,51 @@ public void setUp() {
6163
}
6264

6365
/**
64-
* Captures the CommandObject sent via executeCommand and returns a string containing
65-
* the command name and all arguments.
66+
* Captures the command sent to the mock connection and returns a string containing the
67+
* command name and all arguments.
6668
*/
67-
@SuppressWarnings("unchecked")
6869
private String captureCommand() {
69-
ArgumentCaptor<CommandObject<?>> captor = ArgumentCaptor.forClass(CommandObject.class);
70-
verify(connectionMock, atLeastOnce()).executeCommand(captor.capture());
71-
CommandObject<?> lastCommand = captor.getValue();
72-
// Build a string from all raw arguments
70+
71+
String command = null;
72+
73+
for (var invocation : mockingDetails(connectionMock).getInvocations()) {
74+
75+
Object[] arguments = invocation.getArguments();
76+
77+
if (arguments.length != 1) {
78+
continue;
79+
}
80+
81+
if (arguments[0] instanceof CommandObject<?> commandObject) {
82+
command = commandToString(commandObject.getArguments());
83+
} else if (arguments[0] instanceof CommandArguments commandArguments) {
84+
command = commandToString(commandArguments);
85+
}
86+
}
87+
88+
assertThat(command).as("captured Redis command").isNotNull();
89+
return command;
90+
}
91+
92+
private String commandToString(Iterable<? extends Rawable> arguments) {
93+
7394
StringBuilder sb = new StringBuilder();
74-
for (var arg : lastCommand.getArguments()) {
75-
if (sb.length() > 0) sb.append(" ");
95+
96+
for (Rawable arg : arguments) {
97+
if (sb.length() > 0) {
98+
sb.append(" ");
99+
}
76100
sb.append(new String(arg.getRaw()));
77101
}
102+
78103
return sb.toString();
79104
}
80105

106+
@SuppressWarnings({ "unchecked", "rawtypes" })
107+
private void returnOkForCommands() {
108+
when(connectionMock.executeCommand(any(CommandObject.class))).thenReturn("OK");
109+
}
110+
81111
@Test // DATAREDIS-184, GH-2153
82112
void shutdownWithNullShouldDelegateCommandCorrectly() {
83113

@@ -166,10 +196,30 @@ void shouldThrowExceptionWhenAccessingRedisSentinelsCommandsWhenNoSentinelsConfi
166196
.isThrownBy(() -> connection.getSentinelConnection());
167197
}
168198

169-
@Test // DATAREDIS-472
170-
void restoreShouldThrowExceptionWhenTtlInMillisExceedsIntegerRange() {
171-
assertThatIllegalArgumentException()
172-
.isThrownBy(() -> connection.restore("foo".getBytes(), (long) Integer.MAX_VALUE + 1L, "bar".getBytes()));
199+
@Test // GH-3386
200+
void restoreShouldPassLongTtlToJedis() {
201+
202+
long ttlInMillis = (long) Integer.MAX_VALUE + 1L;
203+
204+
returnOkForCommands();
205+
206+
connection.restore("foo".getBytes(), ttlInMillis, "bar".getBytes());
207+
208+
String command = captureCommand();
209+
assertThat(command).contains("RESTORE").contains(Long.toString(ttlInMillis));
210+
}
211+
212+
@Test // GH-3386
213+
void restoreWithReplaceShouldPassLongTtlToJedis() {
214+
215+
long ttlInMillis = (long) Integer.MAX_VALUE + 1L;
216+
217+
returnOkForCommands();
218+
219+
connection.restore("foo".getBytes(), ttlInMillis, "bar".getBytes(), true);
220+
221+
String command = captureCommand();
222+
assertThat(command).contains("RESTORE").contains(Long.toString(ttlInMillis)).contains("REPLACE");
173223
}
174224

175225
@Test // DATAREDIS-472

0 commit comments

Comments
 (0)