Skip to content

Commit e56b6fa

Browse files
committed
[Fix #897] Fixing flaky test
Signed-off-by: fjtirado <[email protected]>
1 parent a996b4a commit e56b6fa

File tree

4 files changed

+36
-47
lines changed

4 files changed

+36
-47
lines changed

impl/core/src/main/java/io/serverlessworkflow/impl/executors/RunScriptExecutor.java

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,10 @@
3939
public class RunScriptExecutor implements RunnableTask<RunScript> {
4040

4141
private Optional<WorkflowValueResolver<Map<String, Object>>> environmentExpr;
42-
4342
private Optional<WorkflowValueResolver<Map<String, Object>>> argumentExpr;
44-
4543
private WorkflowValueResolver<String> codeSupplier;
4644
private boolean isAwait;
47-
private RunTaskConfiguration.ProcessReturnType returnType;
45+
private Optional<RunTaskConfiguration.ProcessReturnType> returnType;
4846
private ScriptRunner taskRunner;
4947

5048
@Override
@@ -65,7 +63,7 @@ public void init(RunScript taskConfiguration, WorkflowDefinition definition) {
6563

6664
this.isAwait = taskConfiguration.isAwait();
6765

68-
this.returnType = taskConfiguration.getReturn();
66+
this.returnType = Optional.ofNullable(taskConfiguration.getReturn());
6967

7068
WorkflowApplication application = definition.application();
7169
this.environmentExpr =
@@ -102,22 +100,24 @@ public void init(RunScript taskConfiguration, WorkflowDefinition definition) {
102100
@Override
103101
public CompletableFuture<WorkflowModel> apply(
104102
WorkflowContext workflowContext, TaskContext taskContext, WorkflowModel input) {
105-
return CompletableFuture.supplyAsync(
106-
() ->
107-
taskRunner.runScript(
108-
new ScriptContext(
109-
argumentExpr
110-
.map(m -> m.apply(workflowContext, taskContext, input))
111-
.orElse(Map.of()),
112-
environmentExpr
113-
.map(m -> m.apply(workflowContext, taskContext, input))
114-
.orElse(Map.of()),
115-
codeSupplier.apply(workflowContext, taskContext, input),
116-
isAwait,
117-
returnType),
118-
workflowContext,
119-
taskContext,
120-
input));
103+
ScriptContext scriptContext =
104+
new ScriptContext(
105+
argumentExpr.map(m -> m.apply(workflowContext, taskContext, input)).orElse(Map.of()),
106+
environmentExpr.map(m -> m.apply(workflowContext, taskContext, input)).orElse(Map.of()),
107+
codeSupplier.apply(workflowContext, taskContext, input),
108+
returnType);
109+
if (isAwait) {
110+
return CompletableFuture.supplyAsync(
111+
() -> taskRunner.runScript(scriptContext, workflowContext, taskContext, input),
112+
workflowContext.definition().application().executorService());
113+
} else {
114+
workflowContext
115+
.definition()
116+
.application()
117+
.executorService()
118+
.submit(() -> taskRunner.runScript(scriptContext, workflowContext, taskContext, input));
119+
return CompletableFuture.completedFuture(input);
120+
}
121121
}
122122

123123
@Override

impl/core/src/main/java/io/serverlessworkflow/impl/scripts/ScriptContext.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@
1717

1818
import io.serverlessworkflow.api.types.RunTaskConfiguration;
1919
import java.util.Map;
20+
import java.util.Optional;
2021

2122
public record ScriptContext(
2223
Map<String, Object> args,
2324
Map<String, Object> envs,
2425
String code,
25-
boolean isAwait,
26-
RunTaskConfiguration.ProcessReturnType returnType) {}
26+
Optional<RunTaskConfiguration.ProcessReturnType> returnType) {}

impl/script-js/src/main/java/io/serverlessworkflow/impl/executors/script/js/JavaScriptScriptTaskRunner.java

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ public WorkflowModel runScript(
5555
WorkflowApplication application = workflowContext.definition().application();
5656
ByteArrayOutputStream stderr = new ByteArrayOutputStream();
5757
ByteArrayOutputStream stdout = new ByteArrayOutputStream();
58-
5958
try (Context ctx =
6059
Context.newBuilder()
6160
.err(stderr)
@@ -71,29 +70,26 @@ public WorkflowModel runScript(
7170
(key, val) -> {
7271
ctx.getBindings(identifier().getLang()).putMember(key, val);
7372
});
74-
7573
configureProcessEnv(ctx, script.envs());
76-
77-
if (!script.isAwait()) {
78-
application
79-
.executorService()
80-
.submit(
81-
() -> {
82-
ctx.eval(identifier().getLang(), script.code());
83-
});
84-
return application.modelFactory().fromAny(input);
85-
}
86-
8774
ctx.eval(Source.create(identifier().getLang(), script.code()));
88-
89-
return modelFromOutput(
90-
script.returnType(), application.modelFactory(), stdout, () -> stderr.toString());
75+
return script
76+
.returnType()
77+
.map(
78+
type ->
79+
modelFromOutput(
80+
type, application.modelFactory(), stdout, () -> stderr.toString()))
81+
.orElse(input);
9182
} catch (PolyglotException e) {
9283
if (e.getExitStatus() != 0 || e.isSyntaxError()) {
9384
throw new WorkflowException(WorkflowError.runtime(taskContext, e).build());
9485
} else {
95-
return modelFromOutput(
96-
script.returnType(), application.modelFactory(), stdout, () -> buildStderr(e, stderr));
86+
return script
87+
.returnType()
88+
.map(
89+
type ->
90+
modelFromOutput(
91+
type, application.modelFactory(), stdout, () -> buildStderr(e, stderr)))
92+
.orElse(input);
9793
}
9894
}
9995
}

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

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@
2121
import io.serverlessworkflow.impl.WorkflowModel;
2222
import io.serverlessworkflow.impl.executors.ProcessResult;
2323
import java.io.IOException;
24-
import java.nio.file.Files;
25-
import java.nio.file.Path;
2624
import java.util.Map;
2725
import org.assertj.core.api.SoftAssertions;
2826
import org.junit.jupiter.api.Test;
@@ -127,15 +125,10 @@ void testAwaitBehavior() throws IOException {
127125
"workflows-samples/run-shell/echo-not-awaiting.yaml");
128126
try (WorkflowApplication appl = WorkflowApplication.builder().build()) {
129127
Map<String, String> inputMap = Map.of("full_name", "Matheus Cruz");
130-
131128
WorkflowModel outputModel =
132129
appl.workflowDefinition(workflow).instance(inputMap).start().join();
133-
134-
String content = Files.readString(Path.of("/tmp/hello.txt"));
135-
136130
SoftAssertions.assertSoftly(
137131
softly -> {
138-
softly.assertThat(content).contains("hello world not awaiting (Matheus Cruz)");
139132
softly.assertThat(outputModel.asMap().get()).isEqualTo(inputMap);
140133
});
141134
}

0 commit comments

Comments
 (0)