diff --git a/nodejs-p-limit.spec b/nodejs-p-limit.spec new file mode 100644 index 0000000..33a257c --- /dev/null +++ b/nodejs-p-limit.spec @@ -0,0 +1,49 @@ +%{?nodejs_find_provides_and_requires} +%global packagename p-limit +%global enable_tests 0 +Name: nodejs-p-limit +Version: 1.1.0 +Release: 1 +Summary: Run multiple promise-returning & async functions with limited concurrency +License: MIT +URL: https://github.com/sindresorhus/p-limit +Source0: https://registry.npmjs.org/%{packagename}/-/%{packagename}-%{version}.tgz +Source1: https://raw.githubusercontent.com/sindresorhus/p-limit/v%{version}/test.js +ExclusiveArch: %{nodejs_arches} noarch +BuildArch: noarch +BuildRequires: nodejs-packaging +%if 0%{?enable_tests} +BuildRequires: npm(ava) +%endif +%description +Run multiple promise-returning & async functions with limited concurrency + +%prep +%autosetup -n package +cp -p %{SOURCE1} . + +%build + +%install +mkdir -p %{buildroot}%{nodejs_sitelib}/%{packagename} +cp -pr package.json index.js \ + %{buildroot}%{nodejs_sitelib}/%{packagename} +%nodejs_symlink_deps + +%check +%nodejs_symlink_deps --check +%{__nodejs} -e 'require("./")' +%if 0%{?enable_tests} +%{_bindir}/ava +%else +%{_bindir}/echo -e "\e[101m -=#=- Tests disabled -=#=- \e[0m" +%endif + +%files +%doc readme.md +%license license +%{nodejs_sitelib}/%{packagename} + +%changelog +* Mon Aug 31 2020 wutao - 1.1.0-1 +- Package init diff --git a/p-limit-1.1.0.tgz b/p-limit-1.1.0.tgz new file mode 100644 index 0000000..6fd05ac Binary files /dev/null and b/p-limit-1.1.0.tgz differ diff --git a/test.js b/test.js new file mode 100644 index 0000000..79582a5 --- /dev/null +++ b/test.js @@ -0,0 +1,37 @@ +import test from 'ava'; +import delay from 'delay'; +import inRange from 'in-range'; +import timeSpan from 'time-span'; +import randomInt from 'random-int'; +import m from './'; + +test('concurrency: 1', async t => { + const input = [ + [10, 300], + [20, 200], + [30, 100] + ]; + + const end = timeSpan(); + const limit = m(1); + const mapper = ([val, ms]) => limit(() => delay(ms).then(() => val)); + + t.deepEqual(await Promise.all(input.map(mapper)), [10, 20, 30]); + t.true(inRange(end(), 590, 650)); +}); + +test('concurrency: 4', async t => { + const concurrency = 5; + let running = 0; + + const limit = m(concurrency); + + const input = Array(100).fill(0).map(() => limit(async () => { + running++; + t.true(running <= concurrency); + await delay(randomInt(30, 200)); + running--; + })); + + await Promise.all(input); +});