7368322791
安全 - /api/ziniao/** 五个匿名接口加管理员鉴权(此前可匿名换取任意员工店铺登录令牌) - 删除 Flask 遗留后门:默认密码建超管 + 每次启动写生产 users 表(服务端与客户端各一份) - 进度/详情接口归属过滤:新增 TaskProgressOwnershipSupport,11 模块 progress/light 与 /tasks/batch 接入,DTO 补 userId,前端 13 个查询封装补传(未传时后端不过滤,兼容旧端) - 代理提取链接(含账密)不再明文入日志(新增 common/util/SecretMasking) - 全局异常兜底不再回传原始异常信息;内部令牌比较改常量时间 - 登录加失败计数与锁定(10 次锁 15 分钟);品牌源文件下载加 SSRF 防护 - AdminApiGuardFilter 覆盖前缀从 2 扩到 15(开关默认 false,行为不变,为收紧做准备) - 生产关闭 springdoc/knife4j(/doc.html 匿名可读全部接口定义) 正确性 - 40901/40902 拆分:锁竞争不再被伪装成 success=true(此前客户端停止重试、分片静默丢失) - 假成功收敛:集采明细批量写失败改为抛出、去重 worker 异常标失败、4 个 worker 改判 success 字段、publish 空 ASIN 行参与批次 flush、巡店删除全失败带 error 上报 - 客户端心跳 discard 移入 finally(7 模块,失败路径不再留僵尸 RUNNING 任务) - 状态机条件更新:跟价停止循环、集采 activate/fail、imagevideo 归档回填、店铺匹配提交 性能 - 前端入口包 JS 1.05MB→204KB、CSS 355KB→10.7KB(Element Plus 改按需 + el-config-provider) - 载荷引用计数按指针里的 taskId 收敛(原 JSON 列 IN 全表扫且逐行调用) - 店铺明细多值批量 INSERT;快照 upsert 预载缓存;结果文件列改单条 UPDATE - 新增迁移 V120(补 3 个缺失索引)/V121(删 4 个被覆盖的冗余索引)/V122(URL 前缀索引) 稳定性 - 新增 common/util/ThreadPools 有界线程池替换 5 处无界队列(防堆积 OOM) - Redis 锁释放改 Lua 原子校验(原裸 delete 会误删他人已过期的锁) - imagevideo 加死节点接管;锁续期失败重试;调度池 4→16;openStream 全部加超时 - 事务内远程对象删除移到提交后;启动恢复锁按实例命名 客户端 - 不再 taskkill /f /im chrome.exe(改为按调试端口精准回收,不杀用户自己的浏览器) - 密码检测不再无条件杀紫鸟进程;品牌检测加全局互斥(代理池不再互相覆盖) - base_dir 统一到 exe 目录(原被 os.getcwd() 覆盖,日志/缓存会分裂两个目录) - 缓存加定时清理;图片下载加超时;mkstemp 句柄托管 测试 - 同步更新受影响的契约测试(构造器签名/条件更新/方法改名/新增接口方法等) - 修复 FaultInjectionTest 等 3 处 mock 未 stub 流式 read 导致的读循环 OOM - mvn test 2795 个测试全绿
214 lines
9.3 KiB
Java
214 lines
9.3 KiB
Java
package com.nanri.aiimage.config;
|
||
|
||
import com.sun.net.httpserver.HttpServer;
|
||
import org.junit.jupiter.api.AfterAll;
|
||
import org.junit.jupiter.api.BeforeAll;
|
||
import org.junit.jupiter.api.Test;
|
||
import org.springframework.web.client.ResourceAccessException;
|
||
import org.springframework.web.client.RestClient;
|
||
|
||
import java.io.IOException;
|
||
import java.io.OutputStream;
|
||
import java.net.InetSocketAddress;
|
||
import java.net.http.HttpClient;
|
||
import java.nio.charset.StandardCharsets;
|
||
import java.time.Duration;
|
||
import java.util.concurrent.ExecutorService;
|
||
import java.util.concurrent.Executors;
|
||
import java.util.concurrent.TimeoutException;
|
||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||
import static org.junit.jupiter.api.Assertions.fail;
|
||
|
||
/**
|
||
* task-173:超时生效测试(plan 10,mock 慢端点)。
|
||
*
|
||
* 用本机回环 HttpServer 模拟慢端点,验证 HttpClientPool.requestFactory(readTimeout)
|
||
* 真实管线:慢于配置超时的端点超时内失败、快/预算内响应不误伤、超时类型为
|
||
* java.util.concurrent.TimeoutException、超时时长随配置缩放、连接超时对本机不可达地址生效。
|
||
* 全部本地确定性,不发外部网络请求。
|
||
*
|
||
* 已核实的平台边界(task-166 记录):JDK 请求超时封顶的是"到响应头"的时长,
|
||
* 流式 body 读取不受请求超时约束,因此不为此编造"总时长封顶 body"的断言。
|
||
*/
|
||
class HttpClientTimeoutEffectiveTest {
|
||
|
||
private static HttpServer server;
|
||
private static ExecutorService serverExecutor;
|
||
|
||
@BeforeAll
|
||
static void startServer() throws IOException {
|
||
serverExecutor = Executors.newCachedThreadPool(r -> {
|
||
Thread t = new Thread(r, "http-timeout-test");
|
||
t.setDaemon(true);
|
||
return t;
|
||
});
|
||
server = HttpServer.create(new InetSocketAddress("127.0.0.1", 0), 0);
|
||
server.setExecutor(serverExecutor);
|
||
server.createContext("/slow", exchange -> {
|
||
String query = exchange.getRequestURI().getQuery(); // ms=N
|
||
long ms = 2_000;
|
||
if (query != null && query.startsWith("ms=")) {
|
||
ms = Long.parseLong(query.substring(3));
|
||
}
|
||
sleep(ms);
|
||
respond(exchange, "slow-done");
|
||
});
|
||
server.createContext("/fast", exchange -> respond(exchange, "ok"));
|
||
server.start();
|
||
}
|
||
|
||
@AfterAll
|
||
static void stopServer() {
|
||
if (server != null) {
|
||
server.stop(0);
|
||
}
|
||
if (serverExecutor != null) {
|
||
serverExecutor.shutdownNow();
|
||
}
|
||
}
|
||
|
||
private static void respond(com.sun.net.httpserver.HttpExchange exchange, String body) throws IOException {
|
||
byte[] bytes = body.getBytes(StandardCharsets.UTF_8);
|
||
exchange.sendResponseHeaders(200, bytes.length);
|
||
try (OutputStream out = exchange.getResponseBody()) {
|
||
out.write(bytes);
|
||
}
|
||
}
|
||
|
||
private static void sleep(long ms) {
|
||
try {
|
||
Thread.sleep(ms);
|
||
} catch (InterruptedException e) {
|
||
Thread.currentThread().interrupt();
|
||
}
|
||
}
|
||
|
||
private static String baseUrl() {
|
||
return "http://127.0.0.1:" + server.getAddress().getPort();
|
||
}
|
||
|
||
private static RestClient clientWithReadTimeout(int readTimeoutMillis) {
|
||
return RestClient.builder()
|
||
.requestFactory(HttpClientPool.requestFactory(readTimeoutMillis))
|
||
.build();
|
||
}
|
||
|
||
@Test
|
||
void readTimeoutFiresOnSlowEndpoint() {
|
||
RestClient client = clientWithReadTimeout(300);
|
||
long startedAt = System.nanoTime();
|
||
ResourceAccessException ex = assertThrows(ResourceAccessException.class,
|
||
() -> client.get().uri(baseUrl() + "/slow?ms=3000").retrieve().toBodilessEntity());
|
||
long elapsedMs = (System.nanoTime() - startedAt) / 1_000_000;
|
||
assertTrue(isTimeoutRoot(ex), "根因应为超时异常,实际 " + root(ex));
|
||
assertTrue(elapsedMs < 1_500, "慢端点应在配置超时附近失败,实际 " + elapsedMs + "ms");
|
||
}
|
||
|
||
@Test
|
||
void fastEndpointSucceedsWithoutTimeout() {
|
||
RestClient client = clientWithReadTimeout(300);
|
||
client.get().uri(baseUrl() + "/fast").retrieve().toBodilessEntity();
|
||
// 未抛异常即通过:快响应不应被误判超时
|
||
}
|
||
|
||
@Test
|
||
void moderatelySlowWithinBudgetSucceeds() {
|
||
// 预算内慢响应(500ms < readTimeout 3000ms)应正常返回,不得提前误杀
|
||
RestClient client = clientWithReadTimeout(3_000);
|
||
client.get().uri(baseUrl() + "/slow?ms=500").retrieve().toBodilessEntity();
|
||
}
|
||
|
||
@Test
|
||
void timeoutRootIsTimeoutException() {
|
||
RestClient client = clientWithReadTimeout(200);
|
||
ResourceAccessException ex = assertThrows(ResourceAccessException.class,
|
||
() -> client.get().uri(baseUrl() + "/slow?ms=2000").retrieve().toBodilessEntity());
|
||
// JDK 在负载下可能抛 java.util.concurrent.TimeoutException 或 java.net.http.HttpTimeoutException,
|
||
// 两者同属"超时";按超时语义断言而非绑定具体类型。
|
||
assertTrue(isTimeoutRoot(ex), "根因应为超时异常,实际 " + root(ex));
|
||
}
|
||
|
||
@Test
|
||
void readTimeoutScalesWithConfiguredValue() {
|
||
long elapsedShort = timeToFail(150);
|
||
long elapsedLong = timeToFail(700);
|
||
assertTrue(elapsedShort < elapsedLong,
|
||
"更小的 readTimeout 应更快失败:" + elapsedShort + "ms vs " + elapsedLong + "ms");
|
||
assertTrue(elapsedLong < 2_500, "较长配置也应在合理范围内失败");
|
||
}
|
||
|
||
@Test
|
||
void requestFactoryClampsNonPositiveReadTimeout() {
|
||
// HttpClientPool.requestFactory 把非正值钳制到最小 1ms,慢端点极快失败
|
||
RestClient client = RestClient.builder()
|
||
.requestFactory(HttpClientPool.requestFactory(-5))
|
||
.build();
|
||
long startedAt = System.nanoTime();
|
||
assertThrows(ResourceAccessException.class,
|
||
() -> client.get().uri(baseUrl() + "/slow?ms=2000").retrieve().toBodilessEntity());
|
||
long elapsedMs = (System.nanoTime() - startedAt) / 1_000_000;
|
||
assertTrue(elapsedMs < 800, "钳制到 1ms 后应极快失败,实际 " + elapsedMs + "ms");
|
||
}
|
||
|
||
@Test
|
||
void connectTimeoutFiresOnUnreachableHost() {
|
||
// 本机不可达地址(TEST-NET-1 保留段)+ 短连接超时 → 在限定时间内以传输错误失败
|
||
HttpClientProperties props = new HttpClientProperties();
|
||
props.setConnectTimeoutMillis(500);
|
||
// 地址取 127.0.0.1:1(本机未监听端口,连接立即被拒绝):原先用 TEST-NET-1 的 192.0.2.1,
|
||
// 在装有 socks/透明代理的开发机上会被代理接管并等到代理自身超时(实测 63s),
|
||
// 使断言依赖运行机器的网络环境而假失败。
|
||
HttpClient client = HttpClient.newBuilder()
|
||
.connectTimeout(Duration.ofMillis(props.effectiveConnectTimeoutMillis()))
|
||
.build();
|
||
org.springframework.http.client.JdkClientHttpRequestFactory factory =
|
||
new org.springframework.http.client.JdkClientHttpRequestFactory(client);
|
||
RestClient rest = RestClient.builder().requestFactory(factory).build();
|
||
|
||
long startedAt = System.nanoTime();
|
||
try {
|
||
rest.get().uri("http://127.0.0.1:1/").retrieve().toBodilessEntity();
|
||
fail("不可达地址不应成功");
|
||
} catch (ResourceAccessException expected) {
|
||
// 预期:连接被拒绝或快速不可达,均属传输错误
|
||
}
|
||
long elapsedMs = (System.nanoTime() - startedAt) / 1_000_000;
|
||
assertTrue(elapsedMs < 3_000, "连接超时应在限定时间内失败,实际 " + elapsedMs + "ms");
|
||
}
|
||
|
||
@Test
|
||
void sharedPoolAndNamespaceDefaultsAreOperative() {
|
||
// 连接超时来源=共享 HttpClient(10s);命名空间默认 read=60s 经 requestFactory 生效且不误伤快响应
|
||
assertEquals(Duration.ofSeconds(10), HttpClientPool.sharedHttpClient().connectTimeout().orElseThrow(),
|
||
"共享 HttpClient 连接超时为 10s");
|
||
HttpClientProperties props = new HttpClientProperties();
|
||
assertEquals(60_000L, props.effectiveReadTimeoutMillis());
|
||
RestClient client = clientWithReadTimeout((int) props.effectiveReadTimeoutMillis());
|
||
client.get().uri(baseUrl() + "/fast").retrieve().toBodilessEntity();
|
||
}
|
||
|
||
private static long timeToFail(int readTimeoutMillis) {
|
||
RestClient client = clientWithReadTimeout(readTimeoutMillis);
|
||
long startedAt = System.nanoTime();
|
||
assertThrows(ResourceAccessException.class,
|
||
() -> client.get().uri(baseUrl() + "/slow?ms=5000").retrieve().toBodilessEntity());
|
||
return (System.nanoTime() - startedAt) / 1_000_000;
|
||
}
|
||
|
||
private static Throwable root(Throwable error) {
|
||
Throwable current = error;
|
||
while (current.getCause() != null && current.getCause() != current) {
|
||
current = current.getCause();
|
||
}
|
||
return current;
|
||
}
|
||
|
||
private static boolean isTimeoutRoot(Throwable error) {
|
||
return root(error) instanceof TimeoutException
|
||
|| root(error) instanceof java.net.http.HttpTimeoutException;
|
||
}
|
||
}
|