Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
import com.alibaba.cola.statemachine.StateMachine;
import com.alibaba.cola.statemachine.builder.StateMachineBuilder;
import com.alibaba.cola.statemachine.builder.StateMachineBuilderFactory;
import java.util.UUID;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;


/**
* @author dingchenchen
* @since 2021/1/6
Expand Down Expand Up @@ -54,7 +54,8 @@ public void testChoice(){
.when(checkCondition3())
.perform(doAction());

StateMachine<StateMachineTest.States, StateMachineTest.Events, Context> stateMachine = builder.build("ChoiceConditionMachine");
String uniqueId = "ChoiceConditionMachine" + UUID.randomUUID().toString();
StateMachine<StateMachineTest.States, StateMachineTest.Events, Context> stateMachine = builder.build(uniqueId);
StateMachineTest.States target1 = stateMachine.fireEvent(StateMachineTest.States.STATE1, StateMachineTest.Events.EVENT1, new Context("1"));
Assertions.assertEquals(StateMachineTest.States.STATE1,target1);
StateMachineTest.States target2 = stateMachine.fireEvent(StateMachineTest.States.STATE1, StateMachineTest.Events.EVENT1, new Context("2"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.alibaba.cola.statemachine.StateMachine;
import com.alibaba.cola.statemachine.builder.StateMachineBuilder;
import com.alibaba.cola.statemachine.builder.StateMachineBuilderFactory;
import java.util.UUID;
import com.alibaba.cola.statemachine.impl.Debugger;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -168,7 +169,8 @@ public void testPlantUML(){
.when(checkCondition())
.perform(doAction()));

StateMachine stateMachine = builder.build("AdjustPriceTask");
String uniqueId = "AdjustPriceTask" + UUID.randomUUID().toString();
StateMachine stateMachine = builder.build(uniqueId);
String plantUML = stateMachine.generatePlantUML();
System.out.println(plantUML);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.junit.jupiter.api.Test;


import java.util.UUID;
import java.util.List;

/**
Expand Down Expand Up @@ -54,7 +55,8 @@ public void testExternalNormal() {
.when(checkCondition())
.perform(doAction());

StateMachine<States, Events, Context> stateMachine = builder.build(MACHINE_ID);
String uniqueId = MACHINE_ID + UUID.randomUUID().toString();
StateMachine<States, Events, Context> stateMachine = builder.build(uniqueId);
States target = stateMachine.fireEvent(States.STATE1, Events.EVENT1, new Context());
Assertions.assertEquals(States.STATE2, target);
}
Expand All @@ -70,8 +72,8 @@ public void testFail() {
.perform(doAction());

builder.setFailCallback(new AlertFailCallback<>());

StateMachine<States, Events, Context> stateMachine = builder.build(MACHINE_ID + "-testFail");
String uniqueId = MACHINE_ID + "-testFail" + UUID.randomUUID().toString();
StateMachine<States, Events, Context> stateMachine = builder.build(uniqueId);
Assertions.assertThrows(TransitionFailException.class,
() -> stateMachine.fireEvent(States.STATE2, Events.EVENT1, new Context()));
}
Expand All @@ -86,7 +88,8 @@ public void testVerify() {
.when(checkCondition())
.perform(doAction());

StateMachine<States, Events, Context> stateMachine = builder.build(MACHINE_ID + "-testVerify");
String uniqueId = MACHINE_ID + "-testVerify" + UUID.randomUUID().toString();
StateMachine<States, Events, Context> stateMachine = builder.build(uniqueId);

Assertions.assertTrue(stateMachine.verify(States.STATE1, Events.EVENT1));
Assertions.assertFalse(stateMachine.verify(States.STATE1, Events.EVENT2));
Expand All @@ -102,7 +105,8 @@ public void testExternalTransitionsNormal() {
.when(checkCondition())
.perform(doAction());

StateMachine<States, Events, Context> stateMachine = builder.build(MACHINE_ID + "1");
String uniqueId = MACHINE_ID + "1" + UUID.randomUUID().toString();
StateMachine<States, Events, Context> stateMachine = builder.build(uniqueId);
States target = stateMachine.fireEvent(States.STATE2, Events.EVENT1, new Context());
Assertions.assertEquals(States.STATE4, target);
}
Expand All @@ -115,7 +119,8 @@ public void testInternalNormal() {
.on(Events.INTERNAL_EVENT)
.when(checkCondition())
.perform(doAction());
StateMachine<States, Events, Context> stateMachine = builder.build(MACHINE_ID + "2");
String uniqueId = MACHINE_ID + "2" + UUID.randomUUID().toString();
StateMachine<States, Events, Context> stateMachine = builder.build(uniqueId);

stateMachine.fireEvent(States.STATE1, Events.EVENT1, new Context());
States target = stateMachine.fireEvent(States.STATE1, Events.INTERNAL_EVENT, new Context());
Expand All @@ -124,7 +129,8 @@ public void testInternalNormal() {

@Test
public void testExternalInternalNormal() {
StateMachine<States, Events, Context> stateMachine = buildStateMachine("testExternalInternalNormal");
String uniqueId = "testExternalInternalNormal" + UUID.randomUUID().toString();
StateMachine<States, Events, Context> stateMachine = buildStateMachine(uniqueId);

Context context = new Context();
States target = stateMachine.fireEvent(States.STATE1, Events.EVENT1, context);
Expand Down Expand Up @@ -182,7 +188,8 @@ private StateMachine<States, Events, Context> buildStateMachine(String machineId

@Test
public void testMultiThread() {
buildStateMachine("testMultiThread");
String uniqueId = "testMultiThread" + UUID.randomUUID().toString();
buildStateMachine(uniqueId);

for (int i = 0; i < 10; i++) {
Thread thread = new Thread(() -> {
Expand Down Expand Up @@ -227,7 +234,8 @@ public void testParallel(){
.on(StateMachineTest.Events.EVENT2)
.when(checkCondition())
.perform(doAction());
StateMachine<States, Events, Context> stateMachine = builder.build("ParallelMachine");
String uniqueId = "ParallelMachine" + UUID.randomUUID().toString();
StateMachine<States, Events, Context> stateMachine = builder.build(uniqueId);
System.out.println(stateMachine.generatePlantUML());
List<States> states = stateMachine.fireParallelEvent(StateMachineTest.States.STATE1, StateMachineTest.Events.EVENT1, new Context());
for (StateMachineTest.States state : states) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.alibaba.cola.statemachine.builder.StateMachineBuilder;
import com.alibaba.cola.statemachine.builder.StateMachineBuilderFactory;
import com.alibaba.cola.statemachine.impl.StateMachineException;
import java.util.UUID;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

Expand All @@ -27,7 +28,8 @@ public void testConditionNotMeet(){
.when(checkConditionFalse())
.perform(doAction());

StateMachine<StateMachineTest.States, StateMachineTest.Events, StateMachineTest.Context> stateMachine = builder.build("NotMeetConditionMachine");
String uniqueId = "NotMeetConditionMachine" + UUID.randomUUID().toString();
StateMachine<StateMachineTest.States, StateMachineTest.Events, StateMachineTest.Context> stateMachine = builder.build(uniqueId);
StateMachineTest.States target = stateMachine.fireEvent(StateMachineTest.States.STATE1, StateMachineTest.Events.EVENT1, new StateMachineTest.Context());
Assertions.assertEquals(StateMachineTest.States.STATE1,target);
}
Expand Down