forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
179 additions
and
41 deletions.
There are no files selected for viewing
82 changes: 82 additions & 0 deletions
82
x-pack/plugins/task_manager/server/buffered_task_store.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import uuid from 'uuid'; | ||
import { taskStoreMock } from './task_store.mock'; | ||
import { BufferedTaskStore } from './buffered_task_store'; | ||
import { asErr, asOk } from './lib/result_type'; | ||
import { TaskStatus } from './task'; | ||
|
||
describe('Buffered Task Store', () => { | ||
test('proxies the TaskStore for `maxAttempts` and `remove`', async () => { | ||
const taskStore = taskStoreMock.create({ maxAttempts: 10 }); | ||
taskStore.bulkUpdate.mockResolvedValue([]); | ||
const bufferedStore = new BufferedTaskStore(taskStore); | ||
|
||
expect(bufferedStore.maxAttempts).toEqual(10); | ||
|
||
bufferedStore.remove('1'); | ||
expect(taskStore.remove).toHaveBeenCalledWith('1'); | ||
}); | ||
|
||
describe('update', () => { | ||
test("proxies the TaskStore's `bulkUpdate`", async () => { | ||
const taskStore = taskStoreMock.create({ maxAttempts: 10 }); | ||
const bufferedStore = new BufferedTaskStore(taskStore); | ||
|
||
const task = mockTask(); | ||
|
||
taskStore.bulkUpdate.mockResolvedValue([asOk(task)]); | ||
|
||
expect(await bufferedStore.update(task)).toMatchObject(task); | ||
expect(taskStore.bulkUpdate).toHaveBeenCalledWith([task]); | ||
}); | ||
|
||
test('handles partially successfull bulkUpdates resolving each call appropriately', async () => { | ||
const taskStore = taskStoreMock.create({ maxAttempts: 10 }); | ||
const bufferedStore = new BufferedTaskStore(taskStore); | ||
|
||
const tasks = [mockTask(), mockTask(), mockTask()]; | ||
|
||
taskStore.bulkUpdate.mockResolvedValueOnce([ | ||
asOk(tasks[0]), | ||
asErr(new Error('Oh no, something went terribly wrong')), | ||
asOk(tasks[2]), | ||
]); | ||
|
||
const results = [ | ||
bufferedStore.update(tasks[0]), | ||
bufferedStore.update(tasks[1]), | ||
bufferedStore.update(tasks[2]), | ||
]; | ||
expect(await results[0]).toMatchObject(tasks[0]); | ||
expect(results[1]).rejects.toMatchInlineSnapshot( | ||
`[Error: Oh no, something went terribly wrong]` | ||
); | ||
expect(await results[2]).toMatchObject(tasks[2]); | ||
}); | ||
}); | ||
}); | ||
|
||
function mockTask() { | ||
return { | ||
id: `task_${uuid.v4()}`, | ||
attempts: 0, | ||
schedule: undefined, | ||
params: { hello: 'world' }, | ||
retryAt: null, | ||
runAt: new Date(), | ||
scheduledAt: new Date(), | ||
scope: undefined, | ||
startedAt: null, | ||
state: { foo: 'bar' }, | ||
status: TaskStatus.Idle, | ||
taskType: 'report', | ||
user: undefined, | ||
version: '123', | ||
ownerId: '123', | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import { TaskStore } from './task_store'; | ||
|
||
interface TaskStoreOptions { | ||
maxAttempts?: number; | ||
index?: string; | ||
taskManagerId?: string; | ||
} | ||
export const taskStoreMock = { | ||
create({ maxAttempts = 0, index = '', taskManagerId = '' }: TaskStoreOptions) { | ||
const mocked = ({ | ||
update: jest.fn(), | ||
remove: jest.fn(), | ||
schedule: jest.fn(), | ||
claimAvailableTasks: jest.fn(), | ||
bulkUpdate: jest.fn(), | ||
get: jest.fn(), | ||
getLifecycle: jest.fn(), | ||
fetch: jest.fn(), | ||
maxAttempts, | ||
index, | ||
taskManagerId, | ||
} as unknown) as jest.Mocked<TaskStore>; | ||
return mocked; | ||
}, | ||
}; |