builder.transition().from("B").to("C").on("SECOND");
builder.transition().from("A").to("D").on("SECOND");
builder.transition().from("C").to("E").on("SECOND");
final UntypedStateMachine fsm = builder.newStateMachine("A");
// thread 1 start process event "FIRST"
new Thread(new Runnable() {
@Override
public void run() {
fsm.fire("FIRST");
eventCondition.countDown();
}
}, "Test-Thread-1").start();
// thread 2 read fsm state while processing event "FIRST"
new Thread(new Runnable() {
@Override
public void run() {
try {
actionCondition.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
readStateRef.set(fsm.getCurrentState());
eventCondition.countDown();
}
}, "Test-Thread-2").start();
// thread 3 test event "SECOND" while processing event "FIRST"
new Thread(new Runnable() {
@Override
public void run() {
try {
actionCondition.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
testStateRef.set(fsm.test("SECOND"));
eventCondition.countDown();
}
}, "Test-Thread-3").start();
// thread 4 dump data while processing event "FIRST"
new Thread(new Runnable() {
@Override
public void run() {
try {
actionCondition.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
dumpDataRef.set(fsm.dumpSavedData());
eventCondition.countDown();
}
}, "Test-Thread-4").start();
// thread 5 process event "SECOND" while processing event "FIRST"
new Thread(new Runnable() {
@Override
public void run() {
try {
actionCondition.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
fsm.fire("SECOND");
eventCondition.countDown();
}
}, "Test-Thread-5").start();
// wait for all threads finish processing events then check result
try {
eventCondition.await(1000, TimeUnit.MILLISECONDS);
TimeUnit.MILLISECONDS.sleep(TIME_INTERVAL);
} catch (InterruptedException e) {
fail();
}
assertEquals(fsm.getCurrentState(), "C");
assertEquals(readStateRef.get(), "C");
assertEquals(testStateRef.get(), "E");
assertNotNull(dumpDataRef.get());
assertEquals(((StateMachineData.Reader)dumpDataRef.get()).currentState(), "C");
Object testAgain = fsm.test("SECOND");
assertEquals(testAgain, "E");
}