Skip to content

Commit

Permalink
Update style and optimise
Browse files Browse the repository at this point in the history
  • Loading branch information
timursevimli committed Jan 13, 2024
1 parent 1a4a32d commit 6207ae9
Showing 1 changed file with 35 additions and 23 deletions.
58 changes: 35 additions & 23 deletions test/unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ test('Done handling', (t) => {

const queue = new Queue(1)
.process((item, callback) => {
callback(null, item);
setTimeout(() => {
callback(null, item);
}, 0);
})
.done((err, { res }) => {
t.equal(err, null);
Expand All @@ -33,7 +35,9 @@ test('Success handling', (t) => {

const queue = new Queue(1)
.process((item, callback) => {
callback(null, item);
setTimeout(() => {
callback(null, item);
}, 0);
})
.success(({ res }) => {
t.equal(res, 'test');
Expand All @@ -55,7 +59,9 @@ test('Error handling', (t) => {

const queue = new Queue(1)
.process((item, callback) => {
callback(new Error('Task failed'), item);
setTimeout(() => {
callback(new Error('Task failed'), item);
}, 0);
})
.failure((err) => {
t.equal(err.message, 'Task failed');
Expand All @@ -75,20 +81,24 @@ test('Error handling', (t) => {
test('Timeout handling', (t) => {
let onTimeoutCalled = false;
let failureCalled = false;
let timer = null;

const queue = new Queue(1)
.process((item, callback) => {
timer = setTimeout(() => {
t.fail('Never should this line');
callback(null, item);
}, 100);
})
.timeout(50, (err) => {
onTimeoutCalled = true;

t.equal(err.message, 'Process timed out!');
})
.process((item, callback) => {
setTimeout(callback, 100, null, item);

onTimeoutCalled = true;
})
.failure((err) => {
failureCalled = true;

t.equal(err.message, 'Process timed out!');

failureCalled = true;
});

t.plan(4);
Expand All @@ -98,7 +108,8 @@ test('Timeout handling', (t) => {
setTimeout(() => {
t.equal(failureCalled, true);
t.equal(onTimeoutCalled, true);
}, 150);
clearTimeout(timer);
}, 50);
});

test('Wait handling', (t) => {
Expand All @@ -109,13 +120,14 @@ test('Wait handling', (t) => {
.pause()
.process((item, callback) => {
setTimeout(() => {
// t.fail('Never should this line');
callback(null, item);
}, 0);
})
.failure((err) => {
failureCalled = true;

t.equal(err.message, 'Waiting timed out');

failureCalled = true;
});

t.plan(3);
Expand All @@ -128,7 +140,7 @@ test('Wait handling', (t) => {
queue.resume();

t.equal(failureCalled, true);
}, 100);
}, 50);
});

test('Pause handling', (t) => {
Expand All @@ -137,7 +149,9 @@ test('Pause handling', (t) => {
const queue = new Queue(1)
.pause()
.process((item, callback) => {
callback(null, item);
setTimeout(() => {
callback(null, item);
}, 0);
})
.done(() => {
doneCalled = true;
Expand Down Expand Up @@ -206,10 +220,9 @@ test('Drain handling', (t) => {
test('Should task handling: async', (t) => {
const items = new Array(100).fill('item').map((e, i) => e + i);
const results = [];
const job = (item) => Promise.resolve(item);

const queue = new Queue(1)
.process(job)
.process((item) => Promise.resolve(item))
.async()
.done((err, { res }) => {
t.equal(err, null);
Expand All @@ -230,14 +243,13 @@ test('Should task handling: async', (t) => {
test('Should task handling: callback', (t) => {
const items = new Array(100).fill('item').map((e, i) => e + i);
const results = [];
const job = (item, cb) => {
setTimeout(() => {
cb(null, item);
}, 0);
};

const queue = new Queue(1)
.process(job)
.process((item, cb) => {
setTimeout(() => {
cb(null, item);
}, 0);
})
.done((err, { res }) => {
t.equal(err, null);
t.ok(items.includes(res));
Expand Down

0 comments on commit 6207ae9

Please sign in to comment.