|
22 | 22 | import java.nio.charset.StandardCharsets;
|
23 | 23 | import java.time.Duration;
|
24 | 24 | import java.util.Collection;
|
25 |
| -import java.util.concurrent.CountDownLatch; |
26 |
| -import java.util.concurrent.ExecutionException; |
27 |
| -import java.util.concurrent.TimeUnit; |
| 25 | +import java.util.concurrent.*; |
28 | 26 | import java.util.concurrent.atomic.AtomicReference;
|
29 | 27 | import java.util.function.Consumer;
|
30 | 28 |
|
31 | 29 | import org.junit.jupiter.api.BeforeEach;
|
| 30 | +import org.junit.jupiter.api.Disabled; |
32 | 31 | import org.springframework.data.redis.connection.RedisConnection;
|
33 | 32 | import org.springframework.data.redis.connection.RedisConnectionFactory;
|
34 | 33 | import org.springframework.data.redis.connection.RedisStringCommands.SetOption;
|
|
38 | 37 | import org.springframework.data.redis.test.condition.RedisDriver;
|
39 | 38 | import org.springframework.data.redis.test.extension.parametrized.MethodSource;
|
40 | 39 | import org.springframework.data.redis.test.extension.parametrized.ParameterizedRedisTest;
|
| 40 | +import org.springframework.lang.Nullable; |
41 | 41 |
|
42 | 42 | /**
|
43 | 43 | * Integration tests for {@link DefaultRedisCacheWriter}.
|
@@ -419,6 +419,46 @@ void noOpStatisticsCollectorReturnsEmptyStatsInstance() {
|
419 | 419 | assertThat(stats.getPuts()).isZero();
|
420 | 420 | }
|
421 | 421 |
|
| 422 | + @ParameterizedRedisTest |
| 423 | + @Disabled |
| 424 | + void doLockShouldGetLock() throws InterruptedException { |
| 425 | + |
| 426 | + int threadCount = 3; |
| 427 | + |
| 428 | + DefaultRedisCacheWriter cw = new DefaultRedisCacheWriter(connectionFactory, Duration.ofMillis(50), |
| 429 | + BatchStrategies.keys()){ |
| 430 | + @Nullable |
| 431 | + protected Boolean doLock(String name, Object contextualKey, @Nullable Object contextualValue, |
| 432 | + RedisConnection connection) { |
| 433 | + Boolean doLock = super.doLock(name, contextualKey, contextualValue, connection); |
| 434 | + assertThat(doLock).isTrue(); |
| 435 | + return doLock; |
| 436 | + } |
| 437 | + }; |
| 438 | + |
| 439 | + CountDownLatch beforeWrite = new CountDownLatch(threadCount); |
| 440 | + CountDownLatch afterWrite = new CountDownLatch(threadCount); |
| 441 | + |
| 442 | + cw.lock(CACHE_NAME); |
| 443 | + |
| 444 | + for (int i = 0; i < threadCount; i++) { |
| 445 | + Thread th = new Thread(() -> { |
| 446 | + beforeWrite.countDown(); |
| 447 | + cw.putIfAbsent(CACHE_NAME, binaryCacheKey, binaryCacheValue, Duration.ZERO); |
| 448 | + afterWrite.countDown(); |
| 449 | + }); |
| 450 | + |
| 451 | + th.start(); |
| 452 | + } |
| 453 | + |
| 454 | + beforeWrite.await(); |
| 455 | + |
| 456 | + Thread.sleep(200); |
| 457 | + |
| 458 | + cw.unlock(CACHE_NAME); |
| 459 | + afterWrite.await(); |
| 460 | + |
| 461 | + } |
422 | 462 | private void doWithConnection(Consumer<RedisConnection> callback) {
|
423 | 463 |
|
424 | 464 | try (RedisConnection connection = connectionFactory.getConnection()) {
|
|
0 commit comments