- 年龄门槛 5->30 分钟,覆盖建波后订单绑定延迟的分钟级空窗 - 判空新增波次索引表交叉校验:订单表与索引表双空判为未绑定中间态不终结;索引有行时抽样核对识别读偏 - 候选条件与终结 UPDATE 改用 UTC_TIMESTAMP,与 create_time UTC 存储口径显式一致 - 调度执行日志补 unbound/misread/probeFailed 统计 - 测试 9->15 用例(双空/读偏/正常离表/查询失败/抽样失败/时区口径)
400 lines
15 KiB
JavaScript
400 lines
15 KiB
JavaScript
'use strict';
|
||
|
||
const assert = require('node:assert/strict');
|
||
|
||
const WaveBatchClosureService = require('../../../../app/service/systems/wave_batch_closure');
|
||
|
||
const CACHE_KEY = 'schedule:sync_finished_wave_batch:empty_confirm';
|
||
|
||
/**
|
||
* 构造波次自动终结测试上下文。
|
||
*
|
||
* @param {Object} [options={}] 测试选项。
|
||
* @param {Array<Object>} [options.candidateRows] 候选波次行。
|
||
* @param {Array<number>} [options.wavesWithOrders] 待处理表里仍有订单的波次ID。
|
||
* @param {boolean} [options.queryShouldFail] 待处理表查询是否抛错。
|
||
* @param {boolean} [options.cacheUnavailable] 是否禁用缓存(模拟 Redis 不可用)。
|
||
* @param {Array<number>} [options.wavesWithIndex] 波次索引表里有行的波次ID(默认全部候选,
|
||
* 模拟"订单离表但索引残留"的正常清理目标形态)。
|
||
* @param {boolean} [options.indexQueryShouldFail] 波次索引表查询是否抛错。
|
||
* @param {number} [options.probeCount] 索引抽样核对返回的订单表命中数(默认 0)。
|
||
* @param {boolean} [options.probeShouldFail] 索引抽样核对查询是否抛错。
|
||
* @return {{ service: Object, captures: Object, cacheStore: Map }} 测试对象。
|
||
*/
|
||
function createHarness(options = {}) {
|
||
const {
|
||
candidateRows = [],
|
||
wavesWithOrders = [],
|
||
queryShouldFail = false,
|
||
cacheUnavailable = false,
|
||
wavesWithIndex = candidateRows.map(row => Number(row && row.wave_id || 0)).filter(waveId => waveId > 0),
|
||
indexQueryShouldFail = false,
|
||
probeCount = 0,
|
||
probeShouldFail = false,
|
||
} = options;
|
||
|
||
const captures = {
|
||
sqlList: [],
|
||
updateWaveIdList: [],
|
||
updateReplacementList: [],
|
||
infoLogList: [],
|
||
warnLogList: [],
|
||
};
|
||
const cacheStore = new Map();
|
||
const wavesWithOrdersSet = new Set(wavesWithOrders);
|
||
const wavesWithIndexSet = new Set(wavesWithIndex);
|
||
|
||
const app = { config: {} };
|
||
if (!cacheUnavailable) {
|
||
app.getCacheAsync = async cacheKey => (cacheStore.has(cacheKey) ? cacheStore.get(cacheKey) : null);
|
||
app.setCacheAsync = async (cacheKey, data) => {
|
||
cacheStore.set(cacheKey, data);
|
||
};
|
||
}
|
||
|
||
const ctx = {
|
||
app,
|
||
model: {
|
||
QueryTypes: { SELECT: 'SELECT', UPDATE: 'UPDATE' },
|
||
OrderDeliveryFactory: {
|
||
generateTableName(warehouseId) {
|
||
return `wms_order_delivery_${warehouseId}`;
|
||
},
|
||
},
|
||
async query(sql, queryOptions = {}) {
|
||
const normalizedSql = String(sql || '').replace(/\s+/g, ' ').trim();
|
||
const replacements = queryOptions.replacements || {};
|
||
captures.sqlList.push(normalizedSql);
|
||
|
||
if (/^UPDATE\s+wms_wave_batch/i.test(normalizedSql)) {
|
||
const waveIds = Array.isArray(replacements.waveIds) ? replacements.waveIds : [];
|
||
captures.updateWaveIdList.push(...waveIds);
|
||
captures.updateReplacementList.push(replacements);
|
||
// 真实 SQL 带 wave_status IN (0,1) 守卫:这里按"全部命中"模拟最理想情况
|
||
return [ undefined, { affectedRows: waveIds.length }];
|
||
}
|
||
|
||
if (/SELECT DISTINCT wave_id[\s\S]*FROM\s+wms_order_wave_index/i.test(normalizedSql)) {
|
||
if (indexQueryShouldFail) {
|
||
throw new Error('mock wave index query failed');
|
||
}
|
||
const waveIds = Array.isArray(replacements.waveIds) ? replacements.waveIds : [];
|
||
return waveIds
|
||
.filter(waveId => wavesWithIndexSet.has(waveId))
|
||
.map(waveId => ({ wave_id: waveId }));
|
||
}
|
||
|
||
if (/SELECT DISTINCT wave_id/i.test(normalizedSql)) {
|
||
if (queryShouldFail) {
|
||
throw new Error('mock order table query failed');
|
||
}
|
||
const waveIds = Array.isArray(replacements.waveIds) ? replacements.waveIds : [];
|
||
return waveIds
|
||
.filter(waveId => wavesWithOrdersSet.has(waveId))
|
||
.map(waveId => ({ wave_id: waveId }));
|
||
}
|
||
|
||
if (/SELECT COUNT\(\*\) AS probe_count/i.test(normalizedSql)) {
|
||
if (probeShouldFail) {
|
||
throw new Error('mock probe query failed');
|
||
}
|
||
return [{ probe_count: probeCount }];
|
||
}
|
||
|
||
if (/FROM\s+wms_wave_batch/i.test(normalizedSql)) {
|
||
return candidateRows;
|
||
}
|
||
|
||
return [];
|
||
},
|
||
},
|
||
logger: {
|
||
info(message) {
|
||
captures.infoLogList.push(String(message));
|
||
},
|
||
warn(message) {
|
||
captures.warnLogList.push(String(message));
|
||
},
|
||
error() {},
|
||
},
|
||
};
|
||
|
||
return {
|
||
service: new WaveBatchClosureService(ctx),
|
||
captures,
|
||
cacheStore,
|
||
};
|
||
}
|
||
|
||
/**
|
||
* 生成候选波次行。
|
||
*
|
||
* @param {number} waveId 波次ID。
|
||
* @param {number} [warehouseId=1000028] 仓库ID。
|
||
* @return {Object} 候选行。
|
||
*/
|
||
function buildCandidate(waveId, warehouseId = 1000028) {
|
||
return {
|
||
wave_id: waveId,
|
||
wave_no: `B${waveId}`,
|
||
warehouse_id: warehouseId,
|
||
wave_status: 1,
|
||
total_orders: 20,
|
||
};
|
||
}
|
||
|
||
describe('test/app/service/systems/wave_batch_closure.test.js', () => {
|
||
it('首轮判空不终结,只登记待确认(避免瞬时空表误关)', async () => {
|
||
const { service, captures, cacheStore } = createHarness({
|
||
candidateRows: [ buildCandidate(1777888535813505) ],
|
||
});
|
||
|
||
const result = await service.closeFinishedWaves();
|
||
|
||
assert.equal(result.empty_count, 1);
|
||
assert.equal(result.confirmed_count, 0);
|
||
assert.equal(result.closed_count, 0);
|
||
assert.equal(result.pending_confirm_count, 1);
|
||
assert.deepEqual(result.pending_confirm_wave_ids, [ 1777888535813505 ]);
|
||
assert.equal(captures.updateWaveIdList.length, 0);
|
||
assert.deepEqual(cacheStore.get(CACHE_KEY).waveIds, [ 1777888535813505 ]);
|
||
});
|
||
|
||
it('连续两轮判空且间隔达标后才终结(受 wave_status/total_orders 守卫)', async () => {
|
||
const waveId = 1777888535813505;
|
||
const { service, captures, cacheStore } = createHarness({
|
||
candidateRows: [ buildCandidate(waveId) ],
|
||
});
|
||
// 预置上一轮判空结果(轮次时间早于确认间隔)
|
||
cacheStore.set(CACHE_KEY, { roundAt: Date.now() - 60000, waveIds: [ waveId ] });
|
||
|
||
const result = await service.closeFinishedWaves();
|
||
|
||
assert.equal(result.confirmed_count, 1);
|
||
assert.equal(result.closed_count, 1);
|
||
assert.equal(result.pending_confirm_count, 0);
|
||
assert.deepEqual(captures.updateWaveIdList, [ waveId ]);
|
||
assert.deepEqual(result.closed_waves, [{
|
||
waveId,
|
||
waveNo: `B${waveId}`,
|
||
warehouseId: 1000028,
|
||
}]);
|
||
|
||
const updateSql = captures.sqlList.find(sql => /^UPDATE\s+wms_wave_batch/i.test(sql));
|
||
assert.ok(updateSql, '应执行终结 UPDATE');
|
||
assert.ok(/wave_status IN \(:waveStatuses\)/i.test(updateSql), '终结 UPDATE 需带 wave_status 守卫');
|
||
assert.ok(/total_orders > 0/i.test(updateSql), '终结 UPDATE 需带 total_orders > 0 守卫');
|
||
assert.deepEqual(captures.updateReplacementList[0].waveStatuses, [ 0, 1 ]);
|
||
assert.equal(captures.updateReplacementList[0].closedStatus, 5);
|
||
});
|
||
|
||
it('上一轮判空但本轮订单已回来时不终结', async () => {
|
||
const waveId = 1777888535813505;
|
||
const { service, captures, cacheStore } = createHarness({
|
||
candidateRows: [ buildCandidate(waveId) ],
|
||
wavesWithOrders: [ waveId ],
|
||
});
|
||
cacheStore.set(CACHE_KEY, { roundAt: Date.now() - 60000, waveIds: [ waveId ] });
|
||
|
||
const result = await service.closeFinishedWaves();
|
||
|
||
assert.equal(result.empty_count, 0);
|
||
assert.equal(result.confirmed_count, 0);
|
||
assert.equal(captures.updateWaveIdList.length, 0);
|
||
});
|
||
|
||
it('两次判空间隔不足时不终结,且不刷新首轮时间(避免调度过密饿死确认)', async () => {
|
||
const waveId = 1777888535813505;
|
||
const staleRoundAt = Date.now() - 5000;
|
||
const { service, captures, cacheStore } = createHarness({
|
||
candidateRows: [ buildCandidate(waveId) ],
|
||
});
|
||
cacheStore.set(CACHE_KEY, { roundAt: staleRoundAt, waveIds: [ waveId ] });
|
||
|
||
const result = await service.closeFinishedWaves();
|
||
|
||
assert.equal(result.confirmed_count, 0);
|
||
assert.equal(result.pending_confirm_count, 1);
|
||
assert.equal(captures.updateWaveIdList.length, 0);
|
||
assert.equal(cacheStore.get(CACHE_KEY).roundAt, staleRoundAt);
|
||
});
|
||
|
||
it('confirmMinIntervalMs=0 时跳过二次确认、首轮即终结(兼容/手动强制口径)', async () => {
|
||
const waveId = 1777888535813505;
|
||
const { service, captures } = createHarness({
|
||
candidateRows: [ buildCandidate(waveId) ],
|
||
});
|
||
|
||
const result = await service.closeFinishedWaves({ confirmMinIntervalMs: 0 });
|
||
|
||
assert.equal(result.confirmed_count, 1);
|
||
assert.equal(result.closed_count, 1);
|
||
assert.deepEqual(captures.updateWaveIdList, [ waveId ]);
|
||
});
|
||
|
||
it('dryRun 只盘点不写库、不读写确认状态', async () => {
|
||
const waveId = 1777888535813505;
|
||
const { service, captures, cacheStore } = createHarness({
|
||
candidateRows: [ buildCandidate(waveId) ],
|
||
});
|
||
|
||
const result = await service.closeFinishedWaves({ dryRun: true });
|
||
|
||
assert.equal(result.dryRun, true);
|
||
assert.equal(result.empty_count, 1);
|
||
assert.equal(result.confirmed_count, 1);
|
||
assert.equal(result.closed_count, 0);
|
||
assert.equal(captures.updateWaveIdList.length, 0);
|
||
assert.equal(cacheStore.size, 0);
|
||
});
|
||
|
||
it('待处理表查询失败时跳过该仓库,不产生误关', async () => {
|
||
const { service, captures } = createHarness({
|
||
candidateRows: [ buildCandidate(1777888535813505) ],
|
||
queryShouldFail: true,
|
||
});
|
||
|
||
const result = await service.closeFinishedWaves({ confirmMinIntervalMs: 0 });
|
||
|
||
assert.equal(result.empty_count, 0);
|
||
assert.equal(result.confirmed_count, 0);
|
||
assert.equal(captures.updateWaveIdList.length, 0);
|
||
assert.ok(captures.warnLogList.some(message => /查询仓库 1000028 待处理表失败/.test(message)));
|
||
});
|
||
|
||
it('缓存不可用时本轮不终结任何波次(宁可漏关,不可误关)', async () => {
|
||
const waveId = 1777888535813505;
|
||
const { service, captures } = createHarness({
|
||
candidateRows: [ buildCandidate(waveId) ],
|
||
cacheUnavailable: true,
|
||
});
|
||
|
||
const result = await service.closeFinishedWaves();
|
||
|
||
assert.equal(result.empty_count, 1);
|
||
assert.equal(result.confirmed_count, 0);
|
||
assert.equal(result.pending_confirm_count, 1);
|
||
assert.equal(captures.updateWaveIdList.length, 0);
|
||
assert.ok(captures.warnLogList.some(message => /二次确认状态缓存不可用/.test(message)));
|
||
});
|
||
|
||
it('多仓库分别按各自待处理表判定,只终结确认过的仓库波次', async () => {
|
||
const waveIdA = 1777888535813505;
|
||
const waveIdB = 1777888535764266;
|
||
const { service, captures, cacheStore } = createHarness({
|
||
candidateRows: [ buildCandidate(waveIdA, 1000028), buildCandidate(waveIdB, 100000) ],
|
||
// 仓库 100000 的波次仍有订单
|
||
wavesWithOrders: [ waveIdB ],
|
||
});
|
||
cacheStore.set(CACHE_KEY, { roundAt: Date.now() - 60000, waveIds: [ waveIdA, waveIdB ] });
|
||
|
||
const result = await service.closeFinishedWaves();
|
||
|
||
assert.deepEqual(result.warehouse_ids.sort(), [ 100000, 1000028 ]);
|
||
assert.equal(result.empty_count, 1);
|
||
assert.deepEqual(captures.updateWaveIdList, [ waveIdA ]);
|
||
});
|
||
|
||
it('订单表与索引表双空(建波未完成中间态)时不终结', async () => {
|
||
const waveId = 1777888535853195;
|
||
const { service, captures } = createHarness({
|
||
candidateRows: [ buildCandidate(waveId) ],
|
||
wavesWithIndex: [],
|
||
});
|
||
|
||
const result = await service.closeFinishedWaves({ confirmMinIntervalMs: 0 });
|
||
|
||
assert.equal(result.empty_count, 0);
|
||
assert.equal(result.unbound_count, 1);
|
||
assert.deepEqual(result.unbound_wave_ids, [ waveId ]);
|
||
assert.equal(result.confirmed_count, 0);
|
||
assert.equal(captures.updateWaveIdList.length, 0);
|
||
// 双空形态不做抽样核对
|
||
assert.equal(captures.sqlList.some(sql => /probe_count/i.test(sql)), false);
|
||
assert.ok(captures.warnLogList.some(message => /双空/.test(message)));
|
||
});
|
||
|
||
it('索引有行但抽样核对命中订单(读偏)时不终结', async () => {
|
||
const waveId = 1777888535813505;
|
||
const { service, captures } = createHarness({
|
||
candidateRows: [ buildCandidate(waveId) ],
|
||
wavesWithIndex: [ waveId ],
|
||
probeCount: 3,
|
||
});
|
||
|
||
const result = await service.closeFinishedWaves({ confirmMinIntervalMs: 0 });
|
||
|
||
assert.equal(result.empty_count, 0);
|
||
assert.equal(result.misread_count, 1);
|
||
assert.deepEqual(result.misread_wave_ids, [ waveId ]);
|
||
assert.equal(captures.updateWaveIdList.length, 0);
|
||
assert.ok(captures.warnLogList.some(message => /读偏/.test(message)));
|
||
});
|
||
|
||
it('索引有行且抽样为 0(订单真离表)按正常清理口径终结', async () => {
|
||
const waveId = 1777888535813505;
|
||
const { service, captures } = createHarness({
|
||
candidateRows: [ buildCandidate(waveId) ],
|
||
wavesWithIndex: [ waveId ],
|
||
probeCount: 0,
|
||
});
|
||
|
||
const result = await service.closeFinishedWaves({ confirmMinIntervalMs: 0 });
|
||
|
||
assert.equal(result.empty_count, 1);
|
||
assert.equal(result.unbound_count, 0);
|
||
assert.equal(result.misread_count, 0);
|
||
assert.equal(result.confirmed_count, 1);
|
||
assert.deepEqual(captures.updateWaveIdList, [ waveId ]);
|
||
});
|
||
|
||
it('波次索引表查询失败时跳过该仓库,不产生误关', async () => {
|
||
const { service, captures } = createHarness({
|
||
candidateRows: [ buildCandidate(1777888535813505) ],
|
||
indexQueryShouldFail: true,
|
||
});
|
||
|
||
const result = await service.closeFinishedWaves({ confirmMinIntervalMs: 0 });
|
||
|
||
assert.equal(result.empty_count, 0);
|
||
assert.equal(captures.updateWaveIdList.length, 0);
|
||
assert.ok(captures.warnLogList.some(message => /波次索引表失败/.test(message)));
|
||
});
|
||
|
||
it('索引抽样核对失败时本轮不终结该波次(宁可漏关)', async () => {
|
||
const waveId = 1777888535813505;
|
||
const { service, captures } = createHarness({
|
||
candidateRows: [ buildCandidate(waveId) ],
|
||
wavesWithIndex: [ waveId ],
|
||
probeShouldFail: true,
|
||
});
|
||
|
||
const result = await service.closeFinishedWaves({ confirmMinIntervalMs: 0 });
|
||
|
||
assert.equal(result.empty_count, 0);
|
||
assert.equal(result.probe_failed_count, 1);
|
||
assert.deepEqual(result.probe_failed_wave_ids, [ waveId ]);
|
||
assert.equal(captures.updateWaveIdList.length, 0);
|
||
});
|
||
|
||
it('候选查询与终结 UPDATE 使用 UTC_TIMESTAMP 口径(与 create_time UTC 存储一致)', async () => {
|
||
const waveId = 1777888535813505;
|
||
const { service, captures, cacheStore } = createHarness({
|
||
candidateRows: [ buildCandidate(waveId) ],
|
||
wavesWithIndex: [ waveId ],
|
||
});
|
||
cacheStore.set(CACHE_KEY, { roundAt: Date.now() - 60000, waveIds: [ waveId ] });
|
||
|
||
await service.closeFinishedWaves();
|
||
|
||
const candidateSql = captures.sqlList.find(sql => /FROM\s+wms_wave_batch/i.test(sql));
|
||
assert.ok(candidateSql, '应执行候选查询');
|
||
assert.ok(/UTC_TIMESTAMP\(\)/i.test(candidateSql), '候选查询需使用 UTC_TIMESTAMP');
|
||
assert.ok(!/NOW\(\)/i.test(candidateSql), '候选查询不应再使用 NOW()');
|
||
|
||
const updateSql = captures.sqlList.find(sql => /^UPDATE\s+wms_wave_batch/i.test(sql));
|
||
assert.ok(updateSql, '应执行终结 UPDATE');
|
||
assert.ok(/UTC_TIMESTAMP\(\)/i.test(updateSql), '终结 UPDATE 需使用 UTC_TIMESTAMP');
|
||
});
|
||
});
|