20
20
import static org .assertj .core .api .Assumptions .assumeThat ;
21
21
import static org .awaitility .Awaitility .await ;
22
22
23
- import io .netty .util .concurrent .DefaultThreadFactory ;
24
-
25
- import reactor .core .publisher .Mono ;
26
-
27
23
import java .io .Serializable ;
28
24
import java .nio .charset .StandardCharsets ;
29
25
import java .time .Duration ;
48
44
import java .util .stream .IntStream ;
49
45
50
46
import org .junit .jupiter .api .BeforeEach ;
47
+
51
48
import org .springframework .cache .Cache .ValueWrapper ;
52
49
import org .springframework .cache .interceptor .SimpleKey ;
53
50
import org .springframework .cache .interceptor .SimpleKeyGenerator ;
62
59
import org .springframework .data .redis .test .extension .parametrized .ParameterizedRedisTest ;
63
60
import org .springframework .lang .Nullable ;
64
61
62
+ import io .netty .util .concurrent .DefaultThreadFactory ;
63
+
64
+ import reactor .core .publisher .Mono ;
65
+
65
66
/**
66
67
* Tests for {@link RedisCache} with {@link DefaultRedisCacheWriter} using different {@link RedisSerializer} and
67
68
* {@link RedisConnectionFactory} pairs.
@@ -569,9 +570,9 @@ void cacheGetWithTimeToIdleExpirationAfterEntryExpiresShouldReturnNull() {
569
570
assertThat (cache .get (this .cacheKey , Person .class )).isNull ();
570
571
}
571
572
572
- @ ParameterizedRedisTest // Gh -2650
573
+ @ ParameterizedRedisTest // GH -2650
573
574
@ SuppressWarnings ("unchecked" )
574
- void retrieveReturnsCachedValueCorrectly () throws Exception {
575
+ void retrieveReturnsCachedValue () throws Exception {
575
576
576
577
doWithConnection (connection -> connection .stringCommands ().set (this .binaryCacheKey , this .binarySample ));
577
578
@@ -581,10 +582,43 @@ void retrieveReturnsCachedValueCorrectly() throws Exception {
581
582
582
583
assertThat (value ).isNotNull ();
583
584
assertThat (value .get ()).isEqualTo (this .sample );
585
+ assertThat (value ).isDone ();
586
+ }
587
+
588
+ @ ParameterizedRedisTest // GH-2650
589
+ @ SuppressWarnings ("unchecked" )
590
+ void retrieveReturnsCachedValueWhenLockIsReleased () throws Exception {
591
+
592
+ String mockValue = "MockValue" ;
593
+ String testValue = "TestValue" ;
594
+
595
+ byte [] binaryCacheValue = this .serializer .serialize (testValue );
596
+
597
+ doWithConnection (connection -> connection .stringCommands ().set (this .binaryCacheKey , binaryCacheValue ));
598
+
599
+ RedisCache cache = new RedisCache ("cache" , usingLockingRedisCacheWriter (Duration .ofMillis (5L )),
600
+ usingRedisCacheConfiguration ());
601
+
602
+ RedisCacheWriter cacheWriter = cache .getCacheWriter ();
603
+
604
+ assertThat (cacheWriter ).isInstanceOf (DefaultRedisCacheWriter .class );
605
+
606
+ ((DefaultRedisCacheWriter ) cacheWriter ).lock ("cache" );
607
+
608
+ CompletableFuture <String > value = (CompletableFuture <String >) cache .retrieve (this .key );
609
+
610
+ assertThat (value ).isNotNull ();
611
+ assertThat (value .getNow (mockValue )).isEqualTo (mockValue );
612
+ assertThat (value ).isNotDone ();
613
+
614
+ ((DefaultRedisCacheWriter ) cacheWriter ).unlock ("cache" );
615
+
616
+ assertThat (value .get (15L , TimeUnit .MILLISECONDS )).isEqualTo (testValue );
617
+ assertThat (value ).isDone ();
584
618
}
585
619
586
- @ ParameterizedRedisTest // Gh -2650
587
- void retrieveReturnsLoadedValueCorrectly () throws Exception {
620
+ @ ParameterizedRedisTest // GH -2650
621
+ void retrieveReturnsLoadedValue () throws Exception {
588
622
589
623
RedisCache cache = new RedisCache ("cache" , usingLockingRedisCacheWriter (), usingRedisCacheConfiguration ());
590
624
@@ -608,6 +642,7 @@ void retrieveReturnsLoadedValueCorrectly() throws Exception {
608
642
assertThat (loaded .get ()).isFalse ();
609
643
assertThat (value .get ()).isEqualTo (jon );
610
644
assertThat (loaded .get ()).isTrue ();
645
+ assertThat (value ).isDone ();
611
646
}
612
647
613
648
private RedisCacheConfiguration usingRedisCacheConfiguration () {
@@ -629,6 +664,11 @@ private RedisCacheWriter usingLockingRedisCacheWriter() {
629
664
return RedisCacheWriter .lockingRedisCacheWriter (this .connectionFactory );
630
665
}
631
666
667
+ private RedisCacheWriter usingLockingRedisCacheWriter (Duration sleepTime ) {
668
+ return RedisCacheWriter .lockingRedisCacheWriter (this .connectionFactory , sleepTime ,
669
+ RedisCacheWriter .TtlFunction .persistent (), BatchStrategies .keys ());
670
+ }
671
+
632
672
private RedisCacheWriter usingNonLockingRedisCacheWriter () {
633
673
return RedisCacheWriter .nonLockingRedisCacheWriter (this .connectionFactory );
634
674
}
0 commit comments