Skip to content

Commit

Permalink
use task store
Browse files Browse the repository at this point in the history
  • Loading branch information
Liza K committed Jan 7, 2021
1 parent 056a17b commit 762bcc7
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,40 +5,40 @@
*/

import uuid from 'uuid';
import { taskManagerMock } from '../../../task_manager/server/mocks';
import { SavedObjectsErrorHelpers } from '../../../../../src/core/server';
import { deleteTaskIfItExists } from './delete_task_if_it_exists';
import { taskStoreMock } from '../task_store.mock';

describe('deleteTaskIfItExists', () => {
test('removes the task by its ID', async () => {
const tm = taskManagerMock.createStart();
const ts = taskStoreMock.create({});
const id = uuid.v4();

expect(await deleteTaskIfItExists(tm.remove, id)).toBe(undefined);
expect(await deleteTaskIfItExists(ts, id)).toBe(undefined);

expect(tm.remove).toHaveBeenCalledWith(id);
expect(ts.remove).toHaveBeenCalledWith(id);
});

test('handles 404 errors caused by the task not existing', async () => {
const tm = taskManagerMock.createStart();
const ts = taskStoreMock.create({});
const id = uuid.v4();

tm.remove.mockRejectedValue(SavedObjectsErrorHelpers.createGenericNotFoundError('task', id));
ts.remove.mockRejectedValue(SavedObjectsErrorHelpers.createGenericNotFoundError('task', id));

expect(await deleteTaskIfItExists(tm.remove, id)).toBe(undefined);
expect(await deleteTaskIfItExists(ts, id)).toBe(undefined);

expect(tm.remove).toHaveBeenCalledWith(id);
expect(ts.remove).toHaveBeenCalledWith(id);
});

test('throws if any other errro is caused by task removal', async () => {
const tm = taskManagerMock.createStart();
const ts = taskStoreMock.create({});
const id = uuid.v4();

const error = SavedObjectsErrorHelpers.createInvalidVersionError(uuid.v4());
tm.remove.mockRejectedValue(error);
ts.remove.mockRejectedValue(error);

expect(deleteTaskIfItExists(tm.remove, id)).rejects.toBe(error);
expect(deleteTaskIfItExists(ts, id)).rejects.toBe(error);

expect(tm.remove).toHaveBeenCalledWith(id);
expect(ts.remove).toHaveBeenCalledWith(id);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,12 @@
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
import { TaskManagerStartContract } from '../';
import { SavedObjectsErrorHelpers } from '../../../../../src/core/server';
import { TaskStore } from '../task_store';

export async function deleteTaskIfItExists(
removeFn: TaskManagerStartContract['remove'],
taskId: string
) {
export async function deleteTaskIfItExists(taskStore: TaskStore, taskId: string) {
try {
await removeFn(taskId);
await taskStore.remove(taskId);
} catch (err) {
if (!SavedObjectsErrorHelpers.isNotFoundError(err)) {
throw err;
Expand Down
2 changes: 1 addition & 1 deletion x-pack/plugins/task_manager/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ export class TaskManagerPlugin
fetch: (opts: SearchOpts): Promise<FetchResult> => taskStore.fetch(opts),
get: (id: string) => taskStore.get(id),
remove: (id: string) => taskStore.remove(id),
deleteTaskIfItExists: (id: string) => deleteTaskIfItExists(taskStore.remove, id),
deleteTaskIfItExists: (id: string) => deleteTaskIfItExists(taskStore, id),
schedule: (...args) => taskScheduling.schedule(...args),
ensureScheduled: (...args) => taskScheduling.ensureScheduled(...args),
runNow: (...args) => taskScheduling.runNow(...args),
Expand Down

0 comments on commit 762bcc7

Please sign in to comment.