Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update basic test with 2 states #18

Merged
merged 7 commits into from
Oct 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions core/src/main/java/iwf/core/StateDecision.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,21 @@ public abstract class StateDecision {

public static final StateDecision WAIT_FOR_MORE_RESULTS = ImmutableStateDecision.builder().waitForMoreCommandResults(true).build();

public static StateDecision completeWorkflow(Object output) {
public static StateDecision gracefulCompleteWorkflow(final Object output) {
return ImmutableStateDecision.builder().nextStates(Arrays.asList(
StateMovement.completeWorkflow(output)
StateMovement.gracefulCompleteWorkflow(output)
)).build();
}

public static StateDecision singleNextState(final String stateId, final Object stateInput) {
return ImmutableStateDecision.builder().nextStates(Arrays.asList(
ImmutableStateMovement.builder().stateId(stateId)
.nextStateInput(stateInput)
.build()
)).build();
}

public static StateDecision multiNextStates(final StateMovement... stateMovements) {
return ImmutableStateDecision.builder().nextStates(Arrays.asList(stateMovements)).build();
}
}
18 changes: 18 additions & 0 deletions core/src/main/java/iwf/core/StateDef.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,22 @@ public abstract class StateDef {

// indicates if this state can be used to start a workflow
public abstract boolean getCanStartWorkflow();

public static StateDef startingState(WorkflowState state) {
return ImmutableStateDef.builder()
.canStartWorkflow(true)
.workflowState(
state
)
.build();
}

public static StateDef nonStartingState(WorkflowState state) {
return ImmutableStateDef.builder()
.canStartWorkflow(false)
.workflowState(
state
)
.build();
}
}
13 changes: 12 additions & 1 deletion core/src/main/java/iwf/core/StateMovement.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,20 @@ public abstract class StateMovement {
public static final StateMovement GRACEFUL_COMPLETING_WORKFLOW = ImmutableStateMovement.builder().stateId(GRACEFUL_COMPLETING_WORKFLOW_STATE_ID).build();
public static final StateMovement FORCE_FAILING_WORKFLOW_MOVEMENT = ImmutableStateMovement.builder().stateId(FORCE_FAILING_WORKFLOW_STATE_ID).build();

public static StateMovement completeWorkflow(Object output) {
public static StateMovement gracefulCompleteWorkflow(final Object output) {
return ImmutableStateMovement.builder().stateId(GRACEFUL_COMPLETING_WORKFLOW_STATE_ID)
.nextStateInput(output)
.build();
}

public static StateMovement create(final String stateId, final Object stateInput) {
return ImmutableStateMovement.builder().stateId(stateId)
.nextStateInput(stateInput)
.build();
}

public static StateMovement create(final String stateId) {
return ImmutableStateMovement.builder().stateId(stateId)
.build();
}
}
2 changes: 0 additions & 2 deletions core/src/main/java/iwf/core/WorkflowStartOptions.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package iwf.core;

import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import org.immutables.value.Value;

@Value.Immutable
@JsonDeserialize(as = ImmutableWorkflowStartOptions.class)
public abstract class WorkflowStartOptions {
public abstract Integer getWorkflowTimeoutSeconds();

Expand Down
2 changes: 0 additions & 2 deletions core/src/main/java/iwf/core/WorkflowState.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ default StateOptions getStateOptions() {
* @param queryAttributes the query attributes that can be used as readOnly
* @param searchAttributes the search attributes that can be used as readOnly
* @return the requested commands for this step
* NOTE: it's readonly here for simplifying the implementation(execute can be reverted in some edge cases),
* We could change to support R+W if necessary.
*/
CommandRequest start(
final Context context, I input,
Expand Down
10 changes: 5 additions & 5 deletions core/src/test/java/iwf/integ/basic/BasicTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import iwf.core.Client;
import iwf.core.ClientOptions;
import iwf.core.ImmutableWorkflowStartOptions;
import iwf.core.Registry;
import iwf.core.WorkflowStartOptions;
import iwf.spring.TestSingletonWorkerService;
Expand All @@ -18,17 +17,18 @@ public void setup() {
}

@Test
public void testBasicWorkflow() {
public void testBasicWorkflow() throws InterruptedException {
final Registry registry = new Registry();
final BasicWorkflow wf = new BasicWorkflow();
registry.addWorkflow(wf);

final Client client = new Client(registry, ClientOptions.localDefault);
final String wfId = "basic-test-id" + System.currentTimeMillis() / 1000;
final WorkflowStartOptions startOptions = WorkflowStartOptions.minimum(10);
client.StartWorkflow(BasicWorkflow.class, BasicWorkflowS1.StateId, startOptions, wfId, startOptions);
final Integer input = new Integer(0);
client.StartWorkflow(BasicWorkflow.class, BasicWorkflowS1.StateId, input, wfId, startOptions);
// wait for workflow to finish
final ImmutableWorkflowStartOptions output = client.GetSingleWorkflowStateOutputWithLongWait(ImmutableWorkflowStartOptions.class, wfId);
Assertions.assertEquals(startOptions, output);
final Integer output = client.GetSingleWorkflowStateOutputWithLongWait(Integer.class, wfId);
Assertions.assertEquals(input + 2, output);
}
}
10 changes: 2 additions & 8 deletions core/src/test/java/iwf/integ/basic/BasicWorkflow.java
Original file line number Diff line number Diff line change
@@ -1,22 +1,16 @@
package iwf.integ.basic;

import iwf.core.ImmutableStateDef;
import iwf.core.StateDef;
import iwf.core.Workflow;

import java.util.Arrays;
import java.util.List;

public class BasicWorkflow implements Workflow {
@Override
public List<StateDef> getStates() {
return Arrays.asList(
ImmutableStateDef.builder()
.canStartWorkflow(true)
.workflowState(
new BasicWorkflowS1()
)
.build()
StateDef.startingState(new BasicWorkflowS1()),
StateDef.nonStartingState(new BasicWorkflowS2())
);
}
}
15 changes: 7 additions & 8 deletions core/src/test/java/iwf/integ/basic/BasicWorkflowS1.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import iwf.core.Context;
import iwf.core.StateDecision;
import iwf.core.WorkflowStartOptions;
import iwf.core.WorkflowState;
import iwf.core.attributes.QueryAttributesRW;
import iwf.core.attributes.SearchAttributesRW;
Expand All @@ -11,7 +10,7 @@
import iwf.core.command.CommandRequest;
import iwf.core.command.CommandResults;

public class BasicWorkflowS1 implements WorkflowState {
public class BasicWorkflowS1 implements WorkflowState<Integer> {

public static final String StateId = "S1";

Expand All @@ -21,18 +20,18 @@ public String getStateId() {
}

@Override
public Class getInputType() {
return WorkflowStartOptions.class;
public Class<Integer> getInputType() {
return Integer.class;
}

@Override
public CommandRequest start(final Context context, final Object input, final StateLocalAttributesW stateLocals, final SearchAttributesRW searchAttributes, final QueryAttributesRW queryAttributes) {
public CommandRequest start(final Context context, final Integer input, final StateLocalAttributesW stateLocals, final SearchAttributesRW searchAttributes, final QueryAttributesRW queryAttributes) {
return CommandRequest.empty;
}

@Override
public StateDecision decide(final Context context, final Object input, final CommandResults commandResults, final StateLocalAttributesR stateLocals, final SearchAttributesRW searchAttributes, final QueryAttributesRW queryAttributes) {
final StateDecision out = StateDecision.completeWorkflow(input);
return out;
public StateDecision decide(final Context context, final Integer input, final CommandResults commandResults, final StateLocalAttributesR stateLocals, final SearchAttributesRW searchAttributes, final QueryAttributesRW queryAttributes) {
final int output = input + 1;
return StateDecision.singleNextState(BasicWorkflowS2.StateId, output);
}
}
37 changes: 37 additions & 0 deletions core/src/test/java/iwf/integ/basic/BasicWorkflowS2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package iwf.integ.basic;

import iwf.core.Context;
import iwf.core.StateDecision;
import iwf.core.WorkflowState;
import iwf.core.attributes.QueryAttributesRW;
import iwf.core.attributes.SearchAttributesRW;
import iwf.core.attributes.StateLocalAttributesR;
import iwf.core.attributes.StateLocalAttributesW;
import iwf.core.command.CommandRequest;
import iwf.core.command.CommandResults;

public class BasicWorkflowS2 implements WorkflowState<Integer> {

public static final String StateId = "S2";

@Override
public String getStateId() {
return StateId;
}

@Override
public Class<Integer> getInputType() {
return Integer.class;
}

@Override
public CommandRequest start(final Context context, final Integer input, final StateLocalAttributesW stateLocals, final SearchAttributesRW searchAttributes, final QueryAttributesRW queryAttributes) {
return CommandRequest.empty;
}

@Override
public StateDecision decide(final Context context, final Integer input, final CommandResults commandResults, final StateLocalAttributesR stateLocals, final SearchAttributesRW searchAttributes, final QueryAttributesRW queryAttributes) {
final int output = input + 1;
return StateDecision.gracefulCompleteWorkflow(output);
}
}