Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Limit inProgress using maxItems #11

Merged
merged 1 commit into from
Jul 16, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,9 @@ Queue.prototype._processHead = function() {
});
}

while (queue.length && queue[0].time <= now) {
var inProgressSize = Object.keys(inProgress).length;

while (queue.length && queue[0].time <= now && inProgressSize++ < self.maxItems) {
var el = queue.shift();
var id = uuid();

Expand Down
57 changes: 57 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,63 @@ describe('Queue', function() {
assert(call === queue.maxAttempts + 1);
});
});

it('should limit inProgress using maxItems', function() {
var waiting = [];
var i;

queue.maxItems = 100;
queue.maxAttempts = 2;
queue.fn = function(_, done) {
waiting.push(done);
};

// add maxItems * 2 items
for (i = 0; i < queue.maxItems * 2; i++) {
queue.addItem({ index: i });
}

// the queue should be full
assert(size(queue).queue === queue.maxItems);

queue.start();
// the queue is now empty and everything is in progress
assert(size(queue).queue === 0);
assert(size(queue).inProgress === queue.maxItems);

// while the items are in progress let's add maxItems times two items
for (i = 0; i < queue.maxItems * 2; i++) {
queue.addItem({ index: i });
}

// inProgress and queue should be full
assert(size(queue).queue === queue.maxItems);
assert(size(queue).inProgress === queue.maxItems);
assert(waiting.length === queue.maxItems);

// resolved all waiting items
while (waiting.length) {
waiting.pop()();
}

// inProgress should now be empty
assert(size(queue).queue === queue.maxItems);
assert(size(queue).inProgress === 0);

// wait for the queue to be processed
clock.tick(queue.getDelay(0));

// items should now be in progress
assert(size(queue).queue === 0);
assert(size(queue).inProgress === queue.maxItems);

function size(queue) {
return {
queue: queue._store.get(queue.keys.QUEUE).length,
inProgress: Object.keys(queue._store.get(queue.keys.IN_PROGRESS) || {}).length
};
}
});
});

describe('events', function() {
Expand Down