From 8e7fbb294ab4ca4a13c9d2b0d88ba350d4e9c494 Mon Sep 17 00:00:00 2001 From: chenchuchuan <13554600537@163.com> Date: Fri, 30 Oct 2026 17:06:56 +0800 Subject: [PATCH] =?UTF-8?q?test(http):=20TokenManager=20=E7=AD=89=E9=94=81?= =?UTF-8?q?=E8=B6=85=E6=97=B6=E5=8F=AF=E6=B3=A8=E5=85=A5=EF=BC=8C=E6=B5=8B?= =?UTF-8?q?=E8=AF=95=E4=BB=8E=205s=20=E9=99=8D=E5=88=B0=2050ms?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 生产默认仍是 50ms 轮询 / 5s 超时;新增仅测试可见的构造器, 避免 lockHeldByAnotherInstanceIsNotBrokenThrough 真等 5 秒。 Co-Authored-By: Claude --- .../java/common/ecommerce/http/TokenManager.java | 20 ++++++++++++++++---- .../java/common/ecommerce/http/TokenManagerTest.java | 2 +- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/http/src/main/java/common/ecommerce/http/TokenManager.java b/http/src/main/java/common/ecommerce/http/TokenManager.java index a9b4999..d815517 100644 --- a/http/src/main/java/common/ecommerce/http/TokenManager.java +++ b/http/src/main/java/common/ecommerce/http/TokenManager.java @@ -41,16 +41,28 @@ public abstract class TokenManager { /** 拿不到锁时最多等待多久;超过即认为对方卡死。 */ private static final long WAIT_TIMEOUT_MILLIS = 5_000L; + /** 测试用构造器可缩短等待,生产固定值。 */ + private final long waitIntervalMillis; + private final long waitTimeoutMillis; + protected final TokenStore tokenStore; protected final LockProvider lockProvider; protected final Clock clock; private final long earlyRefreshSeconds; protected TokenManager(TokenStore tokenStore, LockProvider lockProvider, Clock clock, long earlyRefreshSeconds) { + this(tokenStore, lockProvider, clock, earlyRefreshSeconds, WAIT_INTERVAL_MILLIS, WAIT_TIMEOUT_MILLIS); + } + + /** 仅供测试缩短等待;生产用上面的构造器。 */ + protected TokenManager(TokenStore tokenStore, LockProvider lockProvider, Clock clock, long earlyRefreshSeconds, + long waitIntervalMillis, long waitTimeoutMillis) { this.tokenStore = tokenStore; this.lockProvider = lockProvider; this.clock = clock; this.earlyRefreshSeconds = earlyRefreshSeconds; + this.waitIntervalMillis = waitIntervalMillis; + this.waitTimeoutMillis = waitTimeoutMillis; } /** 拿到可用的 access token;必要时刷新。 */ @@ -60,7 +72,7 @@ public abstract class TokenManager { return cached; } - long deadline = System.currentTimeMillis() + WAIT_TIMEOUT_MILLIS; + long deadline = System.currentTimeMillis() + waitTimeoutMillis; while (true) { Optional lock = lockProvider.tryLock("token-refresh:" + tokenKey, REFRESH_LOCK_TTL_SECONDS); if (lock.isPresent()) { @@ -71,7 +83,7 @@ public abstract class TokenManager { } } // 别的实例正在刷新:等它写完再读缓存 - if (!waitQuietly()) { + if (!waitQuietly(waitIntervalMillis)) { throw authError("interrupted while waiting for token refresh: " + tokenKey); } AccessToken refreshedByPeer = fresh(tokenKey); @@ -117,9 +129,9 @@ public abstract class TokenManager { return null; } - private static boolean waitQuietly() { + private static boolean waitQuietly(long millis) { try { - Thread.sleep(WAIT_INTERVAL_MILLIS); + Thread.sleep(millis); return true; } catch (InterruptedException e) { Thread.currentThread().interrupt(); diff --git a/http/src/test/java/common/ecommerce/http/TokenManagerTest.java b/http/src/test/java/common/ecommerce/http/TokenManagerTest.java index ca6667d..d33de5b 100644 --- a/http/src/test/java/common/ecommerce/http/TokenManagerTest.java +++ b/http/src/test/java/common/ecommerce/http/TokenManagerTest.java @@ -113,7 +113,7 @@ class TokenManagerTest { void lockHeldByAnotherInstanceIsNotBrokenThrough() { // 模拟另一个实例正在刷新:锁被占,缓存里也没有有效 token locks.tryLock("token-refresh:k", 30); - TokenManager quick = new TokenManager(tokenStore, locks, clock, 600) { + TokenManager quick = new TokenManager(tokenStore, locks, clock, 600, 5, 50) { @Override protected AccessToken refresh(ChannelSecret secret, AccessToken previous) { throw new IllegalStateException("must not be called");