Skip to content

Commit

Permalink
fix: update dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
gajus committed Nov 24, 2021
1 parent d00ac99 commit 7b66342
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 78 deletions.
24 changes: 12 additions & 12 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,27 +18,27 @@
"dependencies": {
"es6-error": "^4.1.1",
"promise-deferred": "^2.0.3",
"roarr": "^7.0.3",
"serialize-error": "^8.1.0"
"roarr": "^7.8.0",
"serialize-error": "^8.0.0"
},
"description": "Database-agnostic task scheduler.",
"devDependencies": {
"@istanbuljs/nyc-config-typescript": "^1.0.1",
"@types/node": "^16.4.3",
"@types/sinon": "^10.0.2",
"@types/node": "^16.11.10",
"@types/sinon": "^10.0.6",
"ava": "^3.15.0",
"coveralls": "^3.1.1",
"del-cli": "^4.0.1",
"delay": "^5.0.0",
"eslint": "^7.31.0",
"eslint-config-canonical": "^26.2.3",
"husky": "^7.0.1",
"lint-staged": "^11.1.1",
"eslint": "^8.3.0",
"eslint-config-canonical": "^32.43.0",
"husky": "^7.0.4",
"lint-staged": "^12.1.2",
"nyc": "^15.1.0",
"semantic-release": "^17.4.4",
"sinon": "^11.1.2",
"ts-node": "^10.1.0",
"typescript": "^4.3.5"
"semantic-release": "^18.0.1",
"sinon": "^12.0.1",
"ts-node": "^10.4.0",
"typescript": "^4.5.2"
},
"engines": {
"node": ">=10"
Expand Down
10 changes: 3 additions & 7 deletions src/errors.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
// @flow

/* eslint-disable fp/no-class, fp/no-this */

import ExtendableError from 'es6-error';

export class PlantonError extends ExtendableError {}
Expand Down Expand Up @@ -32,7 +28,7 @@ export class InvalidTaskConfigurationNameError extends UnexpectedStateError {
export class DuplicateTaskNameError extends UnexpectedStateError {
public duplicateTaskName: string;

public constructor (duplicateTaskName: any) {
public constructor (duplicateTaskName: string) {
super(
'Task name is duplicate.',
'DUPLICATE_TASK_NAME',
Expand All @@ -45,9 +41,9 @@ export class DuplicateTaskNameError extends UnexpectedStateError {
export class UnexpectedTaskInstructionsError extends UnexpectedStateError {
public taskName: string;

public unexpectedTaskInstructions: any;
public unexpectedTaskInstructions: string[];

public constructor (taskName: string, unexpectedTaskInstructions: any) {
public constructor (taskName: string, unexpectedTaskInstructions: string[]) {
super(
'Unexpected task instructions.',
'UNEXPECTED_TASK_INSTRUCTIONS',
Expand Down
14 changes: 8 additions & 6 deletions src/factories/createPlanton.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import delay from 'delay';
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
// @ts-expect-error -- types not available
import Deferred from 'promise-deferred';
import {
serializeError,
Expand All @@ -26,19 +25,20 @@ const log = Logger.child({
type TaskInstruction = string;

type TaskEvent = {
readonly taskName: string,
readonly instruction: string,
readonly taskName: string,
};

type ErrorEvent = {
readonly taskName: string,
readonly error: Error,
readonly taskName: string,
};

/**
* @property activeTaskInstructions A list of active task instructions as retrieved using `getActiveTaskInstructions`.
* @property concurrency The current concurrency setting value.
* @property limit A limit derived based on the value of `concurrency` and the number of `activeTaskInstructions` (CONCURRENCY - ACTIVE TASK INSTRUCTIONS = LIMIT).
* @property taskName Task name.
*/
type ScheduleConfiguration = {
readonly activeTaskInstructions: TaskInstruction[],
Expand Down Expand Up @@ -151,7 +151,7 @@ const createPlanton = (configuration: PlantonConfiguration): Planton => {
}

const terminate = (() => {
let delayPromise: any;
let delayPromise: Promise<void>;

const deferredTermination = new Deferred();

Expand Down Expand Up @@ -287,6 +287,7 @@ const createPlanton = (configuration: PlantonConfiguration): Planton => {
}

if (taskInstructions.length > 0) {
// eslint-disable-next-line require-atomic-updates
task.attemptNumber = 0;

for (const taskInstruction of taskInstructions) {
Expand All @@ -310,7 +311,8 @@ const createPlanton = (configuration: PlantonConfiguration): Planton => {
return () => {
active = false;

if (delayPromise) {
if (delayPromise !== undefined) {
// @ts-expect-error -- deferred-promise types are not available
delayPromise.clear();
}

Expand Down
6 changes: 3 additions & 3 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
export type EventMap = Record<string, any>;
export type EventMap = Record<string, unknown>;

type EventKey<T extends EventMap> = string & keyof T;
type EventReceiver<T> = (parameters: T) => void;

export type Emitter<T extends EventMap> = {
on: <K extends EventKey<T>>(eventName: K, function_: EventReceiver<T[K]>) => void,
off: <K extends EventKey<T>>(eventName: K, function_: EventReceiver<T[K]>) => void,
emit: <K extends EventKey<T>>(eventName: K, parameters: T[K]) => void,
off: <K extends EventKey<T>>(eventName: K, function_: EventReceiver<T[K]>) => void,
on: <K extends EventKey<T>>(eventName: K, function_: EventReceiver<T[K]>) => void,
};
81 changes: 32 additions & 49 deletions test/planton/factories/createPlanton.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import test from 'ava';
import delay from 'delay';
import sinon from 'sinon';
import {
stub, spy,
} from 'sinon';
import {
createPlanton,
} from '../../../src/factories/createPlanton';

test('schedules tasks at a interval', async (t) => {
const schedule = sinon
.stub()
const schedule = stub()
.returns([]);

const planton = createPlanton({
Expand Down Expand Up @@ -95,8 +96,7 @@ test('throws if multiple tasks are registered with the same name', (t) => {
});

test('stops scheduling after Planton is terminated', async (t) => {
const schedule = sinon
.stub()
const schedule = stub()
.onFirstCall()
.returns([])
.onSecondCall()
Expand Down Expand Up @@ -131,8 +131,7 @@ test('stops scheduling after Planton is terminated', async (t) => {
test('cancels delay when Planton is terminated', async (t) => {
t.timeout(100);

const schedule = sinon
.stub()
const schedule = stub()
.throws();

const planton = createPlanton({
Expand All @@ -156,14 +155,13 @@ test('cancels delay when Planton is terminated', async (t) => {
});

test('emits "task" event for every new task instruction', async (t) => {
const schedule = sinon
.stub()
const schedule = stub()
.returns([
'bar',
'baz',
]);

const eventHandler = sinon.spy();
const eventHandler = spy();

const planton = createPlanton({
getActiveTaskInstructions: async () => {
Expand Down Expand Up @@ -199,11 +197,10 @@ test('emits "task" event for every new task instruction', async (t) => {
});

test('does not attempt to schedule tasks when active tasks >= concurrency limit', async (t) => {
const schedule = sinon
.stub()
const schedule = stub()
.throws();

const eventHandler = sinon.spy();
const eventHandler = spy();

const planton = createPlanton({
getActiveTaskInstructions: async () => {
Expand Down Expand Up @@ -235,8 +232,7 @@ test('does not attempt to schedule tasks when active tasks >= concurrency limit'
});

test('invokes schedule with the limit adjusted based on the number of current active tasks', async (t) => {
const schedule = sinon
.stub()
const schedule = stub()
.returns([]);

const planton = createPlanton({
Expand Down Expand Up @@ -267,8 +263,7 @@ test('invokes schedule with the limit adjusted based on the number of current ac
});

test('invokes schedule with the current active task instructions', async (t) => {
const schedule = sinon
.stub()
const schedule = stub()
.returns([]);

const planton = createPlanton({
Expand Down Expand Up @@ -302,16 +297,15 @@ test('invokes schedule with the current active task instructions', async (t) =>
});

test('invokes `calculateDelay` with the number of attempts since the last time `schedule` produced results', async (t) => {
const schedule = sinon
.stub()
const schedule = stub()
.onFirstCall()
.returns([])
.onSecondCall()
.returns([
'foo',
]);

const calculateDelay = sinon.stub().returns(50);
const calculateDelay = stub().returns(50);

const planton = createPlanton({
getActiveTaskInstructions: async () => {
Expand Down Expand Up @@ -367,7 +361,7 @@ test('terminate waits for scheduling to complete', async (t) => {
});

test('emits error if scheduler produces an error', async (t) => {
const eventHandler = sinon.spy();
const eventHandler = spy();

const error = new Error('foo');

Expand All @@ -383,8 +377,6 @@ test('emits error if scheduler produces an error', async (t) => {
name: 'foo',
schedule: async () => {
throw error;

return [];
},
},
],
Expand All @@ -407,7 +399,7 @@ test('emits error if scheduler produces an error', async (t) => {
});

test('emits error if scheduler produces more results than the supplied limit', async (t) => {
const eventHandler = sinon.spy();
const eventHandler = spy();

const planton = createPlanton({
getActiveTaskInstructions: async () => {
Expand Down Expand Up @@ -450,7 +442,7 @@ test('emits error if scheduler produces more results than the supplied limit', a
});

test('emits error if `calculateLimit` produces less than 0', async (t) => {
const eventHandler = sinon.spy();
const eventHandler = spy();

const planton = createPlanton({
getActiveTaskInstructions: async () => {
Expand Down Expand Up @@ -488,7 +480,7 @@ test('emits error if `calculateLimit` produces less than 0', async (t) => {
});

test('emits error if `calculateLimit` does not produce an integer', async (t) => {
const eventHandler = sinon.spy();
const eventHandler = spy();

const planton = createPlanton({
getActiveTaskInstructions: async () => {
Expand Down Expand Up @@ -526,7 +518,7 @@ test('emits error if `calculateLimit` does not produce an integer', async (t) =>
});

test('unexpected scheduler result shape triggers an error (not array)', async (t) => {
const eventHandler = sinon.spy();
const eventHandler = spy();

const planton = createPlanton({
getActiveTaskInstructions: async () => {
Expand Down Expand Up @@ -567,7 +559,7 @@ test('unexpected scheduler result shape triggers an error (not array)', async (t
});

test('unexpected scheduler result shape triggers an error (not an array of string literals)', async (t) => {
const eventHandler = sinon.spy();
const eventHandler = spy();

const planton = createPlanton({
getActiveTaskInstructions: async () => {
Expand Down Expand Up @@ -612,16 +604,14 @@ test('unexpected scheduler result shape triggers an error (not an array of strin
});

test('high-frequency issues do not block other tasks', async (t) => {
const foo = sinon
.stub()
const foo = stub()
.callsFake(async () => {
await delay(10);

return [];
});

const bar = sinon
.stub()
const bar = stub()
.callsFake(async () => {
await delay(10);

Expand Down Expand Up @@ -659,16 +649,13 @@ test('high-frequency issues do not block other tasks', async (t) => {
});

test('scheduler executions are evenly distributed', async (t) => {
const foo = sinon
.stub()
const foo = stub()
.returns([]);

const bar = sinon
.stub()
const bar = stub()
.returns([]);

const baz = sinon
.stub()
const baz = stub()
.returns([]);

const planton = createPlanton({
Expand Down Expand Up @@ -710,8 +697,7 @@ test('scheduler executions are evenly distributed', async (t) => {
});

test('continues to attempt scheduling tasks that breach concurrency', async (t) => {
const getActiveTaskInstructions = sinon
.stub()
const getActiveTaskInstructions = stub()
.returns(['1']);

const planton = createPlanton({
Expand All @@ -738,10 +724,9 @@ test('continues to attempt scheduling tasks that breach concurrency', async (t)
});

test('continues to attempt scheduling tasks that produce invalid instructions (not array)', async (t) => {
const eventHandler = sinon.stub();
const eventHandler = stub();

const getActiveTaskInstructions = sinon
.stub()
const getActiveTaskInstructions = stub()
.returns([]);

const planton = createPlanton({
Expand Down Expand Up @@ -775,10 +760,9 @@ test('continues to attempt scheduling tasks that produce invalid instructions (n
});

test('continues to attempt scheduling tasks that produce invalid instructions (not an array of string literals)', async (t) => {
const eventHandler = sinon.stub();
const eventHandler = stub();

const getActiveTaskInstructions = sinon
.stub()
const getActiveTaskInstructions = stub()
.returns([]);

const planton = createPlanton({
Expand Down Expand Up @@ -814,10 +798,9 @@ test('continues to attempt scheduling tasks that produce invalid instructions (n
});

test('continues to attempt scheduling tasks that produce more instructions than the supplied limit', async (t) => {
const eventHandler = sinon.spy();
const eventHandler = spy();

const getActiveTaskInstructions = sinon
.stub()
const getActiveTaskInstructions = stub()
.returns([]);

const planton = createPlanton({
Expand Down
Loading

0 comments on commit 7b66342

Please sign in to comment.