-
Notifications
You must be signed in to change notification settings - Fork 435
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add support for adding jobs in bulk
- Loading branch information
Showing
4 changed files
with
107 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { Queue, Worker, Job } from '@src/classes'; | ||
import { expect } from 'chai'; | ||
import IORedis from 'ioredis'; | ||
import { beforeEach, describe, it } from 'mocha'; | ||
import { v4 } from 'node-uuid'; | ||
|
||
describe('bulk jobs', () => { | ||
let queue: Queue; | ||
let queueName: string; | ||
let client: IORedis.Redis; | ||
|
||
beforeEach(function() { | ||
client = new IORedis(); | ||
return client.flushdb(); | ||
}); | ||
|
||
beforeEach(async function() { | ||
queueName = 'test-' + v4(); | ||
queue = new Queue(queueName); | ||
}); | ||
|
||
afterEach(async function() { | ||
await queue.close(); | ||
return client.quit(); | ||
}); | ||
|
||
it('should process jobs', async () => { | ||
const name = 'test'; | ||
let processor; | ||
const processing = new Promise(resolve => [ | ||
(processor = async (job: Job) => { | ||
if (job.data.idx === 0) { | ||
expect(job.data.foo).to.be.equal('bar'); | ||
} else { | ||
expect(job.data.idx).to.be.equal(1); | ||
expect(job.data.foo).to.be.equal('baz'); | ||
resolve(); | ||
} | ||
}), | ||
]); | ||
const worker = new Worker(queueName, processor); | ||
await worker.waitUntilReady(); | ||
|
||
const jobs = await queue.addBulk([ | ||
{ name, data: { idx: 0, foo: 'bar' } }, | ||
{ name, data: { idx: 1, foo: 'baz' } }, | ||
]); | ||
expect(jobs).to.have.length(2); | ||
|
||
expect(jobs[0].id).to.be.ok; | ||
expect(jobs[0].data.foo).to.be.eql('bar'); | ||
expect(jobs[1].id).to.be.ok; | ||
expect(jobs[1].data.foo).to.be.eql('baz'); | ||
|
||
await processing; | ||
|
||
await worker.close(); | ||
}); | ||
}); |