|
18 | 18 | import static org.assertj.core.api.Assertions.*;
|
19 | 19 | import static org.mockito.Mockito.*;
|
20 | 20 |
|
21 |
| -import io.lettuce.core.RedisClient; |
22 |
| -import io.lettuce.core.RedisFuture; |
23 |
| -import io.lettuce.core.XAddArgs; |
24 |
| -import io.lettuce.core.XClaimArgs; |
| 21 | +import io.lettuce.core.*; |
25 | 22 | import io.lettuce.core.api.StatefulRedisConnection;
|
26 | 23 | import io.lettuce.core.api.async.RedisAsyncCommands;
|
27 | 24 | import io.lettuce.core.codec.ByteArrayCodec;
|
28 | 25 | import io.lettuce.core.codec.RedisCodec;
|
29 | 26 | import io.lettuce.core.codec.StringCodec;
|
| 27 | +import io.lettuce.core.output.ScanOutput; |
30 | 28 | import io.lettuce.core.output.StatusOutput;
|
31 | 29 | import io.lettuce.core.protocol.AsyncCommand;
|
32 | 30 | import io.lettuce.core.protocol.Command;
|
33 | 31 | import io.lettuce.core.protocol.CommandArgs;
|
34 | 32 | import io.lettuce.core.protocol.CommandType;
|
35 | 33 |
|
36 | 34 | import java.lang.reflect.InvocationTargetException;
|
| 35 | +import java.nio.ByteBuffer; |
37 | 36 | import java.time.Duration;
|
38 | 37 | import java.util.Collections;
|
| 38 | +import java.util.List; |
| 39 | +import java.util.Map; |
| 40 | +import java.util.Map.Entry; |
39 | 41 |
|
40 | 42 | import org.junit.jupiter.api.BeforeEach;
|
41 | 43 | import org.junit.jupiter.api.Disabled;
|
|
48 | 50 | import org.springframework.data.redis.connection.RedisStreamCommands.XAddOptions;
|
49 | 51 | import org.springframework.data.redis.connection.RedisStreamCommands.XClaimOptions;
|
50 | 52 | import org.springframework.data.redis.connection.stream.MapRecord;
|
| 53 | +import org.springframework.data.redis.connection.zset.Tuple; |
| 54 | +import org.springframework.data.redis.core.Cursor; |
| 55 | +import org.springframework.data.redis.core.KeyScanOptions; |
51 | 56 | import org.springframework.test.util.ReflectionTestUtils;
|
52 | 57 |
|
53 | 58 | /**
|
@@ -247,6 +252,146 @@ void xaddShouldHonorNoMkStream() {
|
247 | 252 | assertThat(args.getValue()).extracting("nomkstream").isEqualTo(true);
|
248 | 253 | }
|
249 | 254 |
|
| 255 | + @Test // GH-2796 |
| 256 | + void scanShouldOperateUponUnsigned64BitCursorId() { |
| 257 | + |
| 258 | + String cursorId = "9286422431637962824"; |
| 259 | + KeyScanCursor<byte[]> sc = new KeyScanCursor<>() { |
| 260 | + @Override |
| 261 | + public List<byte[]> getKeys() { |
| 262 | + return List.of("spring".getBytes()); |
| 263 | + } |
| 264 | + }; |
| 265 | + sc.setCursor(cursorId); |
| 266 | + sc.setFinished(false); |
| 267 | + |
| 268 | + Command<byte[], byte[], KeyScanCursor<byte[]>> command = new Command<>(new LettuceConnection.CustomCommandType("SCAN"), |
| 269 | + new ScanOutput<>(ByteArrayCodec.INSTANCE, sc) { |
| 270 | + @Override |
| 271 | + protected void setOutput(ByteBuffer bytes) { |
| 272 | + |
| 273 | + } |
| 274 | + }); |
| 275 | + AsyncCommand<byte[], byte[], KeyScanCursor<byte[]>> future = new AsyncCommand<>(command); |
| 276 | + future.complete(); |
| 277 | + |
| 278 | + when(asyncCommandsMock.scan(any(ScanCursor.class),any(ScanArgs.class))).thenReturn(future, future); |
| 279 | + |
| 280 | + Cursor<byte[]> cursor = connection.scan(KeyScanOptions.NONE); |
| 281 | + cursor.next(); //initial |
| 282 | + assertThat(cursor.getCursorId()).isEqualTo(Long.parseUnsignedLong(cursorId)); |
| 283 | + |
| 284 | + cursor.next(); // fetch next |
| 285 | + ArgumentCaptor<ScanCursor> captor = ArgumentCaptor.forClass(ScanCursor.class); |
| 286 | + verify(asyncCommandsMock, times(2)).scan(captor.capture(), any(ScanArgs.class)); |
| 287 | + assertThat(captor.getAllValues()).map(ScanCursor::getCursor).containsExactly("0", cursorId); |
| 288 | + } |
| 289 | + |
| 290 | + @Test // GH-2796 |
| 291 | + void sScanShouldOperateUponUnsigned64BitCursorId() { |
| 292 | + |
| 293 | + String cursorId = "9286422431637962824"; |
| 294 | + ValueScanCursor<byte[]> sc = new ValueScanCursor<>() { |
| 295 | + @Override |
| 296 | + public List<byte[]> getValues() { |
| 297 | + return List.of("spring".getBytes()); |
| 298 | + } |
| 299 | + }; |
| 300 | + sc.setCursor(cursorId); |
| 301 | + sc.setFinished(false); |
| 302 | + |
| 303 | + Command<byte[], byte[], ValueScanCursor<byte[]>> command = new Command<>(new LettuceConnection.CustomCommandType("SSCAN"), |
| 304 | + new ScanOutput<>(ByteArrayCodec.INSTANCE, sc) { |
| 305 | + @Override |
| 306 | + protected void setOutput(ByteBuffer bytes) { |
| 307 | + |
| 308 | + } |
| 309 | + }); |
| 310 | + AsyncCommand<byte[], byte[], ValueScanCursor<byte[]>> future = new AsyncCommand<>(command); |
| 311 | + future.complete(); |
| 312 | + |
| 313 | + when(asyncCommandsMock.sscan(any(byte[].class), any(ScanCursor.class),any(ScanArgs.class))).thenReturn(future, future); |
| 314 | + |
| 315 | + Cursor<byte[]> cursor = connection.setCommands().sScan("key".getBytes(), KeyScanOptions.NONE); |
| 316 | + cursor.next(); //initial |
| 317 | + assertThat(cursor.getCursorId()).isEqualTo(Long.parseUnsignedLong(cursorId)); |
| 318 | + |
| 319 | + cursor.next(); // fetch next |
| 320 | + ArgumentCaptor<ScanCursor> captor = ArgumentCaptor.forClass(ScanCursor.class); |
| 321 | + verify(asyncCommandsMock, times(2)).sscan(any(byte[].class), captor.capture(), any(ScanArgs.class)); |
| 322 | + assertThat(captor.getAllValues()).map(ScanCursor::getCursor).containsExactly("0", cursorId); |
| 323 | + } |
| 324 | + |
| 325 | + @Test // GH-2796 |
| 326 | + void zScanShouldOperateUponUnsigned64BitCursorId() { |
| 327 | + |
| 328 | + String cursorId = "9286422431637962824"; |
| 329 | + ScoredValueScanCursor<byte[]> sc = new ScoredValueScanCursor<>() { |
| 330 | + @Override |
| 331 | + public List<ScoredValue<byte[]>> getValues() { |
| 332 | + return List.of(ScoredValue.just(10D, "spring".getBytes())); |
| 333 | + } |
| 334 | + }; |
| 335 | + sc.setCursor(cursorId); |
| 336 | + sc.setFinished(false); |
| 337 | + |
| 338 | + Command<byte[], byte[], ScoredValueScanCursor<byte[]>> command = new Command<>(new LettuceConnection.CustomCommandType("ZSCAN"), |
| 339 | + new ScanOutput<>(ByteArrayCodec.INSTANCE, sc) { |
| 340 | + @Override |
| 341 | + protected void setOutput(ByteBuffer bytes) { |
| 342 | + |
| 343 | + } |
| 344 | + }); |
| 345 | + AsyncCommand<byte[], byte[], ScoredValueScanCursor<byte[]>> future = new AsyncCommand<>(command); |
| 346 | + future.complete(); |
| 347 | + |
| 348 | + when(asyncCommandsMock.zscan(any(byte[].class), any(ScanCursor.class),any(ScanArgs.class))).thenReturn(future, future); |
| 349 | + |
| 350 | + Cursor<Tuple> cursor = connection.zSetCommands().zScan("key".getBytes(), KeyScanOptions.NONE); |
| 351 | + cursor.next(); //initial |
| 352 | + assertThat(cursor.getCursorId()).isEqualTo(Long.parseUnsignedLong(cursorId)); |
| 353 | + |
| 354 | + cursor.next(); // fetch next |
| 355 | + ArgumentCaptor<ScanCursor> captor = ArgumentCaptor.forClass(ScanCursor.class); |
| 356 | + verify(asyncCommandsMock, times(2)).zscan(any(byte[].class), captor.capture(), any(ScanArgs.class)); |
| 357 | + assertThat(captor.getAllValues()).map(ScanCursor::getCursor).containsExactly("0", cursorId); |
| 358 | + } |
| 359 | + |
| 360 | + @Test // GH-2796 |
| 361 | + void hScanShouldOperateUponUnsigned64BitCursorId() { |
| 362 | + |
| 363 | + String cursorId = "9286422431637962824"; |
| 364 | + MapScanCursor<byte[], byte[]> sc = new MapScanCursor<>() { |
| 365 | + @Override |
| 366 | + public Map<byte[], byte[]> getMap() { |
| 367 | + return Map.of("spring".getBytes(), "data".getBytes()); |
| 368 | + } |
| 369 | + }; |
| 370 | + sc.setCursor(cursorId); |
| 371 | + sc.setFinished(false); |
| 372 | + |
| 373 | + Command<byte[], byte[], MapScanCursor<byte[], byte[]>> command = new Command<>(new LettuceConnection.CustomCommandType("HSCAN"), |
| 374 | + new ScanOutput<>(ByteArrayCodec.INSTANCE, sc) { |
| 375 | + @Override |
| 376 | + protected void setOutput(ByteBuffer bytes) { |
| 377 | + |
| 378 | + } |
| 379 | + }); |
| 380 | + AsyncCommand<byte[], byte[], MapScanCursor<byte[], byte[]>> future = new AsyncCommand<>(command); |
| 381 | + future.complete(); |
| 382 | + |
| 383 | + when(asyncCommandsMock.hscan(any(byte[].class), any(ScanCursor.class),any(ScanArgs.class))).thenReturn(future, future); |
| 384 | + |
| 385 | + Cursor<Entry<byte[], byte[]>> cursor = connection.hashCommands().hScan("key".getBytes(), KeyScanOptions.NONE); |
| 386 | + cursor.next(); //initial |
| 387 | + assertThat(cursor.getCursorId()).isEqualTo(Long.parseUnsignedLong(cursorId)); |
| 388 | + |
| 389 | + cursor.next(); // fetch next |
| 390 | + ArgumentCaptor<ScanCursor> captor = ArgumentCaptor.forClass(ScanCursor.class); |
| 391 | + verify(asyncCommandsMock, times(2)).hscan(any(byte[].class), captor.capture(), any(ScanArgs.class)); |
| 392 | + assertThat(captor.getAllValues()).map(ScanCursor::getCursor).containsExactly("0", cursorId); |
| 393 | + } |
| 394 | + |
250 | 395 | }
|
251 | 396 |
|
252 | 397 | public static class LettucePipelineConnectionUnitTests extends BasicUnitTests {
|
@@ -304,5 +449,29 @@ public void getClientNameShouldSendRequestCorrectly() {
|
304 | 449 | connection.getClientName();
|
305 | 450 | verify(asyncCommandsMock).clientGetname();
|
306 | 451 | }
|
| 452 | + |
| 453 | + @Test |
| 454 | + @Disabled("scan not supported in pipeline") |
| 455 | + void scanShouldOperateUponUnsigned64BitCursorId() { |
| 456 | + |
| 457 | + } |
| 458 | + |
| 459 | + @Test |
| 460 | + @Disabled("scan not supported in pipeline") |
| 461 | + void sScanShouldOperateUponUnsigned64BitCursorId() { |
| 462 | + |
| 463 | + } |
| 464 | + |
| 465 | + @Test |
| 466 | + @Disabled("scan not supported in pipeline") |
| 467 | + void zScanShouldOperateUponUnsigned64BitCursorId() { |
| 468 | + |
| 469 | + } |
| 470 | + |
| 471 | + @Test |
| 472 | + @Disabled("scan not supported in pipeline") |
| 473 | + void hScanShouldOperateUponUnsigned64BitCursorId() { |
| 474 | + |
| 475 | + } |
307 | 476 | }
|
308 | 477 | }
|
0 commit comments