diff options
Diffstat (limited to 'test/scheduler.test.ts')
-rw-r--r-- | test/scheduler.test.ts | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/test/scheduler.test.ts b/test/scheduler.test.ts new file mode 100644 index 0000000..ffcd623 --- /dev/null +++ b/test/scheduler.test.ts @@ -0,0 +1,60 @@ +import { expect } from 'chai'; +import Scheduler from '../lib/scheduler'; +import Model, { CustomEvent } from './model'; +import connection from './utils/dbConnection'; + +const sleep = async (time: number) => new Promise<void>(res => setTimeout(res, time)); + +describe('Scheduler', async () => { + let event: CustomEvent; + let scheduler: Scheduler; + + beforeEach(() => { + scheduler = new Scheduler(Model); + }); + + afterEach(() => { + scheduler.stopPolling(); + }); + + before(async () => { + event = await Model.create({ + type: 'test', + schedule: '*/15 * * * * *', // Every 15 seconds + context: { + message: 'Hello, world!' + }, + }); + }); + + after(async () => { + connection.dropCollection('customevents'); + }); + + + it('Should run event', async () => { + // Wait until job is run + await new Promise<void>(res => { + scheduler.registerHandler('test', async (event: CustomEvent) => { + await sleep(2000); + await event.log(event.context.message) + res(); + }); + }); + + // Wait for status to change + await sleep(100); + + const updatedEvent = await Model.findOne(); + expect(updatedEvent).to.have.property('status').equal('complete'); + }); + + + it('Should fail an event if no handler is present', async () => { + await sleep(25000); + + const updatedEvent = await Model.findOne(); + expect(updatedEvent).to.have.property('status').equal('failed'); + expect(updatedEvent).to.have.property('error').equal('Error: No handler found'); + }); +}); |