Skip to content

Commit

Permalink
chore(sandbox): reproduce issue taskforcesh#2731
Browse files Browse the repository at this point in the history
  • Loading branch information
tolgap committed Aug 26, 2024
1 parent b8d8886 commit 36d57f2
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 0 deletions.
34 changes: 34 additions & 0 deletions tests/fixtures/fixture_processor_steps.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
'use strict';

const delay = require('./delay');

module.exports = async function (job) {
let step = job.data.step;
while (step !== 'FINISH') {
switch (step) {
case 'INITIAL': {
await delay(200);
await job.updateData({
...job.data,
step: 'SECOND',
extraDataSecondStep: 'second data',
});
step = 'SECOND';
break;
}
case 'SECOND': {
await delay(200);
await job.updateData({
...job.data,
extraDataFinishedStep: 'finish data',
step: 'FINISH',
});
step = 'FINISH';
return;
}
default: {
throw new Error('invalid step');
}
}
}
};
63 changes: 63 additions & 0 deletions tests/test_sandbox_step_pattern.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { expect } from 'chai';
import { default as IORedis } from 'ioredis';
import { Job, Queue, Worker } from '../src/classes';
import { beforeEach, before, after as afterAll, it } from 'mocha';
import { v4 } from 'uuid';
import { removeAllQueueData } from '../src/utils';

describe.only('sandbox step pattern', () => {
const redisHost = process.env.REDIS_HOST || 'localhost';
const prefix = process.env.BULLMQ_TEST_PREFIX || 'bull';
let queue: Queue;
let queueName: string;

let connection;
before(async function () {
connection = new IORedis(redisHost, { maxRetriesPerRequest: null });
});

beforeEach(async function () {
queueName = `test-${v4()}`;
queue = new Queue(queueName, { connection, prefix });
});

afterEach(async function () {
await queue.close();
await removeAllQueueData(new IORedis(), queueName);
});

afterAll(async function () {
await connection.quit();
});

it('should process steps and complete', async () => {
const processFile = __dirname + '/fixtures/fixture_processor_steps.js';

const worker = new Worker(queueName, processFile, {
connection,
prefix,
drainDelay: 1,
});

const completing = new Promise<void>((resolve, reject) => {
worker.on('completed', async (job: Job) => {
try {
expect(job.data).to.be.eql({
step: 'FINISH',
extraDataSecondStep: 'second data',
extraDataFinishedStep: 'finish data',
});
resolve();
} catch (err) {
reject(err);
}
});
});

await queue.add('test', { step: 'INITIAL' });

await completing;

await worker.close();
});
});

0 comments on commit 36d57f2

Please sign in to comment.