Skip to content

Commit

Permalink
Handle error cases in MavenCli#calculateDegreeOfConcurrencyWithCoreMu…
Browse files Browse the repository at this point in the history
…ltiplier(String) and use IllegalArgumentException to report them back
  • Loading branch information
kwart committed Jul 13, 2022
1 parent 5bdcb5c commit a2ed3bd
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 3 deletions.
32 changes: 31 additions & 1 deletion maven-embedder/src/main/java/org/apache/maven/cli/MavenCli.java
Original file line number Diff line number Diff line change
Expand Up @@ -1700,8 +1700,38 @@ private void enableOnAbsentOption( final CommandLine commandLine,

int calculateDegreeOfConcurrencyWithCoreMultiplier( String threadConfiguration )
{
if ( threadConfiguration.startsWith( "C" ) )
{
threadConfiguration = threadConfiguration.substring( 1 );
}
else if ( threadConfiguration.endsWith( "C" ) )
{
threadConfiguration = threadConfiguration.substring( 0, threadConfiguration.length() - 1 );
}
else
{
throw new IllegalArgumentException(
"Wrongly formatted thread core multiplier in threads argument: " + threadConfiguration );
}
float coreMultiplier;
try
{
coreMultiplier = Float.parseFloat( threadConfiguration );
}
catch ( NumberFormatException e )
{
throw new IllegalArgumentException(
"The thread core multiplier argument is not a floating point number: " + threadConfiguration );
}
if ( coreMultiplier <= 0f )
{
throw new IllegalArgumentException(
"The thread core multiplier argument has to be a positive floating point number: "
+ threadConfiguration );
}
int procs = Runtime.getRuntime().availableProcessors();
return (int) ( Float.parseFloat( threadConfiguration.replace( "C", "" ) ) * procs );
int computedCpus = (int) ( coreMultiplier * procs );
return computedCpus < 1 ? 1 : computedCpus;
}

// ----------------------------------------------------------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,10 +154,19 @@ public void testCalculateDegreeOfConcurrencyWithCoreMultiplier()
// -TC2.2
assertEquals( (int) ( cores * 2.2 ), cli.calculateDegreeOfConcurrencyWithCoreMultiplier( "2.2C" ) );

assertThrows( IllegalArgumentException.class,
() -> cli.calculateDegreeOfConcurrencyWithCoreMultiplier( "C2.2C" ) );
assertThrows( IllegalArgumentException.class,
() -> cli.calculateDegreeOfConcurrencyWithCoreMultiplier( "2C2" ) );
assertThrows( IllegalArgumentException.class,
() -> cli.calculateDegreeOfConcurrencyWithCoreMultiplier( "-2.2C" ) );
assertThrows( IllegalArgumentException.class,
() -> cli.calculateDegreeOfConcurrencyWithCoreMultiplier( "0C" ) );

assertThrows(
NumberFormatException.class,
IllegalArgumentException.class,
() -> cli.calculateDegreeOfConcurrencyWithCoreMultiplier( "CXXX" ),
"Should have failed with a NumberFormatException" );
"Should have failed with a IllegalArgumentException" );
}

@Test
Expand Down

0 comments on commit a2ed3bd

Please sign in to comment.