Skip to content

Commit 9706f7f

Browse files
committed
Check protoc via jar first
Signed-off-by: Ricardo Zanini <ricardozanini@gmail.com>
1 parent 6822659 commit 9706f7f

11 files changed

+166
-45
lines changed

impl/test/src/test/java/io/serverlessworkflow/impl/test/CustomFunctionTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
import io.serverlessworkflow.impl.WorkflowApplication;
2323
import io.serverlessworkflow.impl.WorkflowException;
24-
import io.serverlessworkflow.impl.test.junit.DisabledIfBinaryUnavailable;
24+
import io.serverlessworkflow.impl.test.junit.DisabledIfPythonUnavailable;
2525
import java.io.IOException;
2626
import java.util.Map;
2727
import org.junit.jupiter.api.AfterAll;
@@ -59,7 +59,7 @@ void testCustomFunction() {
5959
.isEqualTo(404);
6060
}
6161

62-
@DisabledIfBinaryUnavailable({"python", "--version"})
62+
@DisabledIfPythonUnavailable()
6363
@ParameterizedTest
6464
@ValueSource(
6565
strings = {

impl/test/src/test/java/io/serverlessworkflow/impl/test/grpc/GrpcBiDirectionalStreamingTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
import io.serverlessworkflow.impl.WorkflowDefinition;
2424
import io.serverlessworkflow.impl.WorkflowModel;
2525
import io.serverlessworkflow.impl.test.grpc.handlers.ContributorBiDiStreamingHandler;
26-
import io.serverlessworkflow.impl.test.junit.DisabledIfBinaryUnavailable;
26+
import io.serverlessworkflow.impl.test.junit.DisabledIfProtocUnavailable;
2727
import java.io.IOException;
2828
import java.util.Collection;
2929
import java.util.Map;
@@ -32,7 +32,7 @@
3232
import org.junit.jupiter.api.BeforeAll;
3333
import org.junit.jupiter.api.Test;
3434

35-
@DisabledIfBinaryUnavailable({"protoc", "--version"})
35+
@DisabledIfProtocUnavailable
3636
public class GrpcBiDirectionalStreamingTest {
3737

3838
private static final int PORT_FOR_EXAMPLES = 5011;

impl/test/src/test/java/io/serverlessworkflow/impl/test/grpc/GrpcClientStreamingTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
import io.serverlessworkflow.impl.WorkflowApplication;
2323
import io.serverlessworkflow.impl.WorkflowDefinition;
2424
import io.serverlessworkflow.impl.test.grpc.handlers.ContributorClientStreamingHandler;
25-
import io.serverlessworkflow.impl.test.junit.DisabledIfBinaryUnavailable;
25+
import io.serverlessworkflow.impl.test.junit.DisabledIfProtocUnavailable;
2626
import java.io.IOException;
2727
import java.util.List;
2828
import java.util.Map;
@@ -31,7 +31,7 @@
3131
import org.junit.jupiter.api.BeforeAll;
3232
import org.junit.jupiter.api.Test;
3333

34-
@DisabledIfBinaryUnavailable({"protoc", "--version"})
34+
@DisabledIfProtocUnavailable
3535
public class GrpcClientStreamingTest {
3636

3737
private static final int PORT_FOR_EXAMPLES = 5011;

impl/test/src/test/java/io/serverlessworkflow/impl/test/grpc/GrpcServerStreamingTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
import io.serverlessworkflow.impl.WorkflowDefinition;
2424
import io.serverlessworkflow.impl.WorkflowModel;
2525
import io.serverlessworkflow.impl.test.grpc.handlers.ContributorServerStreamingHandler;
26-
import io.serverlessworkflow.impl.test.junit.DisabledIfBinaryUnavailable;
26+
import io.serverlessworkflow.impl.test.junit.DisabledIfProtocUnavailable;
2727
import java.io.IOException;
2828
import java.util.Collection;
2929
import java.util.Map;
@@ -32,7 +32,7 @@
3232
import org.junit.jupiter.api.BeforeAll;
3333
import org.junit.jupiter.api.Test;
3434

35-
@DisabledIfBinaryUnavailable({"protoc", "--version"})
35+
@DisabledIfProtocUnavailable
3636
public class GrpcServerStreamingTest {
3737

3838
private static final int PORT_FOR_EXAMPLES = 5011;

impl/test/src/test/java/io/serverlessworkflow/impl/test/grpc/GrpcUnaryArgsExprTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,15 @@
2222
import io.serverlessworkflow.impl.WorkflowApplication;
2323
import io.serverlessworkflow.impl.WorkflowDefinition;
2424
import io.serverlessworkflow.impl.test.grpc.handlers.ContributorUnaryArgsExprHandler;
25-
import io.serverlessworkflow.impl.test.junit.DisabledIfBinaryUnavailable;
25+
import io.serverlessworkflow.impl.test.junit.DisabledIfProtocUnavailable;
2626
import java.io.IOException;
2727
import java.util.Map;
2828
import org.assertj.core.api.Assertions;
2929
import org.junit.jupiter.api.AfterEach;
3030
import org.junit.jupiter.api.BeforeAll;
3131
import org.junit.jupiter.api.Test;
3232

33-
@DisabledIfBinaryUnavailable({"protoc", "--version"})
33+
@DisabledIfProtocUnavailable
3434
public class GrpcUnaryArgsExprTest {
3535

3636
private static final int PORT_FOR_EXAMPLES = 5011;

impl/test/src/test/java/io/serverlessworkflow/impl/test/grpc/GrpcUnaryTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,15 @@
2222
import io.serverlessworkflow.impl.WorkflowApplication;
2323
import io.serverlessworkflow.impl.WorkflowDefinition;
2424
import io.serverlessworkflow.impl.test.grpc.handlers.PersonUnaryHandler;
25-
import io.serverlessworkflow.impl.test.junit.DisabledIfBinaryUnavailable;
25+
import io.serverlessworkflow.impl.test.junit.DisabledIfProtocUnavailable;
2626
import java.io.IOException;
2727
import java.util.Map;
2828
import org.assertj.core.api.Assertions;
2929
import org.junit.jupiter.api.AfterEach;
3030
import org.junit.jupiter.api.BeforeAll;
3131
import org.junit.jupiter.api.Test;
3232

33-
@DisabledIfBinaryUnavailable({"protoc", "--version"})
33+
@DisabledIfProtocUnavailable
3434
public class GrpcUnaryTest {
3535

3636
private static final int PORT_FOR_EXAMPLES = 5011;
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Copyright 2020-Present The Serverless Workflow Specification Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.serverlessworkflow.impl.test.junit;
17+
18+
import java.io.IOException;
19+
import java.util.concurrent.ConcurrentHashMap;
20+
import java.util.concurrent.ConcurrentMap;
21+
import java.util.concurrent.TimeUnit;
22+
23+
public final class BinaryCheckUtil {
24+
25+
private BinaryCheckUtil() {}
26+
27+
private static final ConcurrentMap<String, Boolean> BINARY_CHECKS = new ConcurrentHashMap<>();
28+
29+
public static boolean isBinaryAvailable(String... command) {
30+
return BINARY_CHECKS.computeIfAbsent(
31+
String.join(" ", command),
32+
__ -> {
33+
try {
34+
Process process =
35+
new ProcessBuilder(command)
36+
.redirectErrorStream(true)
37+
.redirectOutput(ProcessBuilder.Redirect.DISCARD)
38+
.start();
39+
boolean finished = process.waitFor(2, TimeUnit.SECONDS);
40+
if (finished) {
41+
return process.exitValue() == 0;
42+
}
43+
process.destroyForcibly();
44+
return false;
45+
} catch (IOException | InterruptedException e) {
46+
if (e instanceof InterruptedException) {
47+
Thread.currentThread().interrupt();
48+
}
49+
return false;
50+
}
51+
});
52+
}
53+
}

impl/test/src/test/java/io/serverlessworkflow/impl/test/junit/DisabledIfBinaryUnavailable.java renamed to impl/test/src/test/java/io/serverlessworkflow/impl/test/junit/DisabledIfProtocUnavailable.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,5 @@
2323

2424
@Target({ElementType.TYPE, ElementType.METHOD})
2525
@Retention(RetentionPolicy.RUNTIME)
26-
@ExtendWith(BinaryCheckCondition.class)
27-
public @interface DisabledIfBinaryUnavailable {
28-
String[] value();
29-
}
26+
@ExtendWith(ProtocCheckCondition.class)
27+
public @interface DisabledIfProtocUnavailable {}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Copyright 2020-Present The Serverless Workflow Specification Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.serverlessworkflow.impl.test.junit;
17+
18+
import java.lang.annotation.ElementType;
19+
import java.lang.annotation.Retention;
20+
import java.lang.annotation.RetentionPolicy;
21+
import java.lang.annotation.Target;
22+
import org.junit.jupiter.api.extension.ExtendWith;
23+
24+
@Target({ElementType.TYPE, ElementType.METHOD})
25+
@Retention(RetentionPolicy.RUNTIME)
26+
@ExtendWith(PythonCheckCondition.class)
27+
public @interface DisabledIfPythonUnavailable {}

impl/test/src/test/java/io/serverlessworkflow/impl/test/junit/BinaryCheckCondition.java renamed to impl/test/src/test/java/io/serverlessworkflow/impl/test/junit/ProtocCheckCondition.java

Lines changed: 28 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -15,53 +15,52 @@
1515
*/
1616
package io.serverlessworkflow.impl.test.junit;
1717

18-
import java.io.IOException;
19-
import java.util.concurrent.TimeUnit;
18+
import static io.serverlessworkflow.impl.test.junit.BinaryCheckUtil.isBinaryAvailable;
19+
20+
import com.github.os72.protocjar.Protoc;
21+
import java.util.concurrent.atomic.AtomicReference;
2022
import org.junit.jupiter.api.extension.ConditionEvaluationResult;
2123
import org.junit.jupiter.api.extension.ExecutionCondition;
2224
import org.junit.jupiter.api.extension.ExtensionContext;
2325
import org.junit.platform.commons.util.AnnotationUtils;
2426

25-
public class BinaryCheckCondition implements ExecutionCondition {
27+
public class ProtocCheckCondition implements ExecutionCondition {
28+
29+
private static final AtomicReference<Boolean> protocAvailable = new AtomicReference<>(null);
2630

2731
@Override
2832
public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext context) {
29-
return AnnotationUtils.findAnnotation(context.getElement(), DisabledIfBinaryUnavailable.class)
33+
return AnnotationUtils.findAnnotation(context.getElement(), DisabledIfProtocUnavailable.class)
3034
.map(
3135
annotation -> {
32-
String[] binary = annotation.value();
33-
if (binary == null || binary.length == 0) {
36+
if (isProtocAvailable()) {
3437
return ConditionEvaluationResult.enabled(
35-
"No command found in the annotation @DisabledIfBinaryUnavailable");
36-
}
37-
if (isBinaryAvailable(binary)) {
38-
return ConditionEvaluationResult.enabled(binary[0] + " is available.");
38+
"Protoc is available for the current platform.");
3939
} else {
4040
return ConditionEvaluationResult.disabled(
41-
"Test disabled: " + binary[0] + " not found.");
41+
"Test disabled: Protoc not currently available not found.");
4242
}
4343
})
44-
.orElse(ConditionEvaluationResult.enabled("No @DisabledIfBinaryUnavailable found."));
44+
.orElse(ConditionEvaluationResult.enabled("No @DisabledIfProtocUnavailable found."));
4545
}
4646

47-
public boolean isBinaryAvailable(String... command) {
47+
private boolean isProtocAvailable() {
48+
Boolean cached = protocAvailable.get();
49+
if (cached != null) {
50+
return cached;
51+
}
52+
53+
boolean found = false;
4854
try {
49-
Process process =
50-
new ProcessBuilder(command)
51-
.redirectErrorStream(true)
52-
.redirectOutput(ProcessBuilder.Redirect.DISCARD)
53-
.start();
54-
boolean finished = process.waitFor(2, TimeUnit.SECONDS);
55-
if (finished) {
56-
return process.exitValue() == 0;
57-
}
58-
process.destroyForcibly();
59-
return false;
60-
} catch (IOException | InterruptedException e) {
61-
if (e instanceof InterruptedException) {
62-
Thread.currentThread().interrupt();
63-
}
64-
return false;
55+
if (Protoc.runProtoc(new String[] {"--version"}) == 0) found = true;
56+
} catch (Exception e) {
57+
// ignore
6558
}
59+
60+
if (!found) found = isBinaryAvailable("protoc", "--version");
61+
62+
protocAvailable.compareAndSet(null, found);
63+
64+
return protocAvailable.get();
6665
}
6766
}

0 commit comments

Comments
 (0)