I am trying to execute SWF workflow. I am running into an issue regarding state of Promise
object. My code strucutre is as below:
Methods in WorkflowClientImpl.java:
@Override
public void doSomething() {
new TryCatch() {
@Override
protected void doTry() throws Throwable {
System.out.println("Workflow Started");
Promise<SomeObject> someObject = activityClient.doAction(param1);
if(someObject.isready()) {
boolean reDo = shouldRestartWorkflow(someObject);
if(reDo) {
Promise<Void> timer = decisionContextProvider.getDecisionContext().getWorkflowClock()
.createTimer(TimeUnit.MINUTES.toSeconds(5));
continueAsNew(timer, param1);
}
}
}
@Override
protected void doCatch(Throwable e) throws Throwable {
System.err.printlnt("Error occured while workflow");
throw new RuntimeException(e);
}
};
}
@Asynchronous
private boolean shouldRestartWorkflow(@Wait Promise<SomeObject> someObject) {
if(someObject.get().getVariable() > 1)
return true;
return false;
}
@Asynchronous
public void continueAsNew(Promise<Void> timer, String param1) {
selfClient.execute(param1);
// SelfClient is instance of TempWorkflowSelfClient
}
The above code is supposed to restart the workflow when certain conditions are met. The conditions are dependent upon values populated in instance of SomeObject returned by activity method. However the code shouldRestartWorkflow
never appears to get invoked.
I tried to write a unit test for this. Below is the code:
@Before
public void setUp() throws Exception {
trace = new ArrayList<String>();
// Register activity implementation to be used during test run
TempActivitiesImpl activitiesImpl = new TempActivitiesImpl(null, null) {
@Override
public SomeObject doAction(String randomString) {
trace.add("Test Case - " + randomString);
SomeObject testObject = new SomeObject();
testObject.setVariable(true);
return testObject;
}
};
workflowTest.addActivitiesImplementation(activityImpl); //Instance to activity class
workflowTest.addWorkflowImplementationType(WorkflowImpl.class);
}
@Test
public void testWorkflowExecutionCall() throws Throwable {
WorkflowClient workflow = workflowFactory.getClient("RandomString");
Promise<Void> promise = workflow.execute("RandomString");
List<String> expected = new ArrayList<String>();
expected.add("Test Case - RandomString");
AsyncAssert.assertEqualsWaitFor("Unexpected Result", expected, trace, promise);
}
The above test case works. However if I were to remove the if(someObject.isready())
condition. I get error IllegalStateException: Not Ready
. I was able to determine the error occurs when it tries to execute the shouldRestartWorkflow()
call.
Am I doing something wrong? As far I understand, the shouldRestartWorkflow()
should wait till the SomeObject is populated and returned by activity method before proceeding.