Skip to content

Commit

Permalink
Merge pull request #437 from mattia-lau/@features/doesExist
Browse files Browse the repository at this point in the history
[Reopen] Add API for checking the task is existing without throwing error
  • Loading branch information
kamilmysliwiec committed Jan 22, 2021
2 parents 5e5fe17 + 45bcd27 commit d54c7ce
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 0 deletions.
13 changes: 13 additions & 0 deletions lib/scheduler.registry.ts
Expand Up @@ -8,6 +8,19 @@ export class SchedulerRegistry {
private readonly timeouts = new Map<string, any>();
private readonly intervals = new Map<string, any>();

doesExists(type: 'cron' | 'timeout' | 'interval', name: string) {
switch (type) {
case 'cron':
return this.cronJobs.has(name);
case 'interval':
return this.intervals.has(name);
case 'timeout':
return this.timeouts.has(name);
default:
return false;
}
}

getCronJob(name: string) {
const ref = this.cronJobs.get(name);
if (!ref) {
Expand Down
14 changes: 14 additions & 0 deletions tests/e2e/cron-jobs.spec.ts
Expand Up @@ -172,6 +172,20 @@ describe('Cron', () => {
expect(clock.countTimers()).toBe(0);
});

it('should return true for dynamic cron job', async () => {
const service: CronService = app.get(CronService);
await app.init();

service.addCronJob();
expect(service.doesExists('dynamic')).toEqual(true);
});

it('should return false for dynamic cron job', async () => {
const service: CronService = app.get(CronService);
await app.init();
expect(service.doesExists('dynamic')).toEqual(false);
});

afterEach(async () => {
await app.close();
});
Expand Down
14 changes: 14 additions & 0 deletions tests/e2e/interval.spec.ts
Expand Up @@ -98,6 +98,20 @@ describe('Interval', () => {
expect(jest.getTimerCount()).toBe(0);
});

it('should return true for dynamic interval', async () => {
const service: IntervalService = app.get(IntervalService);
await app.init();

service.addInterval();
expect(service.doesExists('dynamic')).toEqual(true);
});

it('should return false for dynamic interval', async () => {
const service: IntervalService = app.get(IntervalService);
await app.init();
expect(service.doesExists('dynamic')).toEqual(false);
});

afterEach(async () => {
await app.close();
});
Expand Down
14 changes: 14 additions & 0 deletions tests/e2e/timeout.spec.ts
Expand Up @@ -98,6 +98,20 @@ describe('Timeout', () => {
expect(jest.getTimerCount()).toBe(0);
});

it('should return true for dynamic timeout', async () => {
const service: TimeoutService = app.get(TimeoutService);
await app.init();

service.addTimeout();
expect(service.doesExists('dynamic')).toEqual(true);
});

it('should return false for dynamic timeout', async () => {
const service: TimeoutService = app.get(TimeoutService);
await app.init();
expect(service.doesExists('dynamic')).toEqual(false);
});

afterEach(async () => {
await app.close();
});
Expand Down
4 changes: 4 additions & 0 deletions tests/src/cron.service.ts
Expand Up @@ -68,4 +68,8 @@ export class CronService {
this.schedulerRegistry.addCronJob('dynamic', job);
return job;
}

doesExists(name: string): boolean {
return this.schedulerRegistry.doesExists('cron', name);
}
}
4 changes: 4 additions & 0 deletions tests/src/interval.service.ts
Expand Up @@ -26,4 +26,8 @@ export class IntervalService {
(intervalRef as unknown) as number,
);
}

doesExists(name: string): boolean {
return this.schedulerRegistry.doesExists('interval', name);
}
}
4 changes: 4 additions & 0 deletions tests/src/timeout.service.ts
Expand Up @@ -25,4 +25,8 @@ export class TimeoutService {
(timeoutRef as unknown) as number,
);
}

doesExists(name: string): boolean {
return this.schedulerRegistry.doesExists('timeout', name);
}
}

0 comments on commit d54c7ce

Please sign in to comment.