From effa7bf38a424d2db5d343e2dcf85b2b50675d37 Mon Sep 17 00:00:00 2001 From: root Date: Fri, 26 Jun 2026 05:10:35 +0800 Subject: [PATCH] =?UTF-8?q?test(cache):=20=E9=9B=86=E6=88=90=E6=B5=8B?= =?UTF-8?q?=E8=AF=95=E5=87=BB=E7=A9=BF=E5=B9=B6=E5=8F=91(=E4=BA=92?= =?UTF-8?q?=E6=96=A5=E9=94=81=E4=B8=8B=20loader=20=E4=BB=85=E5=9B=9E?= =?UTF-8?q?=E6=BA=90=E4=B8=80=E6=AC=A1)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Claude Opus 4.8 (1M context) --- .../CacheBreakdownConcurrencyTest.java | 89 +++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 src/test/java/com/njcn/middle/cache/integration/CacheBreakdownConcurrencyTest.java diff --git a/src/test/java/com/njcn/middle/cache/integration/CacheBreakdownConcurrencyTest.java b/src/test/java/com/njcn/middle/cache/integration/CacheBreakdownConcurrencyTest.java new file mode 100644 index 0000000..35a270c --- /dev/null +++ b/src/test/java/com/njcn/middle/cache/integration/CacheBreakdownConcurrencyTest.java @@ -0,0 +1,89 @@ +package com.njcn.middle.cache.integration; + +import com.njcn.middle.cache.template.RedisCacheEnhanceTemplate; +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.SpringBootConfiguration; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.boot.test.context.SpringBootTest; + +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicInteger; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +/** + * 集成测试:N 个线程并发 getOrLoad 同一未命中热点 key,慢回源放大竞争窗口, + * 互斥锁应保证 loader 仅被调用 1 次,且所有线程拿到同一结果。 + */ +@ExtendWith(RedisAvailableCondition.class) +@SpringBootTest(classes = CacheBreakdownConcurrencyTest.TestApp.class, + properties = {"redis-cache.key-prefix=it-cache:"}) +class CacheBreakdownConcurrencyTest { + + @Autowired + private RedisCacheEnhanceTemplate cache; + + @Test + void onlyOneLoaderUnderConcurrency() throws Exception { + String key = "bd:hot:1"; + cache.delete(key); + + AtomicInteger calls = new AtomicInteger(); + java.util.function.Supplier loader = () -> { + calls.incrementAndGet(); + try { + Thread.sleep(300); // 慢回源 + } catch (InterruptedException ignored) { + Thread.currentThread().interrupt(); + } + return new User(1L, "hot"); + }; + + int n = 10; + ExecutorService pool = Executors.newFixedThreadPool(n); + CountDownLatch ready = new CountDownLatch(n); + CountDownLatch go = new CountDownLatch(1); + List> futures = new ArrayList<>(); + for (int i = 0; i < n; i++) { + futures.add(pool.submit(() -> { + ready.countDown(); + go.await(); + return cache.getOrLoad(key, User.class, 60, loader); + })); + } + ready.await(); + go.countDown(); + + for (Future f : futures) { + assertEquals("hot", f.get(10, TimeUnit.SECONDS).getName()); + } + assertEquals(1, calls.get(), "并发下 loader 应只回源一次"); + + pool.shutdownNow(); + cache.delete(key); + } + + @SpringBootConfiguration + @EnableAutoConfiguration + static class TestApp { + } + + @Data + @AllArgsConstructor + @NoArgsConstructor + static class User { + private Long id; + private String name; + } +}