[MNG-7756] The degree of concurrency does not support "2." as a factor

Also fix the thrown exception to report valid syntaxes without assuming the user intent (for example "0.5c" would say it's not an int...)
This commit is contained in:
Guillaume Nodet 2023-04-05 15:22:55 +02:00
parent 8421a36592
commit 26056b9b20
2 changed files with 15 additions and 30 deletions

View File

@ -48,7 +48,6 @@
import org.apache.commons.cli.Option;
import org.apache.commons.cli.ParseException;
import org.apache.commons.cli.UnrecognizedOptionException;
import org.apache.commons.lang3.math.NumberUtils;
import org.apache.maven.BuildAbort;
import org.apache.maven.InternalErrorException;
import org.apache.maven.Maven;
@ -1422,43 +1421,30 @@ private void enableOnAbsentOption(
}
int calculateDegreeOfConcurrency(String threadConfiguration) {
if (threadConfiguration.endsWith("C")) {
threadConfiguration = threadConfiguration.substring(0, threadConfiguration.length() - 1);
try {
if (threadConfiguration.endsWith("C")) {
String str = threadConfiguration.substring(0, threadConfiguration.length() - 1);
float coreMultiplier = Float.parseFloat(str);
if (!NumberUtils.isParsable(threadConfiguration)) {
throw new IllegalArgumentException("Invalid threads core multiplier value: '" + threadConfiguration
+ "C'. Supported are int and float values ending with C.");
}
if (coreMultiplier <= 0.0f) {
throw new IllegalArgumentException("Invalid threads core multiplier value: '" + threadConfiguration
+ "'. Value must be positive.");
}
float coreMultiplier = Float.parseFloat(threadConfiguration);
if (coreMultiplier <= 0.0f) {
throw new IllegalArgumentException("Invalid threads core multiplier value: '" + threadConfiguration
+ "C'. Value must be positive.");
}
int procs = Runtime.getRuntime().availableProcessors();
int threads = (int) (coreMultiplier * procs);
return threads == 0 ? 1 : threads;
} else {
if (!NumberUtils.isParsable(threadConfiguration)) {
throw new IllegalArgumentException(
"Invalid threads value: '" + threadConfiguration + "'. Supported are int values.");
}
try {
int procs = Runtime.getRuntime().availableProcessors();
int threads = (int) (coreMultiplier * procs);
return threads == 0 ? 1 : threads;
} else {
int threads = Integer.parseInt(threadConfiguration);
if (threads <= 0) {
throw new IllegalArgumentException(
"Invalid threads value: '" + threadConfiguration + "'. Value must be positive.");
}
return threads;
} catch (NumberFormatException e) {
throw new IllegalArgumentException(
"Invalid threads value: '" + threadConfiguration + "'. Supported are integer values.");
}
} catch (NumberFormatException e) {
throw new IllegalArgumentException("Invalid threads value: '" + threadConfiguration
+ "'. Supported are int and float values ending with C.");
}
}

View File

@ -158,7 +158,6 @@ public void testCalculateDegreeOfConcurrency() {
int cpus = Runtime.getRuntime().availableProcessors();
assertEquals((int) (cpus * 2.2), cli.calculateDegreeOfConcurrency("2.2C"));
assertEquals(1, cli.calculateDegreeOfConcurrency("0.0001C"));
assertThrows(IllegalArgumentException.class, () -> cli.calculateDegreeOfConcurrency("2.C"));
assertThrows(IllegalArgumentException.class, () -> cli.calculateDegreeOfConcurrency("-2.2C"));
assertThrows(IllegalArgumentException.class, () -> cli.calculateDegreeOfConcurrency("0C"));
}