oms/backend/test/app/service/orderWms.test.js
liufumingren 315d2cf96a 调整订单推送队列并发与扫描周期
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-11-23 15:04:26 +08:00

88 lines
2.1 KiB
JavaScript

'use strict';
const { assert } = require('egg-mock/bootstrap');
const OrderWmsService = require('../../../app/service/orderWms');
function createService(rows, pushOne) {
const service = Object.create(OrderWmsService.prototype);
service.app = {
Sequelize: {
Op: {
in: Symbol('in'),
or: Symbol('or'),
lt: Symbol('lt'),
},
},
};
service.ctx = {
model: {
OrderStatusQueued: {
async findAll(options) {
assert(options.limit === 50);
return rows;
},
},
},
service: {
cache: {
async getEnabledPrincipalIds() {
return [ 1 ];
},
},
orderStatus: {
markPushFailed() {
return Promise.resolve();
},
},
},
logger: {
error() {},
},
};
service.pushOne = pushOne;
return service;
}
describe('test/app/service/orderWms.test.js', () => {
it('should push the pending batch concurrently up to its limit', async () => {
const rows = Array.from({ length: 50 }, (_, index) => ({ id: index + 1, principal_id: 1 }));
let active = 0;
let maxActive = 0;
const service = createService(rows, async () => {
active++;
maxActive = Math.max(maxActive, active);
await new Promise(resolve => setTimeout(resolve, 10));
active--;
return { ok: true };
});
const stat = await service.processPending();
assert(stat.scanned === 50);
assert(stat.pushed === 50);
assert(maxActive === 50);
});
it('should wait for all in-flight pushes before surfacing an error', async () => {
let secondFinished = false;
const rows = [
{ id: 1, principal_id: 1 },
{ id: 2, principal_id: 1 },
];
const service = createService(rows, async id => {
if (id === 1) throw new Error('push failed');
await new Promise(resolve => setTimeout(resolve, 10));
secondFinished = true;
return { ok: true };
});
let error;
try {
await service.processPending();
} catch (err) {
error = err;
}
assert(error instanceof AggregateError);
assert(secondFinished);
});
});