16
16
package org .springframework .data .redis .cache ;
17
17
18
18
import static org .assertj .core .api .Assertions .*;
19
- import static org .assertj .core .api .Assumptions .*;
20
19
import static org .awaitility .Awaitility .*;
21
20
22
21
import io .netty .util .concurrent .DefaultThreadFactory ;
49
48
import org .springframework .cache .support .NullValue ;
50
49
import org .springframework .data .redis .connection .RedisConnection ;
51
50
import org .springframework .data .redis .connection .RedisConnectionFactory ;
52
- import org .springframework .data .redis .connection .jedis .JedisConnectionFactory ;
53
- import org .springframework .data .redis .connection .lettuce .LettuceConnectionFactory ;
54
51
import org .springframework .data .redis .serializer .RedisSerializationContext .SerializationPair ;
55
52
import org .springframework .data .redis .serializer .RedisSerializer ;
56
53
import org .springframework .data .redis .test .condition .EnabledOnCommand ;
54
+ import org .springframework .data .redis .test .condition .EnabledOnRedisDriver ;
55
+ import org .springframework .data .redis .test .condition .EnabledOnRedisDriver .DriverQualifier ;
56
+ import org .springframework .data .redis .test .condition .RedisDriver ;
57
57
import org .springframework .data .redis .test .extension .parametrized .MethodSource ;
58
58
import org .springframework .data .redis .test .extension .parametrized .ParameterizedRedisTest ;
59
59
import org .springframework .lang .Nullable ;
@@ -80,7 +80,7 @@ public class RedisCacheTests {
80
80
81
81
private byte [] binaryNullValue = RedisSerializer .java ().serialize (NullValue .INSTANCE );
82
82
83
- private RedisConnectionFactory connectionFactory ;
83
+ private final @ DriverQualifier RedisConnectionFactory connectionFactory ;
84
84
private RedisSerializer serializer ;
85
85
private RedisCache cache ;
86
86
@@ -284,13 +284,9 @@ void clearShouldClearCache() {
284
284
}
285
285
286
286
@ ParameterizedRedisTest // GH-1721
287
+ @ EnabledOnRedisDriver (RedisDriver .LETTUCE ) // SCAN not supported via Jedis Cluster.
287
288
void clearWithScanShouldClearCache () {
288
289
289
- // SCAN not supported via Jedis Cluster.
290
- if (connectionFactory instanceof JedisConnectionFactory ) {
291
- assumeThat (((JedisConnectionFactory ) connectionFactory ).isRedisClusterAware ()).isFalse ();
292
- }
293
-
294
290
RedisCache cache = new RedisCache ("cache" ,
295
291
RedisCacheWriter .nonLockingRedisCacheWriter (connectionFactory , BatchStrategies .scan (25 )),
296
292
RedisCacheConfiguration .defaultCacheConfig ().serializeValuesWith (SerializationPair .fromSerializer (serializer )));
@@ -573,11 +569,9 @@ void cacheGetWithTimeToIdleExpirationAfterEntryExpiresShouldReturnNull() {
573
569
}
574
570
575
571
@ ParameterizedRedisTest
572
+ @ EnabledOnRedisDriver (RedisDriver .JEDIS )
576
573
void retrieveCacheValueUsingJedis () {
577
574
578
- // TODO: Is there a better way to do this? @EnableOnRedisDriver(RedisDriver.JEDIS) does not work!
579
- assumeThat (this .connectionFactory instanceof JedisConnectionFactory ).isTrue ();
580
-
581
575
assertThatExceptionOfType (UnsupportedOperationException .class )
582
576
.isThrownBy (() -> this .cache .retrieve (this .binaryCacheKey )).withMessageContaining ("RedisCache" );
583
577
@@ -587,12 +581,10 @@ void retrieveCacheValueUsingJedis() {
587
581
}
588
582
589
583
@ ParameterizedRedisTest // GH-2650
584
+ @ EnabledOnRedisDriver (RedisDriver .LETTUCE )
590
585
@ SuppressWarnings ("unchecked" )
591
586
void retrieveReturnsCachedValue () throws Exception {
592
587
593
- // TODO: Is there a better way to do this? @EnableOnRedisDriver(RedisDriver.LETTUCE) does not work!
594
- assumeThat (this .connectionFactory instanceof LettuceConnectionFactory ).isTrue ();
595
-
596
588
doWithConnection (connection -> connection .stringCommands ().set (this .binaryCacheKey , this .binarySample ));
597
589
598
590
RedisCache cache = new RedisCache ("cache" , usingLockingRedisCacheWriter (), usingRedisCacheConfiguration ());
@@ -605,13 +597,10 @@ void retrieveReturnsCachedValue() throws Exception {
605
597
}
606
598
607
599
@ ParameterizedRedisTest // GH-2650
600
+ @ EnabledOnRedisDriver (RedisDriver .LETTUCE )
608
601
@ SuppressWarnings ("unchecked" )
609
602
void retrieveReturnsCachedValueWhenLockIsReleased () throws Exception {
610
603
611
- // TODO: Is there a better way to do this? @EnableOnRedisDriver(RedisDriver.LETTUCE) does not work!
612
- assumeThat (this .connectionFactory instanceof LettuceConnectionFactory ).isTrue ();
613
-
614
- String mockValue = "MockValue" ;
615
604
String testValue = "TestValue" ;
616
605
617
606
byte [] binaryCacheValue = this .serializer .serialize (testValue );
@@ -634,11 +623,9 @@ void retrieveReturnsCachedValueWhenLockIsReleased() throws Exception {
634
623
}
635
624
636
625
@ ParameterizedRedisTest // GH-2650
626
+ @ EnabledOnRedisDriver (RedisDriver .LETTUCE )
637
627
void retrieveReturnsLoadedValue () throws Exception {
638
628
639
- // TODO: Is there a better way to do this? @EnableOnRedisDriver(RedisDriver.LETTUCE) does not work!
640
- assumeThat (this .connectionFactory instanceof LettuceConnectionFactory ).isTrue ();
641
-
642
629
RedisCache cache = new RedisCache ("cache" , usingLockingRedisCacheWriter (), usingRedisCacheConfiguration ());
643
630
AtomicBoolean loaded = new AtomicBoolean (false );
644
631
Person jon = new Person ("Jon" , Date .from (Instant .now ()));
@@ -658,11 +645,9 @@ void retrieveReturnsLoadedValue() throws Exception {
658
645
}
659
646
660
647
@ ParameterizedRedisTest // GH-2650
648
+ @ EnabledOnRedisDriver (RedisDriver .LETTUCE )
661
649
void retrieveStoresLoadedValue () throws Exception {
662
650
663
- // TODO: Is there a better way to do this? @EnableOnRedisDriver(RedisDriver.LETTUCE) does not work!
664
- assumeThat (this .connectionFactory instanceof LettuceConnectionFactory ).isTrue ();
665
-
666
651
RedisCache cache = new RedisCache ("cache" , usingLockingRedisCacheWriter (), usingRedisCacheConfiguration ());
667
652
Person jon = new Person ("Jon" , Date .from (Instant .now ()));
668
653
Supplier <CompletableFuture <Person >> valueLoaderSupplier = () -> CompletableFuture .completedFuture (jon );
@@ -675,11 +660,9 @@ void retrieveStoresLoadedValue() throws Exception {
675
660
}
676
661
677
662
@ ParameterizedRedisTest // GH-2650
663
+ @ EnabledOnRedisDriver (RedisDriver .LETTUCE )
678
664
void retrieveReturnsNull () throws Exception {
679
665
680
- // TODO: Is there a better way to do this? @EnableOnRedisDriver(RedisDriver.LETTUCE) does not work!
681
- assumeThat (this .connectionFactory instanceof LettuceConnectionFactory ).isTrue ();
682
-
683
666
doWithConnection (connection -> connection .stringCommands ().set (this .binaryCacheKey , this .binaryNullValue ));
684
667
685
668
RedisCache cache = new RedisCache ("cache" , usingLockingRedisCacheWriter (), usingRedisCacheConfiguration ());
@@ -733,11 +716,8 @@ private Function<RedisCacheConfiguration, RedisCacheConfiguration> withTtiExpira
733
716
}
734
717
735
718
void doWithConnection (Consumer <RedisConnection > callback ) {
736
- RedisConnection connection = connectionFactory .getConnection ();
737
- try {
719
+ try (RedisConnection connection = connectionFactory .getConnection ()) {
738
720
callback .accept (connection );
739
- } finally {
740
- connection .close ();
741
721
}
742
722
}
743
723
0 commit comments