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