|
18 | 18 | import static org.assertj.core.api.Assertions.*;
|
19 | 19 | import static org.assertj.core.api.Assumptions.*;
|
20 | 20 |
|
| 21 | +import io.netty.util.concurrent.DefaultThreadFactory; |
21 | 22 | import lombok.AllArgsConstructor;
|
22 | 23 | import lombok.Data;
|
23 | 24 | import lombok.NoArgsConstructor;
|
24 | 25 | import lombok.RequiredArgsConstructor;
|
25 | 26 |
|
26 | 27 | import java.io.Serializable;
|
27 | 28 | import java.nio.charset.StandardCharsets;
|
| 29 | +import java.time.Duration; |
28 | 30 | import java.util.Collection;
|
29 | 31 | import java.util.Collections;
|
30 | 32 | import java.util.Date;
|
31 |
| -import java.util.concurrent.ConcurrentHashMap; |
32 |
| -import java.util.concurrent.ConcurrentMap; |
33 | 33 | import java.util.concurrent.CountDownLatch;
|
| 34 | +import java.util.concurrent.LinkedBlockingDeque; |
| 35 | +import java.util.concurrent.ThreadPoolExecutor; |
| 36 | +import java.util.concurrent.TimeUnit; |
34 | 37 | import java.util.concurrent.atomic.AtomicInteger;
|
| 38 | +import java.util.concurrent.atomic.AtomicReference; |
35 | 39 | import java.util.function.Consumer;
|
36 |
| -import java.util.stream.Stream; |
| 40 | +import java.util.stream.IntStream; |
37 | 41 |
|
38 | 42 | import org.junit.jupiter.api.BeforeEach;
|
39 | 43 |
|
|
48 | 52 | import org.springframework.data.redis.serializer.RedisSerializer;
|
49 | 53 | import org.springframework.data.redis.test.extension.parametrized.MethodSource;
|
50 | 54 | import org.springframework.data.redis.test.extension.parametrized.ParameterizedRedisTest;
|
| 55 | +import org.springframework.lang.Nullable; |
51 | 56 |
|
52 | 57 | /**
|
53 | 58 | * Tests for {@link RedisCache} with {@link DefaultRedisCacheWriter} using different {@link RedisSerializer} and
|
54 | 59 | * {@link RedisConnectionFactory} pairs.
|
55 | 60 | *
|
56 | 61 | * @author Christoph Strobl
|
57 | 62 | * @author Mark Paluch
|
| 63 | + * @author Piotr Mionskowski |
58 | 64 | */
|
59 | 65 | @MethodSource("testParams")
|
60 | 66 | public class RedisCacheTests {
|
@@ -415,35 +421,85 @@ void cacheShouldFailOnNonConvertibleCacheKey() {
|
415 | 421 | }
|
416 | 422 |
|
417 | 423 | @ParameterizedRedisTest // GH-2079
|
418 |
| - void multipleThreadsLoadValueOnce() { |
| 424 | + void multipleThreadsLoadValueOnce() throws InterruptedException { |
419 | 425 |
|
420 |
| - int threadCount = 5; |
| 426 | + int threadCount = 2; |
421 | 427 |
|
422 |
| - ConcurrentMap<Integer, Integer> valuesByThreadId = new ConcurrentHashMap<>(threadCount); |
| 428 | + CountDownLatch prepare = new CountDownLatch(threadCount); |
| 429 | + CountDownLatch prepareForReturn = new CountDownLatch(1); |
| 430 | + CountDownLatch finished = new CountDownLatch(threadCount); |
| 431 | + AtomicInteger retrievals = new AtomicInteger(); |
| 432 | + AtomicReference<byte[]> storage = new AtomicReference<>(); |
423 | 433 |
|
424 |
| - CountDownLatch waiter = new CountDownLatch(threadCount); |
| 434 | + cache = new RedisCache("foo", new RedisCacheWriter() { |
| 435 | + @Override |
| 436 | + public void put(String name, byte[] key, byte[] value, @Nullable Duration ttl) { |
| 437 | + storage.set(value); |
| 438 | + } |
425 | 439 |
|
426 |
| - AtomicInteger threadIds = new AtomicInteger(0); |
| 440 | + @Override |
| 441 | + public byte[] get(String name, byte[] key) { |
427 | 442 |
|
428 |
| - AtomicInteger currentValueForKey = new AtomicInteger(0); |
| 443 | + prepare.countDown(); |
| 444 | + try { |
| 445 | + prepareForReturn.await(1, TimeUnit.MINUTES); |
| 446 | + } catch (InterruptedException e) { |
| 447 | + throw new RuntimeException(e); |
| 448 | + } |
429 | 449 |
|
430 |
| - Stream.generate(threadIds::getAndIncrement) |
431 |
| - .limit(threadCount) |
432 |
| - .parallel() |
433 |
| - .forEach((threadId) -> { |
434 |
| - waiter.countDown(); |
435 |
| - try { |
436 |
| - waiter.await(); |
437 |
| - } catch (InterruptedException e) { |
438 |
| - e.printStackTrace(); |
439 |
| - } |
440 |
| - Integer valueForThread = cache.get("key", currentValueForKey::incrementAndGet); |
441 |
| - valuesByThreadId.put(threadId, valueForThread); |
442 |
| - }); |
| 450 | + return storage.get(); |
| 451 | + } |
443 | 452 |
|
444 |
| - valuesByThreadId.forEach((thread, valueForThread) -> { |
445 |
| - assertThat(valueForThread).isEqualTo(currentValueForKey.get()); |
446 |
| - }); |
| 453 | + @Override |
| 454 | + public byte[] putIfAbsent(String name, byte[] key, byte[] value, @Nullable Duration ttl) { |
| 455 | + return new byte[0]; |
| 456 | + } |
| 457 | + |
| 458 | + @Override |
| 459 | + public void remove(String name, byte[] key) { |
| 460 | + |
| 461 | + } |
| 462 | + |
| 463 | + @Override |
| 464 | + public void clean(String name, byte[] pattern) { |
| 465 | + |
| 466 | + } |
| 467 | + |
| 468 | + @Override |
| 469 | + public void clearStatistics(String name) { |
| 470 | + |
| 471 | + } |
| 472 | + |
| 473 | + @Override |
| 474 | + public RedisCacheWriter withStatisticsCollector(CacheStatisticsCollector cacheStatisticsCollector) { |
| 475 | + return null; |
| 476 | + } |
| 477 | + |
| 478 | + @Override |
| 479 | + public CacheStatistics getCacheStatistics(String cacheName) { |
| 480 | + return null; |
| 481 | + } |
| 482 | + }, RedisCacheConfiguration.defaultCacheConfig()); |
| 483 | + |
| 484 | + ThreadPoolExecutor tpe = new ThreadPoolExecutor(threadCount, threadCount, 1, TimeUnit.MINUTES, |
| 485 | + new LinkedBlockingDeque<>(), new DefaultThreadFactory("RedisCacheTests")); |
| 486 | + |
| 487 | + IntStream.range(0, threadCount).forEach(it -> tpe.submit(() -> { |
| 488 | + cache.get("foo", retrievals::incrementAndGet); |
| 489 | + finished.countDown(); |
| 490 | + })); |
| 491 | + |
| 492 | + // wait until all Threads have arrived in RedisCacheWriter.get(…) |
| 493 | + prepare.await(); |
| 494 | + |
| 495 | + // let all threads continue |
| 496 | + prepareForReturn.countDown(); |
| 497 | + |
| 498 | + // wait until ThreadPoolExecutor has completed. |
| 499 | + finished.await(); |
| 500 | + tpe.shutdown(); |
| 501 | + |
| 502 | + assertThat(retrievals).hasValue(1); |
447 | 503 | }
|
448 | 504 |
|
449 | 505 | void doWithConnection(Consumer<RedisConnection> callback) {
|
|
0 commit comments