-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
127 additions
and
187 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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,20 @@ | ||
async function* asyncPool(concurrency, iterable, iteratorFn) { | ||
let done; | ||
const ret = []; | ||
const executing = new Set(); | ||
for (const item of iterable) { | ||
const promise = Promise.resolve().then(() => iteratorFn(item, iterable)); | ||
ret.push(promise); | ||
executing.add(promise); | ||
const clean = () => executing.delete(promise); | ||
promise.then(clean).catch(clean); | ||
if (executing.size >= concurrency) { | ||
yield await Promise.race(executing); | ||
} | ||
} | ||
while (executing.size) { | ||
yield await Promise.race(executing); | ||
} | ||
} | ||
|
||
module.exports = asyncPool; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,90 +1,78 @@ | ||
const es6AsyncPool = require("../../lib/es6"); | ||
const es7AsyncPool = require("../../lib/es7"); | ||
const asyncPool = require("../../lib/es9"); | ||
const { expect } = require("chai"); | ||
|
||
describe("asyncPool", function() { | ||
for (const [title, asyncPool] of [ | ||
["ES6 support", es6AsyncPool], | ||
["ES7 support", es7AsyncPool] | ||
]) { | ||
describe(title, function() { | ||
it("only runs as many promises in parallel as given by the pool limit", async function() { | ||
const results = []; | ||
const timeout = i => | ||
new Promise(resolve => | ||
setTimeout(() => { | ||
results.push(i); | ||
resolve(); | ||
}, i) | ||
); | ||
await asyncPool(2, [100, 500, 300, 200], timeout); | ||
expect(results).to.deep.equal([100, 300, 500, 200]); | ||
}); | ||
|
||
it("runs all promises in parallel when the pool is bigger than needed", async function() { | ||
const results = []; | ||
const timeout = i => | ||
new Promise(resolve => | ||
setTimeout(() => { | ||
results.push(i); | ||
resolve(); | ||
}, i) | ||
); | ||
await asyncPool(5, [100, 500, 300, 200], timeout); | ||
expect(results).to.deep.equal([100, 200, 300, 500]); | ||
}); | ||
const timeout = i => | ||
new Promise(resolve => | ||
setTimeout(() => { | ||
resolve(i); | ||
}, i) | ||
); | ||
|
||
it("rejects on error (but does not leave unhandled rejections) (1/2)", async function() { | ||
const timeout = _ => Promise.reject(); | ||
return expect( | ||
asyncPool(5, [100, 500, 300, 200], timeout) | ||
).to.be.rejected; | ||
// check console - no UnhandledPromiseRejectionWarning should appear | ||
}); | ||
describe("asyncPool", function() { | ||
it("only runs as many promises in parallel as given by the pool limit", async function() { | ||
const gen = asyncPool(2, [10, 50, 30, 20], timeout); | ||
expect((await gen.next()).value).to.equal(10); | ||
expect((await gen.next()).value).to.equal(30); | ||
expect((await gen.next()).value).to.equal(50); | ||
expect((await gen.next()).value).to.equal(20); | ||
}); | ||
|
||
it("rejects on error (but does not leave unhandled rejections) (2/2)", async function() { | ||
return expect( | ||
asyncPool( | ||
2, | ||
[0, 1, 2], | ||
(i, a) => | ||
i < a.length - 1 ? Promise.resolve(i) : Promise.reject(i) | ||
) | ||
).to.be.rejected; | ||
// check console - no UnhandledPromiseRejectionWarning should appear | ||
}); | ||
it("runs all promises in parallel when the pool is bigger than needed", async function() { | ||
const gen = asyncPool(5, [10, 50, 30, 20], timeout); | ||
expect((await gen.next()).value).to.equal(10); | ||
expect((await gen.next()).value).to.equal(20); | ||
expect((await gen.next()).value).to.equal(30); | ||
expect((await gen.next()).value).to.equal(50); | ||
}); | ||
|
||
it("rejects as soon as first promise rejects", async function() { | ||
const startedTasks = []; | ||
const finishedTasks = []; | ||
const timeout = i => { | ||
startedTasks.push(i); | ||
return new Promise((resolve, reject) => | ||
setTimeout(() => { | ||
if (i === 300) { | ||
reject(new Error("Oops")); | ||
} else { | ||
finishedTasks.push(i); | ||
resolve(); | ||
} | ||
}, i) | ||
); | ||
}; | ||
it("rejects on error (but does not leave unhandled rejections) (1/2)", async function() { | ||
const timeout = _ => Promise.reject(); | ||
const gen = asyncPool(5, [10, 50, 30, 20], timeout); | ||
expect(gen.next()).to.be.rejected; | ||
// check console - no UnhandledPromiseRejectionWarning should appear | ||
}); | ||
|
||
const testResult = await expect( | ||
asyncPool(2, [100, 500, 300, 200], timeout) | ||
).to.be.rejected; | ||
it("rejects on error (but does not leave unhandled rejections) (2/2)", async function() { | ||
const gen = asyncPool( | ||
2, | ||
[0, 1, 2], | ||
(i, a) => (i < a.length - 1 ? Promise.resolve(i) : Promise.reject(i)) | ||
); | ||
expect((await gen.next()).value).to.equal(0); | ||
expect(gen.next()).to.be.rejected; | ||
// check console - no UnhandledPromiseRejectionWarning should appear | ||
}); | ||
|
||
expect(startedTasks).to.deep.equal([100, 500, 300]); | ||
expect(finishedTasks).to.deep.equal([100]); | ||
it("rejects as soon as first promise rejects", async function() { | ||
const startedTasks = []; | ||
const finishedTasks = []; | ||
const timeout = i => { | ||
startedTasks.push(i); | ||
return new Promise((resolve, reject) => | ||
setTimeout(() => { | ||
if (i === 30) { | ||
reject(new Error("Oops")); | ||
} else { | ||
finishedTasks.push(i); | ||
resolve(); | ||
} | ||
}, i) | ||
); | ||
}; | ||
|
||
// tasks started before the error will continue, though - just wait a bit | ||
await new Promise(resolve => setTimeout(() => resolve(), 500)); | ||
expect(startedTasks).to.deep.equal([100, 500, 300]); | ||
expect(finishedTasks).to.deep.equal([100, 500]); | ||
const gen = asyncPool(2, [10, 50, 30, 20], timeout); | ||
const step1 = gen.next(); | ||
const step2 = gen.next(); | ||
const step3 = gen.next(); | ||
expect(step1).to.be.fulfilled; | ||
await expect(step2).to.be.rejected; | ||
expect(startedTasks).to.deep.equal([10, 50, 30]); | ||
expect(finishedTasks).to.deep.equal([10]); | ||
await expect(step3).to.be.fulfilled; | ||
|
||
return testResult; | ||
}); | ||
}); | ||
} | ||
// tasks started before the exception will continue, though - just wait a bit | ||
await new Promise(resolve => setTimeout(() => resolve(), 50)); | ||
expect(startedTasks).to.deep.equal([10, 50, 30]); | ||
expect(finishedTasks).to.deep.equal([10, 50]); | ||
}); | ||
}); |