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

Fixed executeInParallel() function throwing error when the number o… #1015

Merged
merged 4 commits into from
Sep 27, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export default async function executeInParallel( options ) {
concurrency = os.cpus().length / 2
} = options;

const concurrencyAsInteger = Math.floor( concurrency ) || 1;
martnpaneq marked this conversation as resolved.
Show resolved Hide resolved
const normalizedCwd = upath.toUnix( cwd );
const packages = ( await glob( `${ packagesDirectory }/*/`, {
cwd: normalizedCwd,
Expand All @@ -58,7 +59,7 @@ export default async function executeInParallel( options ) {
packages.filter( packagesDirectoryFilter ) :
packages;

const packagesInThreads = getPackagesGroupedByThreads( packagesToProcess, concurrency );
const packagesInThreads = getPackagesGroupedByThreads( packagesToProcess, concurrencyAsInteger );

const callbackModule = upath.join( cwd, crypto.randomUUID() + '.mjs' );
await fs.writeFile( callbackModule, `export default ${ taskToExecute };`, 'utf-8' );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,56 @@ describe( 'executeInParallel()', () => {
await promise;
} );

it( 'should use number of cores divided by two as default (`concurrency`)', async () => {
const promise = executeInParallel( defaultOptions );
await delay( 0 );

expect( stubs.WorkerMock.instances ).toHaveLength( 2 );

// Workers did not emit an error.
for ( const worker of stubs.WorkerMock.instances ) {
getExitCallback( worker )( 0 );
}

await promise;
} );

it( 'should round down to the closest integer (`concurrency`)', async () => {
const options = Object.assign( {}, defaultOptions, {
concurrency: 3.5
martnpaneq marked this conversation as resolved.
Show resolved Hide resolved
} );

const promise = executeInParallel( options );
await delay( 0 );

expect( stubs.WorkerMock.instances ).toHaveLength( 3 );

// Workers did not emit an error.
for ( const worker of stubs.WorkerMock.instances ) {
getExitCallback( worker )( 0 );
}

await promise;
} );

it( 'should assign at least one thread even if concurrency is 0 (`concurrency`)', async () => {
const options = Object.assign( {}, defaultOptions, {
concurrency: 0
} );

const promise = executeInParallel( options );
await delay( 0 );

expect( stubs.WorkerMock.instances ).toHaveLength( 1 );

// Workers did not emit an error.
for ( const worker of stubs.WorkerMock.instances ) {
getExitCallback( worker )( 0 );
}

await promise;
} );

it( 'should resolve the promise if a worker finished (aborted) with a non-zero exit code (first worker)', async () => {
const promise = executeInParallel( defaultOptions );
await delay( 0 );
Expand Down