task-90: 进度接口增加断网、超时、服务恢复和重复响应测试

This commit is contained in:
2026-08-30 23:01:17 +08:00
parent cc2722c104
commit bf38a1b9a0
3 changed files with 314 additions and 16 deletions
+13 -16
View File
@@ -8,19 +8,20 @@
unwrapJavaResponse,
} from "@/shared/api/http";
import { getTaskProgressCacheTtlMs } from "@/shared/task-progress-config";
import { createTaskProgressRequestCache } from "@/shared/task-progress-request-cache";
const JAVA_API_PREFIX = "/newApi/api";
type TaskProgressCacheEntry = {
expiresAt: number;
data: unknown;
};
interface TaskProgressBatchOptions {
force?: boolean;
}
const taskProgressResponseCache = new Map<string, TaskProgressCacheEntry>();
const taskProgressInflightRequests = new Map<string, Promise<unknown>>();
/** 进度批量接口的响应缓存与并发合并:TTL 过期、有界条目、in-flight 去重 */
const taskProgressResponseCache = createTaskProgressRequestCache<unknown>({
ttlMs: () => getTaskProgressCacheTtlMs(),
maxEntries: 100,
maxInflight: 16,
});
function getCurrentUserId() {
const raw =
@@ -59,13 +60,12 @@ async function postTaskProgressBatch<T>(
}
const cacheKey = buildTaskProgressRequestKey(path, normalizedTaskIds);
const now = Date.now();
const cached = taskProgressResponseCache.get(cacheKey);
if (!options.force && cached && cached.expiresAt > now) {
return cached.data as T;
if (!options.force && cached !== undefined) {
return cached as T;
}
const inflight = taskProgressInflightRequests.get(cacheKey);
const inflight = taskProgressResponseCache.getInflight(cacheKey);
if (!options.force && inflight) {
return (await inflight) as T;
}
@@ -74,17 +74,14 @@ async function postTaskProgressBatch<T>(
post<JavaApiResponse<T>, { taskIds: number[] }>(path, { taskIds: normalizedTaskIds }),
)
.then((data) => {
taskProgressResponseCache.set(cacheKey, {
data,
expiresAt: Date.now() + getTaskProgressCacheTtlMs(),
});
taskProgressResponseCache.set(cacheKey, data);
return data;
})
.finally(() => {
taskProgressInflightRequests.delete(cacheKey);
taskProgressResponseCache.endInflight(cacheKey);
});
taskProgressInflightRequests.set(cacheKey, requestPromise);
taskProgressResponseCache.startInflight(cacheKey, requestPromise);
return requestPromise;
}