Skip to content

Commit

Permalink
feat: throw an error if task name is duplicate
Browse files Browse the repository at this point in the history
  • Loading branch information
gajus committed Nov 26, 2020
1 parent 011ac87 commit 8072d28
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,19 @@ export class UnexpectedStateError extends ExtendableError {
}
}

export class DuplicateTaskNameError extends UnexpectedStateError {
duplicateTaskName: string;

constructor (duplicateTaskName: any) {
super(
'Task name is duplicate.',
'DUPLICATE_TASK_NAME',
);

this.duplicateTaskName = duplicateTaskName;
}
}

export class UnexpectedTaskInstructionsError extends UnexpectedStateError {
unexpectedTaskInstructions: any;

Expand Down
7 changes: 7 additions & 0 deletions src/factories/createPlanton.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
} from 'serialize-error';
import Logger from '../Logger';
import {
DuplicateTaskNameError,
UnexpectedTaskInstructionsError,
} from '../errors';
import type {
Expand Down Expand Up @@ -83,6 +84,12 @@ const createPlanton = (configuration: PlantonConfigurationInput): Planton => {
for (const inputTask of configuration.tasks) {
log.debug('registered %s task', inputTask.name);

for (const existingTask of tasks) {
if (existingTask.name === inputTask.name) {
throw new DuplicateTaskNameError(existingTask.name);
}
}

const calculateDelay = inputTask.delay || (() => {
return 1_000;
});
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ export {
createPlanton,
} from './factories';
export {
DuplicateTaskNameError,
UnexpectedTaskInstructionsError,
} from './errors';
35 changes: 35 additions & 0 deletions test/planton/factories/createPlanton.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,41 @@ test('schedules tasks at a interval', async (t) => {
await planton.terminate();
});

test('throws if multiple tasks are registered with the same name', (t) => {
const error = t.throws(() => {
createPlanton({
getActiveTaskInstructions: () => {
return [];
},
tasks: [
{
delay: () => {
return 90;
},
name: 'foo',
schedule: async () => {
return [];
},
},
{
delay: () => {
return 90;
},
name: 'foo',
schedule: async () => {
return [];
},
},
],
});
});

t.like(error, {
code: 'DUPLICATE_TASK_NAME',
duplicateTaskName: 'foo',
});
});

test('stops scheduling after Planton is terminated', async (t) => {
const schedule = sinon
.stub()
Expand Down

0 comments on commit 8072d28

Please sign in to comment.