Skip to content

Commit

Permalink
Second attempt
Browse files Browse the repository at this point in the history
  • Loading branch information
XhmikosR committed Feb 9, 2022
1 parent 2117982 commit 8098237
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 39 deletions.
35 changes: 16 additions & 19 deletions lib/getThreads.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,23 @@ const THREADS = CPUS > 2 ? CPUS - 1 : 1;
function getThreads(config) {
const { threads } = config;

const defaultCheck = threads === 'auto' ||
threads === -1 ||
threads === '' ||
threads === true ||
threads === null ||
typeof threads === 'undefined';

if (defaultCheck) {
return THREADS;
}

if (threads === 0 || threads === 1 || threads === false) {
return 1;
switch (threads) {
case 'auto':
case -1:
case '':
case true:
case null:
case undefined:
return THREADS;
case 0:
case 1:
case false:
return 1;
default:
if (threads > 1) {
return threads;
}
}

if (threads > 1) {
return threads;
}

throw new Error(`Invalid threads option! (${threads})`);
}

module.exports = getThreads;
30 changes: 10 additions & 20 deletions test/threads_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ describe('getThreads', () => {
it('should use the number of available threads -1', done => {
const config = {};

for (const option of ['auto', true, -1, '', null, undefined]) {
for (const option of ['auto', -1, '', true, null, undefined]) {
config.threads = option;
const expected = THREADS;
const actual = getThreads(config);
Expand All @@ -23,14 +23,16 @@ describe('getThreads', () => {
done();
});

it('should return 1 with `threads: false`', done => {
const config = {
threads: false
};
const expected = 1;
const actual = getThreads(config);
it('should return 1 with false, 0, or 1', done => {
const config = {};

for (const option of [0, 1, false]) {
config.threads = option;
const expected = 1;
const actual = getThreads(config);
assert.equal(actual, expected);
}

assert.equal(actual, expected);
done();
});

Expand All @@ -44,16 +46,4 @@ describe('getThreads', () => {
assert.equal(actual, expected);
done();
});

it('should throw an error with invalid threads option', done => {
const config = {};

for (const option of [-2, 'foo']) {
config.threads = option;
const expected = () => getThreads(config);
assert.throws(expected, /^Error: Invalid threads/, `"thread: ${option}" failed!`);
}

done();
});
});

0 comments on commit 8098237

Please sign in to comment.