- 库存抵扣成本按目标抵扣量幂等更新,作废/退货清 cover_qty;多批次展示按加权均价;商品成本排行纳入库存抵扣成本并改标签为“成本” - 主系统事件:allocated 定向 order_code、returned 等批量状态核对、clamped/全量兜底下调频、失败不计 applied - 订单完成:零价闸门排除已退货行;供应商系统关闭“主数据不卡流程”入口与执行接口 - 迁移 20261080/20261081;补充相关契约/回归测试
123 lines
5.8 KiB
TypeScript
123 lines
5.8 KiB
TypeScript
import { describe, expect, it, vi } from 'vitest';
|
||
import { readFileSync } from 'node:fs';
|
||
import path from 'node:path';
|
||
|
||
/**
|
||
* 规模加固(2026-10-13,开发者问「扛得住 500 家绑码分公司吗」后落地):
|
||
*
|
||
* · **P0**:跨系统任务收进**独立分布式锁**并在回调里 await —— 否则 N 个实例各跑一遍,
|
||
* 出站请求 ×N(原来的 10 分钟限流是进程内变量,挡不住多实例);
|
||
* · **P1b**:拉货并发 6→4 + **错峰**:把 N 家的开始时刻在窗口内均匀铺开,
|
||
* 500 家从"~11 s 内打完 500 次"变成"~2 分钟摊平",峰值从 30–45 QPS 降到个位数。
|
||
*/
|
||
|
||
const importSrc = readFileSync(
|
||
path.join(__dirname, '../src/services/mainSystemOrderImport.ts'),
|
||
'utf8'
|
||
);
|
||
const schedulerSrc = readFileSync(
|
||
path.join(__dirname, '../src/services/scheduledJobs.ts'),
|
||
'utf8'
|
||
);
|
||
|
||
const { planImportSpread, resolveImportSpreadWindowMs } = await import(
|
||
'../src/services/mainSystemOrderImport'
|
||
);
|
||
|
||
describe('P1b:拉货错峰槽位(纯函数)', () => {
|
||
it('① 均匀铺开:槽位在窗口内等距,且顺序确定(同输入两次结果一致)', () => {
|
||
const branches = ['BR003', 'BR002', 'BR010', 'BR007', 'BR001'];
|
||
const a = planImportSpread(branches, { windowMs: 120_000, perBranchMs: 400 });
|
||
const b = planImportSpread(branches, { windowMs: 120_000, perBranchMs: 400 });
|
||
expect(a).toEqual(b);
|
||
expect([...a].map((s) => s.branchCode).sort()).toEqual([...branches].sort());
|
||
// 5 家 × 400 ms = 2000 ms 窗口(远小于 120 s)⇒ 小规模不白等
|
||
expect(a.map((s) => s.atMs)).toEqual([0, 500, 1000, 1500, 2000]);
|
||
});
|
||
|
||
it('② 大规模:500 家用满窗口,等距铺开(间隔 ≈ 窗口/家数)', () => {
|
||
const branches = Array.from({ length: 500 }, (_, i) => `BR${String(i).padStart(3, '0')}`);
|
||
const slots = planImportSpread(branches, { windowMs: 120_000 });
|
||
expect(slots).toHaveLength(500);
|
||
expect(slots[0].atMs).toBe(0);
|
||
expect(slots[slots.length - 1].atMs).toBe(120_000);
|
||
const gaps = slots.slice(1).map((s, i) => s.atMs - slots[i].atMs);
|
||
expect(Math.max(...gaps)).toBeLessThanOrEqual(2 * (120_000 / 499) + 1);
|
||
// 每家的开始时刻互不相同(不挤在同一毫秒)
|
||
expect(new Set(slots.map((s) => s.atMs)).size).toBe(500);
|
||
});
|
||
|
||
it('③ 窗口上限 = 家数 × 每槽宽度(小规模不等满窗口)', () => {
|
||
const slots = planImportSpread(['BR002', 'BR003'], { windowMs: 120_000, perBranchMs: 400 });
|
||
expect(slots.map((s) => s.atMs).sort((x, y) => x - y)).toEqual([0, 800]);
|
||
});
|
||
|
||
it('④ 关闭错峰:windowMs=0 ⇒ 全部 0(回到"尽快跑完")', () => {
|
||
const slots = planImportSpread(['BR002', 'BR003', 'BR004'], { windowMs: 0 });
|
||
expect(slots.every((s) => s.atMs === 0)).toBe(true);
|
||
});
|
||
|
||
it('⑤ 环境变量解析:空/非法 ⇒ 默认 120 s;0 ⇒ 关闭;超上限封顶 1 h', () => {
|
||
expect(resolveImportSpreadWindowMs('')).toBe(120_000);
|
||
expect(resolveImportSpreadWindowMs(undefined)).toBe(120_000);
|
||
expect(resolveImportSpreadWindowMs('abc')).toBe(0);
|
||
expect(resolveImportSpreadWindowMs('0')).toBe(0);
|
||
expect(resolveImportSpreadWindowMs('-5')).toBe(0);
|
||
expect(resolveImportSpreadWindowMs('300')).toBe(300_000);
|
||
expect(resolveImportSpreadWindowMs('99999')).toBe(3_600_000);
|
||
});
|
||
|
||
it('⑥ 接线(源码钉):并发默认 4、按槽位等待、把原并发参数留给运维上调', () => {
|
||
expect(importSrc).toContain('planImportSpread(branches, { windowMs: resolveImportSpreadWindowMs() })');
|
||
expect(importSrc).toMatch(/MAIN_SYSTEM_IMPORT_CONCURRENCY,\s*\n\s*4,/);
|
||
expect(importSrc).toContain('await sleepMs(waitMs)');
|
||
expect(importSrc).toContain('const branchCode = slot.branchCode;');
|
||
});
|
||
});
|
||
|
||
describe('P0:跨系统任务在独立分布式锁内(源码钉)', () => {
|
||
const lockAt = schedulerSrc.indexOf('MAIN_SYSTEM_LOCK_KEY');
|
||
|
||
it('① 存在独立锁常量与 TTL', () => {
|
||
expect(lockAt).toBeGreaterThan(-1);
|
||
expect(schedulerSrc).toContain("const MAIN_SYSTEM_LOCK_KEY = 'main-system-reconcile-tick'");
|
||
expect(schedulerSrc).toMatch(/MAIN_SYSTEM_LOCK_TTL_SEC = 10 \* 60/);
|
||
});
|
||
|
||
it('② 八个跨系统任务都在锁回调里,并且**被 await**(void 起飞=没锁)', () => {
|
||
const callbackAt = schedulerSrc.indexOf('withDistributedLock(MAIN_SYSTEM_LOCK_KEY');
|
||
expect(callbackAt).toBeGreaterThan(-1);
|
||
const body = schedulerSrc.slice(callbackAt, callbackAt + 2000);
|
||
expect(body).toContain('await Promise.all([');
|
||
for (const fn of [
|
||
'runMainSystemImportReconcile()',
|
||
'runPriceQtyRequestReconcile()',
|
||
'runStockCoverBatchPlanBackfill()',
|
||
'runCoverBatchesSweep()',
|
||
'runReturnedCoverResidueSweep()',
|
||
'runDeferredUnitResendSweep()',
|
||
'runReportPushReconcile()',
|
||
'runEmptyMirrorOrderClosure()',
|
||
]) {
|
||
expect(body, `${fn} 必须在锁回调内`).toContain(fn);
|
||
// 旧形态(锁外 void 起飞)必须消失
|
||
expect(schedulerSrc, `${fn} 不得再以 void 形态出现在锁外`).not.toContain(`void ${fn}`);
|
||
}
|
||
});
|
||
|
||
it('④ 全量导入与 30 分钟全量 active 闸门同一口径,不再写死 10 分钟', () => {
|
||
const at = schedulerSrc.indexOf('async function runMainSystemImportReconcile');
|
||
expect(at).toBeGreaterThan(-1);
|
||
const body = schedulerSrc.slice(at, at + 900);
|
||
expect(body).toContain('fullReturnFollowIntervalMs()');
|
||
expect(body).not.toContain('10 * 60 * 1000');
|
||
expect(schedulerSrc).toContain('MAIN_SYSTEM_FULL_ACTIVE_PULL_MINUTES');
|
||
});
|
||
|
||
it('③ 锁回调自身兜异常(不能被 Promise 拒绝掀翻进程)', () => {
|
||
const callbackAt = schedulerSrc.indexOf('withDistributedLock(MAIN_SYSTEM_LOCK_KEY');
|
||
const body = schedulerSrc.slice(callbackAt, callbackAt + 2000);
|
||
expect(body).toContain("console.error('[scheduler] 跨系统任务异常:'");
|
||
});
|
||
});
|