Skip to content

Commit 0c93388

Browse files
committed
Fix DefaultStringRedisConnection tests.
Add missing command returns. Original Pull Request: #3226 See: #3211
1 parent a43541b commit 0c93388

File tree

4 files changed

+174
-23
lines changed

4 files changed

+174
-23
lines changed

src/test/java/org/springframework/data/redis/connection/DefaultStringRedisConnectionPipelineTests.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,42 @@ public void testHVals() {
412412
super.testHVals();
413413
}
414414

415+
@Test
416+
void hGetDelBytes() {
417+
doReturn(Collections.singletonList(Collections.singletonList(barBytes))).when(nativeConnection).closePipeline();
418+
super.hGetDelBytes();
419+
}
420+
421+
@Test
422+
void hGetDel() {
423+
doReturn(Collections.singletonList(Collections.singletonList(barBytes))).when(nativeConnection).closePipeline();
424+
super.hGetDel();
425+
}
426+
427+
@Test
428+
void hGetExBytes() {
429+
doReturn(Collections.singletonList(Collections.singletonList(barBytes))).when(nativeConnection).closePipeline();
430+
super.hGetExBytes();
431+
}
432+
433+
@Test
434+
void hGetEx() {
435+
doReturn(Collections.singletonList(Collections.singletonList(barBytes))).when(nativeConnection).closePipeline();
436+
super.hGetEx();
437+
}
438+
439+
@Test
440+
void hSetExBytes() {
441+
doReturn(Collections.singletonList(true)).when(nativeConnection).closePipeline();
442+
super.hSetExBytes();
443+
}
444+
445+
@Test
446+
void hSetEx() {
447+
doReturn(Collections.singletonList(true)).when(nativeConnection).closePipeline();
448+
super.hSetEx();
449+
}
450+
415451
@Test
416452
public void testIncrBytes() {
417453
doReturn(Collections.singletonList(2L)).when(nativeConnection).closePipeline();

src/test/java/org/springframework/data/redis/connection/DefaultStringRedisConnectionPipelineTxTests.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,46 @@ public void testHVals() {
438438
super.testHVals();
439439
}
440440

441+
@Test
442+
void hGetDelBytes() {
443+
doReturn(Collections.singletonList(Collections.singletonList(Collections.singletonList(barBytes))))
444+
.when(nativeConnection).closePipeline();
445+
super.hGetDelBytes();
446+
}
447+
448+
@Test
449+
void hGetDel() {
450+
doReturn(Collections.singletonList(Collections.singletonList(Collections.singletonList(barBytes))))
451+
.when(nativeConnection).closePipeline();
452+
super.hGetDel();
453+
}
454+
455+
@Test
456+
void hGetExBytes() {
457+
doReturn(Collections.singletonList(Collections.singletonList(Collections.singletonList(barBytes))))
458+
.when(nativeConnection).closePipeline();
459+
super.hGetExBytes();
460+
}
461+
462+
@Test
463+
void hGetEx() {
464+
doReturn(Collections.singletonList(Collections.singletonList(Collections.singletonList(barBytes))))
465+
.when(nativeConnection).closePipeline();
466+
super.hGetEx();
467+
}
468+
469+
@Test
470+
void hSetExBytes() {
471+
doReturn(Collections.singletonList(Collections.singletonList(true))).when(nativeConnection).closePipeline();
472+
super.hSetExBytes();
473+
}
474+
475+
@Test
476+
void hSetEx() {
477+
doReturn(Collections.singletonList(Collections.singletonList(true))).when(nativeConnection).closePipeline();
478+
super.hSetEx();
479+
}
480+
441481
@Test
442482
public void testIncrBytes() {
443483
doReturn(Collections.singletonList(Collections.singletonList(2L))).when(nativeConnection).closePipeline();

src/test/java/org/springframework/data/redis/connection/DefaultStringRedisConnectionTests.java

Lines changed: 61 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import java.util.concurrent.TimeUnit;
3232
import java.util.stream.Collectors;
3333

34+
import org.jspecify.annotations.Nullable;
3435
import org.junit.jupiter.api.BeforeEach;
3536
import org.junit.jupiter.api.Test;
3637
import org.junit.jupiter.api.extension.ExtendWith;
@@ -63,7 +64,10 @@
6364
import org.springframework.data.redis.connection.zset.Tuple;
6465
import org.springframework.data.redis.connection.zset.Weights;
6566
import org.springframework.data.redis.core.types.Expiration;
67+
import org.springframework.data.redis.serializer.RedisSerializer;
68+
import org.springframework.data.redis.serializer.SerializationException;
6669
import org.springframework.data.redis.serializer.StringRedisSerializer;
70+
import org.springframework.util.ConcurrentLruCache;
6771

6872
/**
6973
* Unit test of {@link DefaultStringRedisConnection}
@@ -85,7 +89,7 @@ public class DefaultStringRedisConnectionTests {
8589

8690
protected DefaultStringRedisConnection connection;
8791

88-
protected StringRedisSerializer serializer = StringRedisSerializer.UTF_8;
92+
protected RedisSerializer<String> serializer = new CachingSerializer(StringRedisSerializer.UTF_8);
8993

9094
protected String foo = "foo";
9195

@@ -544,54 +548,56 @@ public void testHVals() {
544548
}
545549

546550
@Test // GH-3211
547-
public void hGetDelBytes() {
551+
void hGetDelBytes() {
548552
doReturn(Collections.singletonList(barBytes)).when(nativeConnection).hGetDel(fooBytes, barBytes);
549-
List<byte[]> deleted = connection.hGetDel(fooBytes, barBytes);
550-
assertThat(deleted).containsExactly(barBytes);
553+
actual.add(connection.hGetDel(fooBytes, barBytes));
554+
verifyResults(Collections.singletonList(Collections.singletonList(barBytes)));
551555
}
552556

553557
@Test // GH-3211
554-
public void hGetDel() {
558+
void hGetDel() {
555559
doReturn(Collections.singletonList(barBytes)).when(nativeConnection).hGetDel(fooBytes, barBytes);
556-
List<String> deleted = connection.hGetDel(foo, bar);
557-
assertThat(deleted).containsExactly(bar);
560+
actual.add(connection.hGetDel(foo, bar));
561+
verifyResults(Collections.singletonList(Collections.singletonList(bar)));
558562
}
559563

560564
@Test // GH-3211
561-
public void hGetExBytes() {
562-
Expiration expiration = mock();
565+
void hGetExBytes() {
566+
Expiration expiration = Expiration.persistent();
563567
doReturn(bytesList).when(nativeConnection).hGetEx(fooBytes, expiration, barBytes);
564-
List<byte[]> values = connection.hGetEx(fooBytes, expiration, barBytes);
565-
assertThat(values).containsExactly(barBytes);
568+
actual.add(connection.hGetEx(fooBytes, expiration, barBytes));
569+
verifyResults(Collections.singletonList(Collections.singletonList(barBytes)));
566570
}
567571

568572
@Test // GH-3211
569-
public void hGetEx() {
570-
Expiration expiration = mock();
573+
void hGetEx() {
574+
Expiration expiration = Expiration.persistent();
571575
doReturn(bytesList).when(nativeConnection).hGetEx(fooBytes, expiration, barBytes);
572-
List<String> values = connection.hGetEx(foo, expiration, bar);
573-
assertThat(values).containsExactly(bar);
576+
actual.add(connection.hGetEx(foo, expiration, bar));
577+
verifyResults(Collections.singletonList(Collections.singletonList(bar)));
574578
}
575579

576580
@Test // GH-3211
577-
public void hSetExBytes() {
578-
Expiration expiration = mock();
579-
RedisHashCommands.HashFieldSetOption setOption = mock();
581+
void hSetExBytes() {
582+
Expiration expiration = Expiration.persistent();
583+
RedisHashCommands.HashFieldSetOption setOption = RedisHashCommands.HashFieldSetOption.upsert();
580584
Map<byte[], byte[]> fieldMap = Map.of(barBytes, bar2Bytes);
581585
doReturn(Boolean.TRUE).when(nativeConnection).hSetEx(fooBytes, fieldMap, setOption, expiration);
582-
assertThat(connection.hSetEx(fooBytes, fieldMap, setOption, expiration)).isTrue();
586+
actual.add(connection.hSetEx(fooBytes, fieldMap, setOption, expiration));
587+
verifyResults(Collections.singletonList(true));
583588
}
584589

585590
@Test // GH-3211
586-
public void hSetEx() {
587-
Expiration expiration = mock();
588-
RedisHashCommands.HashFieldSetOption setOption = mock();
591+
void hSetEx() {
592+
Expiration expiration = Expiration.persistent();
593+
RedisHashCommands.HashFieldSetOption setOption = RedisHashCommands.HashFieldSetOption.upsert();
589594
Map<String, String> stringMap = Map.of(bar, bar2);
590595
doReturn(Boolean.TRUE).when(nativeConnection).hSetEx(
591596
eq(fooBytes),
592597
argThat(fieldMap -> isFieldMap(fieldMap, stringMap)),
593598
eq(setOption), eq(expiration));
594-
assertThat(connection.hSetEx(foo, stringMap, setOption, expiration)).isTrue();
599+
actual.add(connection.hSetEx(foo, stringMap, setOption, expiration));
600+
verifyResults(Collections.singletonList(true));
595601
}
596602

597603
private boolean isFieldMap(Map<byte[], byte[]> fieldMap, Map<String, String> stringMap) {
@@ -2296,4 +2302,36 @@ protected List<Object> getResults() {
22962302
protected void verifyResults(List<?> expected) {
22972303
assertThat(getResults()).isEqualTo(expected);
22982304
}
2305+
2306+
private class CachingSerializer implements RedisSerializer<String> {
2307+
2308+
private final ConcurrentLruCache<String, byte[]> cache;
2309+
2310+
private final RedisSerializer<String> delegate;
2311+
2312+
public CachingSerializer(RedisSerializer<String> serializer) {
2313+
cache = new ConcurrentLruCache<>(256, serializer::serialize);
2314+
delegate = serializer;
2315+
}
2316+
2317+
@Override
2318+
public byte[] serialize(@Nullable String value) throws SerializationException {
2319+
return cache.get(value);
2320+
}
2321+
2322+
@Override
2323+
public @Nullable String deserialize(byte @Nullable [] bytes) throws SerializationException {
2324+
return delegate.deserialize(bytes);
2325+
}
2326+
2327+
@Override
2328+
public boolean canSerialize(Class<?> type) {
2329+
return delegate.canSerialize(type);
2330+
}
2331+
2332+
@Override
2333+
public Class<?> getTargetType() {
2334+
return delegate.getTargetType();
2335+
}
2336+
}
22992337
}

src/test/java/org/springframework/data/redis/connection/DefaultStringRedisConnectionTxTests.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,42 @@ public void testHVals() {
396396
super.testHVals();
397397
}
398398

399+
@Test
400+
void hGetDelBytes() {
401+
doReturn(Collections.singletonList(Collections.singletonList(barBytes))).when(nativeConnection).exec();
402+
super.hGetDelBytes();
403+
}
404+
405+
@Test
406+
void hGetDel() {
407+
doReturn(Collections.singletonList(Collections.singletonList(barBytes))).when(nativeConnection).exec();
408+
super.hGetDel();
409+
}
410+
411+
@Test
412+
void hGetExBytes() {
413+
doReturn(Collections.singletonList(Collections.singletonList(barBytes))).when(nativeConnection).exec();
414+
super.hGetExBytes();
415+
}
416+
417+
@Test
418+
void hGetEx() {
419+
doReturn(Collections.singletonList(Collections.singletonList(barBytes))).when(nativeConnection).exec();
420+
super.hGetEx();
421+
}
422+
423+
@Test
424+
void hSetExBytes() {
425+
doReturn(Collections.singletonList(true)).when(nativeConnection).exec();
426+
super.hSetExBytes();
427+
}
428+
429+
@Test
430+
void hSetEx() {
431+
doReturn(Collections.singletonList(true)).when(nativeConnection).exec();
432+
super.hSetEx();
433+
}
434+
399435
@Test
400436
public void testIncrBytes() {
401437
doReturn(Collections.singletonList(2L)).when(nativeConnection).exec();
@@ -1777,6 +1813,7 @@ public void xTrimApproximateShouldDelegateAndConvertCorrectly() {
17771813
super.xTrimApproximateShouldDelegateAndConvertCorrectly();
17781814
}
17791815

1816+
17801817
protected List<Object> getResults() {
17811818
return connection.exec();
17821819
}

0 commit comments

Comments
 (0)