Skip to content

Commit

Permalink
feat: expose maxThreads and minThreads out of Piscina instance
Browse files Browse the repository at this point in the history
  • Loading branch information
metcoder95 committed Sep 11, 2021
1 parent 2f95678 commit bbb800c
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1030,6 +1030,14 @@ class Piscina extends EventEmitterAsyncResource {
return this.#pool.destroy();
}

get maxThreads (): number {
return this.#pool.options.maxThreads;
}

get minThreads (): number {
return this.#pool.options.minThreads;
}

get options () : FilledOptions {
return this.#pool.options;
}
Expand Down
45 changes: 45 additions & 0 deletions test/post-task.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { MessageChannel } from 'worker_threads';
import { cpus } from 'os';
import Piscina from '..';
import { test } from 'tap';
import { resolve } from 'path';
Expand Down Expand Up @@ -131,3 +132,47 @@ test('Piscina.run options is correct type', async ({ rejects }) => {

rejects(pool.run(42, 1 as any), /options must be an object/);
});

test('Piscina.maxThreads should return the max number of threads to be used (default)', ({ equal, plan }) => {
plan(1);
const pool = new Piscina({
filename: resolve(__dirname, 'fixtures/eval.js')
});

const maxThreads = (cpus().length || 1) * 1.5;

equal(pool.maxThreads, maxThreads);
});

test('Piscina.minThreads should return the max number of threads to be used (custom)', ({ equal, plan }) => {
const maxThreads = 3;
const pool = new Piscina({
maxThreads,
filename: resolve(__dirname, 'fixtures/eval.js')
});

plan(1);

equal(pool.maxThreads, maxThreads);
});

test('Piscina.minThreads should return the max number of threads to be used (default)', ({ equal, plan }) => {
const pool = new Piscina({
filename: resolve(__dirname, 'fixtures/eval.js')
});
const minThreads = Math.max((cpus().length || 1) / 2, 1);

plan(1);
equal(pool.minThreads, minThreads);
});

test('Piscina.minThreads should return the max number of threads to be used (custom)', ({ equal, plan }) => {
const minThreads = 2;
const pool = new Piscina({
filename: resolve(__dirname, 'fixtures/eval.js'),
minThreads
});
plan(1);

equal(pool.minThreads, minThreads);
});

0 comments on commit bbb800c

Please sign in to comment.