|
38 | 38 | import org.springframework.data.redis.test.condition.RedisDriver;
|
39 | 39 | import org.springframework.data.redis.test.extension.parametrized.MethodSource;
|
40 | 40 | import org.springframework.data.redis.test.extension.parametrized.ParameterizedRedisTest;
|
| 41 | +import org.springframework.lang.Nullable; |
41 | 42 |
|
42 | 43 | /**
|
43 | 44 | * Integration tests for {@link DefaultRedisCacheWriter}.
|
44 | 45 | *
|
45 | 46 | * @author Christoph Strobl
|
46 | 47 | * @author Mark Paluch
|
| 48 | + * @author ChanYoung Joung |
47 | 49 | */
|
48 | 50 | @MethodSource("testParams")
|
49 | 51 | public class DefaultRedisCacheWriterTests {
|
@@ -419,6 +421,45 @@ void noOpStatisticsCollectorReturnsEmptyStatsInstance() {
|
419 | 421 | assertThat(stats.getPuts()).isZero();
|
420 | 422 | }
|
421 | 423 |
|
| 424 | + @ParameterizedRedisTest |
| 425 | + void doLockShouldGetLock() throws InterruptedException { |
| 426 | + |
| 427 | + int threadCount = 3; |
| 428 | + CountDownLatch beforeWrite = new CountDownLatch(threadCount); |
| 429 | + CountDownLatch afterWrite = new CountDownLatch(threadCount); |
| 430 | + |
| 431 | + DefaultRedisCacheWriter cw = new DefaultRedisCacheWriter(connectionFactory, Duration.ofMillis(50), |
| 432 | + BatchStrategies.keys()){ |
| 433 | + @Nullable |
| 434 | + protected Boolean doLock(String name, Object contextualKey, @Nullable Object contextualValue, |
| 435 | + RedisConnection connection) { |
| 436 | + Boolean doLock = super.doLock(name, contextualKey, contextualValue, connection); |
| 437 | + assertThat(doLock).isTrue(); |
| 438 | + return doLock; |
| 439 | + } |
| 440 | + }; |
| 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 | + } |
| 462 | + |
422 | 463 | private void doWithConnection(Consumer<RedisConnection> callback) {
|
423 | 464 |
|
424 | 465 | try (RedisConnection connection = connectionFactory.getConnection()) {
|
|
0 commit comments