Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support overriding state options (#193)
Co-authored-by: Kaili Zhu <kzhu@indeed.com>
- Loading branch information
Showing
5 changed files
with
239 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
src/test/java/io/iworkflow/integ/StateOptionsOverrideTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package io.iworkflow.integ; | ||
|
||
import io.iworkflow.core.Client; | ||
import io.iworkflow.core.ClientOptions; | ||
import io.iworkflow.core.ImmutableWorkflowOptions; | ||
import io.iworkflow.core.WorkflowOptions; | ||
import io.iworkflow.gen.models.IDReusePolicy; | ||
import io.iworkflow.integ.stateoptionsoverride.StateOptionsOverrideWorkflow; | ||
import io.iworkflow.spring.TestSingletonWorkerService; | ||
import io.iworkflow.spring.controller.WorkflowRegistry; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.concurrent.ExecutionException; | ||
|
||
public class StateOptionsOverrideTest { | ||
@BeforeEach | ||
public void setup() throws ExecutionException, InterruptedException { | ||
TestSingletonWorkerService.startWorkerIfNotUp(); | ||
} | ||
@Test | ||
public void testStateOptionsOverrideWorkflow() throws InterruptedException { | ||
final Client client = new Client(WorkflowRegistry.registry, ClientOptions.localDefault); | ||
final String wfId = "state-options-override-test-id" + System.currentTimeMillis() / 1000; | ||
final WorkflowOptions startOptions = ImmutableWorkflowOptions.builder() | ||
.workflowIdReusePolicy(IDReusePolicy.DISALLOW_REUSE) | ||
.build(); | ||
final String input = "input"; | ||
client.startWorkflow(StateOptionsOverrideWorkflow.class, wfId, 10, input, startOptions); | ||
// wait for workflow to finish | ||
final String output = client.getSimpleWorkflowResultWithWait(String.class, wfId); | ||
Assertions.assertEquals("input_state1_start_state1_decide_state2_start_state2_decide", output); | ||
} | ||
} |
84 changes: 84 additions & 0 deletions
84
src/test/java/io/iworkflow/integ/stateoptionsoverride/StateOptionsOverrideWorkflow.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package io.iworkflow.integ.stateoptionsoverride; | ||
|
||
import io.iworkflow.core.Context; | ||
import io.iworkflow.core.ObjectWorkflow; | ||
import io.iworkflow.core.StateDecision; | ||
import io.iworkflow.core.StateDef; | ||
import io.iworkflow.core.WorkflowState; | ||
import io.iworkflow.core.command.CommandRequest; | ||
import io.iworkflow.core.command.CommandResults; | ||
import io.iworkflow.core.communication.Communication; | ||
import io.iworkflow.core.persistence.Persistence; | ||
import io.iworkflow.gen.models.RetryPolicy; | ||
import io.iworkflow.gen.models.WaitUntilApiFailurePolicy; | ||
import io.iworkflow.gen.models.WorkflowStateOptions; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
@Component | ||
public class StateOptionsOverrideWorkflow implements ObjectWorkflow { | ||
|
||
@Override | ||
public List<StateDef> getWorkflowStates() { | ||
return Arrays.asList( | ||
StateDef.startingState(new StateOptionsOverrideWorkflowState1()), | ||
StateDef.nonStartingState(new StateOptionsOverrideWorkflowState2()) | ||
); | ||
} | ||
} | ||
|
||
class StateOptionsOverrideWorkflowState1 implements WorkflowState<String> { | ||
private String output = ""; | ||
|
||
@Override | ||
public Class<String> getInputType() { | ||
return String.class; | ||
} | ||
|
||
@Override | ||
public CommandRequest waitUntil(Context context, String input, Persistence persistence, Communication communication) { | ||
output = input + "_state1_start"; | ||
return CommandRequest.empty; | ||
} | ||
|
||
@Override | ||
public StateDecision execute(Context context, String input, CommandResults commandResults, Persistence persistence, Communication communication) { | ||
output = output + "_state1_decide"; | ||
return StateDecision.singleNextState( | ||
StateOptionsOverrideWorkflowState2.class, output, | ||
new WorkflowStateOptions() | ||
.waitUntilApiRetryPolicy(new RetryPolicy().maximumAttempts(2)) | ||
.waitUntilApiFailurePolicy(WaitUntilApiFailurePolicy.PROCEED_ON_FAILURE) | ||
); | ||
} | ||
} | ||
|
||
class StateOptionsOverrideWorkflowState2 implements WorkflowState<String> { | ||
private String output = ""; | ||
|
||
@Override | ||
public Class<String> getInputType() { | ||
return String.class; | ||
} | ||
|
||
@Override | ||
public CommandRequest waitUntil(Context context, String input, Persistence persistence, Communication communication) { | ||
output = input + "_state2_start"; | ||
throw new RuntimeException(""); | ||
} | ||
|
||
@Override | ||
public StateDecision execute(Context context, String input, CommandResults commandResults, Persistence persistence, Communication communication) { | ||
output = output + "_state2_decide"; | ||
return StateDecision.gracefulCompleteWorkflow(output); | ||
} | ||
|
||
@Override | ||
public WorkflowStateOptions getStateOptions() { | ||
return new WorkflowStateOptions() | ||
.waitUntilApiRetryPolicy(new RetryPolicy().maximumAttempts(1)) | ||
.waitUntilApiFailurePolicy(WaitUntilApiFailurePolicy.FAIL_WORKFLOW_ON_FAILURE); | ||
} | ||
} |