Skip to content

Commit d29fef7

Browse files
committed
Adding Javi's comment
Signed-off-by: Ricardo Zanini <ricardozanini@gmail.com>
1 parent 113ce36 commit d29fef7

3 files changed

Lines changed: 34 additions & 50 deletions

File tree

impl/test/src/test/java/io/serverlessworkflow/impl/test/junit/BinaryCheckUtil.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,31 +22,31 @@
2222

2323
public final class BinaryCheckUtil {
2424

25-
private BinaryCheckUtil() {}
26-
2725
private static final ConcurrentMap<String, Boolean> BINARY_CHECKS = new ConcurrentHashMap<>();
2826

27+
private BinaryCheckUtil() {}
28+
2929
public static boolean isBinaryAvailable(String... command) {
3030
return BINARY_CHECKS.computeIfAbsent(
3131
String.join(" ", command),
3232
__ -> {
33+
Process process;
3334
try {
34-
Process process =
35+
process =
3536
new ProcessBuilder(command)
3637
.redirectErrorStream(true)
3738
.redirectOutput(ProcessBuilder.Redirect.DISCARD)
3839
.start();
39-
boolean finished = process.waitFor(2, TimeUnit.SECONDS);
40-
if (finished) {
41-
return process.exitValue() == 0;
42-
}
43-
process.destroyForcibly();
40+
} catch (IOException e) {
4441
return false;
45-
} catch (IOException | InterruptedException e) {
46-
if (e instanceof InterruptedException) {
47-
Thread.currentThread().interrupt();
48-
}
42+
}
43+
try {
44+
return process.waitFor(2, TimeUnit.SECONDS) && process.exitValue() == 0;
45+
} catch (InterruptedException e) {
46+
Thread.currentThread().interrupt();
4947
return false;
48+
} finally {
49+
process.destroyForcibly();
5050
}
5151
});
5252
}

impl/test/src/test/java/io/serverlessworkflow/impl/test/junit/ProtocCheckCondition.java

Lines changed: 18 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -18,49 +18,36 @@
1818
import static io.serverlessworkflow.impl.test.junit.BinaryCheckUtil.isBinaryAvailable;
1919

2020
import com.github.os72.protocjar.Protoc;
21-
import java.util.concurrent.atomic.AtomicReference;
2221
import org.junit.jupiter.api.extension.ConditionEvaluationResult;
2322
import org.junit.jupiter.api.extension.ExecutionCondition;
2423
import org.junit.jupiter.api.extension.ExtensionContext;
2524
import org.junit.platform.commons.util.AnnotationUtils;
2625

2726
public class ProtocCheckCondition implements ExecutionCondition {
2827

29-
private static final AtomicReference<Boolean> protocAvailable = new AtomicReference<>(null);
28+
private static boolean protocAvailable = false;
3029

31-
@Override
32-
public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext context) {
33-
return AnnotationUtils.findAnnotation(context.getElement(), DisabledIfProtocUnavailable.class)
34-
.map(
35-
annotation -> {
36-
if (isProtocAvailable()) {
37-
return ConditionEvaluationResult.enabled(
38-
"Protoc is available for the current platform.");
39-
} else {
40-
return ConditionEvaluationResult.disabled(
41-
"Test disabled: Protoc not currently available not found.");
42-
}
43-
})
44-
.orElse(ConditionEvaluationResult.enabled("No @DisabledIfProtocUnavailable found."));
45-
}
46-
47-
private boolean isProtocAvailable() {
48-
Boolean cached = protocAvailable.get();
49-
if (cached != null) {
50-
return cached;
51-
}
52-
53-
boolean found = false;
30+
static {
5431
try {
55-
if (Protoc.runProtoc(new String[] {"--version"}) == 0) found = true;
32+
protocAvailable = Protoc.runProtoc(new String[] {"--version"}) == 0;
5633
} catch (Exception e) {
5734
// ignore
5835
}
36+
if (!protocAvailable) {
37+
protocAvailable = isBinaryAvailable("protoc", "--version");
38+
}
39+
}
5940

60-
if (!found) found = isBinaryAvailable("protoc", "--version");
61-
62-
protocAvailable.compareAndSet(null, found);
63-
64-
return protocAvailable.get();
41+
@Override
42+
public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext context) {
43+
return AnnotationUtils.findAnnotation(context.getElement(), DisabledIfProtocUnavailable.class)
44+
.map(
45+
annotation ->
46+
protocAvailable
47+
? ConditionEvaluationResult.enabled(
48+
"Protoc is available for the current platform.")
49+
: ConditionEvaluationResult.disabled(
50+
"Test disabled: Protoc not currently available not found."))
51+
.orElse(ConditionEvaluationResult.enabled("No @DisabledIfProtocUnavailable found."));
6552
}
6653
}

impl/test/src/test/java/io/serverlessworkflow/impl/test/junit/PythonCheckCondition.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,10 @@ public class PythonCheckCondition implements ExecutionCondition {
3232
public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext context) {
3333
return AnnotationUtils.findAnnotation(context.getElement(), DisabledIfPythonUnavailable.class)
3434
.map(
35-
annotation -> {
36-
if (isBinaryAvailable("python", "--version")) {
37-
return ConditionEvaluationResult.enabled("Python is available.");
38-
} else {
39-
return ConditionEvaluationResult.disabled("Test disabled: Python not found.");
40-
}
41-
})
35+
annotation ->
36+
isBinaryAvailable("python", "--version")
37+
? ConditionEvaluationResult.enabled("Python is available.")
38+
: ConditionEvaluationResult.disabled("Test disabled: Python not found."))
4239
.orElse(ConditionEvaluationResult.enabled("No @DisabledIfBinaryUnavailable found."));
4340
}
4441
}

0 commit comments

Comments
 (0)