Skip to content

Commit

Permalink
Added some unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mikecote committed Mar 21, 2023
1 parent 4fbf8bd commit 4e296e9
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 1 deletion.
107 changes: 107 additions & 0 deletions x-pack/plugins/task_manager/server/task_running/task_runner.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1499,6 +1499,113 @@ describe('TaskManagerRunner', () => {
});
});

describe('isAdHocTaskAndOutOfAttempts', () => {
it(`should return false if the task doesn't have a schedule`, async () => {
const { runner } = await pendingStageSetup({
instance: {
id: 'foo',
taskType: 'testbar',
},
});

expect(runner.isAdHocTaskAndOutOfAttempts).toEqual(false);
});

it(`should return false if the recurring task still has attempts remaining`, async () => {
const { runner } = await pendingStageSetup({
instance: {
id: 'foo',
taskType: 'testbar',
attempts: 4,
},
});

expect(runner.isAdHocTaskAndOutOfAttempts).toEqual(false);
});

it(`should return true if the recurring task is out of attempts`, async () => {
const { runner } = await pendingStageSetup({
instance: {
id: 'foo',
taskType: 'testbar',
attempts: 5,
},
});

expect(runner.isAdHocTaskAndOutOfAttempts).toEqual(true);
});
});

describe('removeTask()', () => {
it(`should remove the task saved-object`, async () => {
const { runner, store } = await readyToRunStageSetup({
instance: {
id: 'foo',
taskType: 'testbar',
},
});

await runner.run();
await runner.removeTask();
expect(store.remove).toHaveBeenCalledWith('foo');
});

it(`should call the task cleanup function if defined`, async () => {
const cleanupFn = jest.fn();
const { runner } = await readyToRunStageSetup({
instance: {
id: 'foo',
taskType: 'testbar2',
},
definitions: {
testbar2: {
title: 'Bar!',
createTaskRunner: () => ({
async run() {
return { state: {} };
},
cancel: jest.fn(),
cleanup: cleanupFn,
}),
},
},
});

// Remove task is called after run() with the this.task object defined
await runner.run();
expect(cleanupFn).toHaveBeenCalledTimes(1);
});

it(`doesn't throw an error if the cleanup function throws an error`, async () => {
const cleanupFn = jest.fn().mockRejectedValue(new Error('Fail'));
const { runner, logger } = await readyToRunStageSetup({
instance: {
id: 'foo',
taskType: 'testbar2',
},
definitions: {
testbar2: {
title: 'Bar!',
createTaskRunner: () => ({
async run() {
return { state: {} };
},
cancel: jest.fn(),
cleanup: cleanupFn,
}),
},
},
});

// Remove task is called after run() with the this.task object defined
await runner.run();
expect(cleanupFn).toHaveBeenCalledTimes(1);
expect(logger.error).toHaveBeenCalledWith(
`Error encountered when running onTaskRemoved() hook for testbar2 "foo": Fail`
);
});
});

interface TestOpts {
instance?: Partial<ConcreteTaskInstance>;
definitions?: TaskDefinitionRegistry;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -766,7 +766,7 @@ export default function ({ getService }: FtrProviderContext) {
});
});

it('should flag non-recurring task as failed and delete the task if it is still running but maxAttempts has been reached', async () => {
it('should delete the task if it is still running but maxAttempts has been reached', async () => {
await scheduleTask({
taskType: 'sampleOneTimeTaskThrowingError',
params: {},
Expand Down

0 comments on commit 4e296e9

Please sign in to comment.