From e9a70711c10ff11c01bdee60ddac77d007ae9551 Mon Sep 17 00:00:00 2001 From: kicer Date: Tue, 1 Sep 2026 17:12:31 +0800 Subject: [PATCH] =?UTF-8?q?fix=20smoke=20[5]:=E7=AD=89=E6=A8=A1=E5=9E=8B?= =?UTF-8?q?=E6=B1=A0=E5=85=A8=E7=A9=BA=E9=97=B2=E5=86=8D=E6=B5=8B=E6=8E=92?= =?UTF-8?q?=E9=98=9F,=E6=96=AD=E8=A8=80=E4=B8=8D=E5=86=8D=E7=A1=AC?= =?UTF-8?q?=E7=BC=96=E7=A0=81ds1/ds2=E7=BB=91=E5=AE=9A=E9=A1=BA=E5=BA=8F;?= =?UTF-8?q?=E5=BC=80=E8=B7=91=E5=89=8D=E6=A3=80=E6=B5=8B9999/8099=E7=AB=AF?= =?UTF-8?q?=E5=8F=A3=E6=AE=8B=E7=95=99=E5=BF=AB=E9=80=9F=E5=A4=B1=E8=B4=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/test/smoke.js | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/app/test/smoke.js b/app/test/smoke.js index 63ad3da..75e0654 100644 --- a/app/test/smoke.js +++ b/app/test/smoke.js @@ -3,6 +3,7 @@ 'use strict'; const { spawn } = require('child_process'); const fs = require('fs'); +const net = require('net'); const path = require('path'); const ROOT = path.resolve(__dirname, '..'); @@ -15,6 +16,17 @@ function ok(cond, name, extra) { } const sleep = ms => new Promise(r => setTimeout(r, ms)); +// 端口占用探测:上次运行崩溃可能残留 mock/server,导致本次测试行为错乱,先快速失败 +function portInUse(port) { + return new Promise(resolve => { + const s = net.connect(port, '127.0.0.1'); + s.setTimeout(500); + s.on('connect', () => { s.destroy(); resolve(true); }); + s.on('error', () => resolve(false)); + s.on('timeout', () => { s.destroy(); resolve(false); }); + }); +} + async function api(method, url, body) { const opt = { method, headers: {} }; if (body) { @@ -40,6 +52,12 @@ async function waitSession(id, predicate, timeoutMs, label) { } async function main() { + for (const p of [9999, 8099]) { + if (await portInUse(p)) { + console.error('端口 ' + p + ' 已被占用(可能是上次测试残留的 mock/server),请先清理: pkill -f "test/(mock-upstream|server.js)"'); + process.exit(3); + } + } // 清理旧数据 fs.rmSync(path.join(ROOT, 'test/tmp'), { recursive: true, force: true }); @@ -122,13 +140,21 @@ async function main() { // ---- 5. 排队调度(ds1/ds2 都占用 → 第三个排队 → 释放后 FIFO 绑定)---- console.log('\n[5] 排队调度'); + // 先等模型池全空闲:前节的 held 租约最长还需 idle_release_ms 才释放, + // 否则本节会话会绑到不同模型,下面「分到不同模型」的断言时机依赖运气 + for (let i = 0; i < 30; i++) { + const pool0 = await api('GET', '/api/models'); + if (pool0.every(m => !m.is_busy)) break; + await sleep(200); + } const a = await api('POST', '/api/sessions', { title: 'A' }); const b = await api('POST', '/api/sessions', { title: 'B' }); const c = await api('POST', '/api/sessions', { title: 'C' }); const da = await waitSession(a.id, x => x.session.status === 'active', 5000, 'A active'); const db = await waitSession(b.id, x => x.session.status === 'active', 5000, 'B active'); const dc = await api('GET', '/api/sessions/' + c.id); - ok(da.session.model_id === 'ds1' && db.session.model_id === 'ds2', '两个会话分到不同模型'); + ok(da.session.model_id && db.session.model_id && da.session.model_id !== db.session.model_id, + '两个会话分到不同模型', da.session.model_id + ' vs ' + db.session.model_id); ok(dc.session.status === 'queued' && dc.queue_pos === 1, '第三个会话排队 position 1'); await api('POST', '/api/sessions/' + a.id + '/stop'); await waitSession(c.id, x => x.session.status === 'active', 6000, 'C 顶替');