This commit is contained in:
Ivan
2021-03-05 18:12:34 +08:00
parent 7913184174
commit 8901c49bb3
2 changed files with 53 additions and 24 deletions

View File

@@ -38,8 +38,8 @@ describe('PipelineTasksService', () => {
beforeEach(async () => {
redisClient = (() => ({
set: jest.fn().mockImplementation(async () => 'OK'),
incr: jest.fn().mockImplementation(async () => 1),
decr: jest.fn().mockImplementation(async () => 0),
del: jest.fn().mockImplementation(async () => 'test'),
get: jest.fn().mockImplementation(async () => 'test'),
lpush: jest.fn().mockImplementation(async () => 1),
rpop: jest.fn().mockImplementation(async () => JSON.stringify(getTask())),
}))() as any;
@@ -103,6 +103,9 @@ describe('PipelineTasksService', () => {
const save = jest
.spyOn(taskRepository, 'save')
.mockImplementation(async (data: any) => data);
jest
.spyOn(service, 'doNextTask')
.mockImplementation(async () => undefined);
await service.addTask({ pipelineId: 'test', commit: 'test', units: [] }),
expect(save.mock.calls[0][0]).toMatchObject({
pipelineId: 'test',
@@ -113,6 +116,9 @@ describe('PipelineTasksService', () => {
it('add task', async () => {
const lpush = jest.spyOn(redisClient, 'lpush');
const doNextTask = jest.spyOn(service, 'doNextTask');
jest
.spyOn(service, 'doNextTask')
.mockImplementation(async () => undefined);
await service.addTask({ pipelineId: 'test', commit: 'test', units: [] });
expect(typeof lpush.mock.calls[0][1] === 'string').toBeTruthy();
expect(JSON.parse(lpush.mock.calls[0][1] as string)).toMatchObject({
@@ -127,15 +133,24 @@ describe('PipelineTasksService', () => {
describe('doNextTask', () => {
it('add task to queue', async () => {
const decr = jest.spyOn(redisClient, 'decr');
let lckValue: string;
const set = jest
.spyOn(redisClient, 'set')
.mockImplementation(async (...args) => (lckValue = args[3] as string));
const get = jest
.spyOn(redisClient, 'get')
.mockImplementation(async () => lckValue);
const del = jest.spyOn(redisClient, 'del');
const rpop = jest.spyOn(redisClient, 'rpop');
const add = jest.spyOn(taskQueue, 'add');
await service.doNextTask(getBasePipeline());
expect(add).toHaveBeenCalledWith(getTask());
expect(decr).toHaveBeenCalledTimes(1);
expect(set).toHaveBeenCalledTimes(1);
expect(rpop).toHaveBeenCalledTimes(1);
expect(get).toHaveBeenCalledTimes(1);
expect(del).toHaveBeenCalledTimes(1);
});
it('pipeline is busy', async () => {
let remainTimes = 3;
@@ -155,15 +170,21 @@ describe('PipelineTasksService', () => {
expect(add).toHaveBeenCalledWith(getTask());
});
it('pipeline always busy and timeout', async () => {
const incr = jest.spyOn(redisClient, 'incr').mockImplementation(() => 3);
const decr = jest.spyOn(redisClient, 'decr');
const set = jest
.spyOn(redisClient, 'set')
.mockImplementation(async () => {
throw new Error();
});
const get = jest.spyOn(redisClient, 'get');
const del = jest.spyOn(redisClient, 'del');
await expect(
service.doNextTask(getBasePipeline()),
).rejects.toBeInstanceOf(LockFailedException);
expect(decr).toHaveBeenCalledTimes(5);
expect(incr).toHaveBeenCalledTimes(5);
expect(set).toHaveBeenCalledTimes(5);
expect(get).toHaveBeenCalledTimes(0);
expect(del).toHaveBeenCalledTimes(0);
}, 15_000);
});
});