3131import java .util .concurrent .TimeUnit ;
3232import java .util .stream .Collectors ;
3333
34+ import org .jspecify .annotations .Nullable ;
3435import org .junit .jupiter .api .BeforeEach ;
3536import org .junit .jupiter .api .Test ;
3637import org .junit .jupiter .api .extension .ExtendWith ;
6364import org .springframework .data .redis .connection .zset .Tuple ;
6465import org .springframework .data .redis .connection .zset .Weights ;
6566import org .springframework .data .redis .core .types .Expiration ;
67+ import org .springframework .data .redis .serializer .RedisSerializer ;
68+ import org .springframework .data .redis .serializer .SerializationException ;
6669import 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}
0 commit comments