Skip to content

Commit

Permalink
fixed typing in TaskStore
Browse files Browse the repository at this point in the history
  • Loading branch information
gmmorris committed Apr 24, 2020
1 parent 2e22429 commit 44439ed
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 26 deletions.
6 changes: 3 additions & 3 deletions x-pack/plugins/task_manager/server/task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ export interface TaskInstance {
* A task-specific set of parameters, used by the task's run function to tailor
* its work. This is generally user-input, such as { sms: '333-444-2222' }.
*/
// we allow any here as unknown will break current use in otehr plugins
// we allow any here as unknown will break current use in other plugins
// this can be fixed by supporting generics in the future
// eslint-disable-next-line @typescript-eslint/no-explicit-any
params: Record<string, any>;
Expand All @@ -247,7 +247,7 @@ export interface TaskInstance {
* run. If there was no previous run, or if the previous run did not return
* any state, this will be the empy object: {}
*/
// we allow any here as unknown will break current use in otehr plugins
// we allow any here as unknown will break current use in other plugins
// this can be fixed by supporting generics in the future
// eslint-disable-next-line @typescript-eslint/no-explicit-any
state: Record<string, any>;
Expand Down Expand Up @@ -342,7 +342,7 @@ export interface ConcreteTaskInstance extends TaskInstance {
* run. If there was no previous run, or if the previous run did not return
* any state, this will be the empy object: {}
*/
// we allow any here as unknown will break current use in otehr plugins
// we allow any here as unknown will break current use in other plugins
// this can be fixed by supporting generics in the future
// eslint-disable-next-line @typescript-eslint/no-explicit-any
state: Record<string, any>;
Expand Down
54 changes: 31 additions & 23 deletions x-pack/plugins/task_manager/server/task_store.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
/* eslint-disable @typescript-eslint/no-explicit-any */

import _ from 'lodash';
import sinon from 'sinon';
Expand All @@ -16,10 +15,16 @@ import {
TaskInstance,
TaskStatus,
TaskLifecycleResult,
SerializedConcreteTaskInstance,
ConcreteTaskInstance,
} from './task';
import { StoreOpts, OwnershipClaimingOpts, TaskStore, SearchOpts } from './task_store';
import { savedObjectsRepositoryMock } from '../../../../src/core/server/mocks';
import { SavedObjectsSerializer, SavedObjectTypeRegistry } from '../../../../src/core/server';
import {
SavedObjectsSerializer,
SavedObjectTypeRegistry,
SavedObjectAttributes,
} from '../../../../src/core/server';
import { SavedObjectsErrorHelpers } from '../../../../src/core/server/saved_objects/service/lib/errors';
import { asTaskClaimEvent, TaskEvent } from './task_events';
import { asOk, asErr } from './lib/result_type';
Expand Down Expand Up @@ -48,6 +53,7 @@ const serializer = new SavedObjectsSerializer(new SavedObjectTypeRegistry());
beforeEach(() => jest.resetAllMocks());

const mockedDate = new Date('2019-02-12T21:01:22.479Z');
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(global as any).Date = class Date {
constructor() {
return mockedDate;
Expand All @@ -59,9 +65,9 @@ const mockedDate = new Date('2019-02-12T21:01:22.479Z');

describe('TaskStore', () => {
describe('schedule', () => {
async function testSchedule(task: TaskInstance) {
async function testSchedule(task: unknown) {
const callCluster = jest.fn();
savedObjectsClient.create.mockImplementation(async (type: string, attributes: any) => ({
savedObjectsClient.create.mockImplementation(async (type: string, attributes: unknown) => ({
id: 'testid',
type,
attributes,
Expand All @@ -77,7 +83,7 @@ describe('TaskStore', () => {
definitions: taskDefinitions,
savedObjectsRepository: savedObjectsClient,
});
const result = await store.schedule(task);
const result = await store.schedule(task as TaskInstance);

expect(savedObjectsClient.create).toHaveBeenCalledTimes(1);

Expand Down Expand Up @@ -150,14 +156,16 @@ describe('TaskStore', () => {
test('sets runAt to now if not specified', async () => {
await testSchedule({ taskType: 'dernstraight', params: {}, state: {} });
expect(savedObjectsClient.create).toHaveBeenCalledTimes(1);
const attributes: any = savedObjectsClient.create.mock.calls[0][1];
const attributes = savedObjectsClient.create.mock
.calls[0][1] as SerializedConcreteTaskInstance;
expect(new Date(attributes.runAt as string).getTime()).toEqual(mockedDate.getTime());
});

test('ensures params and state are not null', async () => {
await testSchedule({ taskType: 'yawn' } as any);
await testSchedule({ taskType: 'yawn' });
expect(savedObjectsClient.create).toHaveBeenCalledTimes(1);
const attributes: any = savedObjectsClient.create.mock.calls[0][1];
const attributes = savedObjectsClient.create.mock
.calls[0][1] as SerializedConcreteTaskInstance;
expect(attributes.params).toEqual('{}');
expect(attributes.state).toEqual('{}');
});
Expand All @@ -170,8 +178,8 @@ describe('TaskStore', () => {
});

describe('fetch', () => {
async function testFetch(opts?: SearchOpts, hits: any[] = []) {
const callCluster = sinon.spy(async (name: string, params?: any) => ({ hits: { hits } }));
async function testFetch(opts?: SearchOpts, hits: unknown[] = []) {
const callCluster = sinon.spy(async (name: string, params?: unknown) => ({ hits: { hits } }));
const store = new TaskStore({
index: 'tasky',
taskManagerId: '',
Expand Down Expand Up @@ -230,11 +238,11 @@ describe('TaskStore', () => {
claimingOpts,
}: {
opts: Partial<StoreOpts>;
hits?: any[];
hits?: unknown[];
claimingOpts: OwnershipClaimingOpts;
}) {
const versionConflicts = 2;
const callCluster = sinon.spy(async (name: string, params?: any) =>
const callCluster = sinon.spy(async (name: string, params?: unknown) =>
name === 'updateByQuery'
? {
total: hits.length + versionConflicts,
Expand Down Expand Up @@ -267,7 +275,7 @@ describe('TaskStore', () => {
}

test('it returns normally with no tasks when the index does not exist.', async () => {
const callCluster = sinon.spy(async (name: string, params?: any) => ({
const callCluster = sinon.spy(async (name: string, params?: unknown) => ({
total: 0,
updated: 0,
}));
Expand Down Expand Up @@ -746,7 +754,7 @@ if (doc['task.runAt'].size()!=0) {
};

savedObjectsClient.update.mockImplementation(
async (type: string, id: string, attributes: any) => {
async (type: string, id: string, attributes: SavedObjectAttributes) => {
return {
id,
type,
Expand Down Expand Up @@ -1018,7 +1026,7 @@ if (doc['task.runAt'].size()!=0) {

test('emits an event when a task is succesfully claimed by id', async done => {
const { taskManagerId, runAt, tasks } = generateTasks();
const callCluster = sinon.spy(async (name: string, params?: any) =>
const callCluster = sinon.spy(async (name: string, params?: unknown) =>
name === 'updateByQuery'
? {
total: tasks.length,
Expand All @@ -1037,9 +1045,9 @@ if (doc['task.runAt'].size()!=0) {
});

const sub = store.events
.pipe(filter((event: TaskEvent<any, any>) => event.id === 'aaa'))
.pipe(filter((event: TaskEvent<ConcreteTaskInstance, Error>) => event.id === 'aaa'))
.subscribe({
next: (event: TaskEvent<any, any>) => {
next: (event: TaskEvent<ConcreteTaskInstance, Error>) => {
expect(event).toMatchObject(
asTaskClaimEvent(
'aaa',
Expand Down Expand Up @@ -1075,7 +1083,7 @@ if (doc['task.runAt'].size()!=0) {

test('emits an event when a task is succesfully by scheduling', async done => {
const { taskManagerId, runAt, tasks } = generateTasks();
const callCluster = sinon.spy(async (name: string, params?: any) =>
const callCluster = sinon.spy(async (name: string, params?: unknown) =>
name === 'updateByQuery'
? {
total: tasks.length,
Expand All @@ -1094,9 +1102,9 @@ if (doc['task.runAt'].size()!=0) {
});

const sub = store.events
.pipe(filter((event: TaskEvent<any, any>) => event.id === 'bbb'))
.pipe(filter((event: TaskEvent<ConcreteTaskInstance, Error>) => event.id === 'bbb'))
.subscribe({
next: (event: TaskEvent<any, any>) => {
next: (event: TaskEvent<ConcreteTaskInstance, Error>) => {
expect(event).toMatchObject(
asTaskClaimEvent(
'bbb',
Expand Down Expand Up @@ -1132,7 +1140,7 @@ if (doc['task.runAt'].size()!=0) {

test('emits an event when the store fails to claim a required task by id', async done => {
const { taskManagerId, tasks } = generateTasks();
const callCluster = sinon.spy(async (name: string, params?: any) =>
const callCluster = sinon.spy(async (name: string, params?: unknown) =>
name === 'updateByQuery'
? {
total: tasks.length,
Expand All @@ -1151,9 +1159,9 @@ if (doc['task.runAt'].size()!=0) {
});

const sub = store.events
.pipe(filter((event: TaskEvent<any, any>) => event.id === 'ccc'))
.pipe(filter((event: TaskEvent<ConcreteTaskInstance, Error>) => event.id === 'ccc'))
.subscribe({
next: (event: TaskEvent<any, any>) => {
next: (event: TaskEvent<ConcreteTaskInstance, Error>) => {
expect(event).toMatchObject(
asTaskClaimEvent('ccc', asErr(new Error(`failed to claim task 'ccc'`)))
);
Expand Down

0 comments on commit 44439ed

Please sign in to comment.