From 281003535c3c2cd8d0e1b6a0457efe2b2eae3793 Mon Sep 17 00:00:00 2001 From: Kaiyao Ke Date: Thu, 25 Apr 2024 18:30:46 -0500 Subject: [PATCH] fix non-idempotent test --- .../cola/test/StateMachineChoiceTest.java | 4 ++- .../cola/test/StateMachinePlantUMLTest.java | 4 ++- .../alibaba/cola/test/StateMachineTest.java | 25 +++++++++++++------ .../cola/test/StateMachineUnNormalTest.java | 4 ++- 4 files changed, 26 insertions(+), 11 deletions(-) diff --git a/cola-components/cola-component-statemachine/src/test/java/com/alibaba/cola/test/StateMachineChoiceTest.java b/cola-components/cola-component-statemachine/src/test/java/com/alibaba/cola/test/StateMachineChoiceTest.java index 6cea7bcdd..61e6e63e4 100644 --- a/cola-components/cola-component-statemachine/src/test/java/com/alibaba/cola/test/StateMachineChoiceTest.java +++ b/cola-components/cola-component-statemachine/src/test/java/com/alibaba/cola/test/StateMachineChoiceTest.java @@ -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 org.junit.Assert; import org.junit.Test; @@ -53,7 +54,8 @@ public void testChoice(){ .when(checkCondition3()) .perform(doAction()); - StateMachine stateMachine = builder.build("ChoiceConditionMachine"); + String uniqueId = "ChoiceConditionMachine" + UUID.randomUUID().toString(); + StateMachine stateMachine = builder.build(uniqueId); StateMachineTest.States target1 = stateMachine.fireEvent(StateMachineTest.States.STATE1, StateMachineTest.Events.EVENT1, new Context("1")); Assert.assertEquals(StateMachineTest.States.STATE1,target1); StateMachineTest.States target2 = stateMachine.fireEvent(StateMachineTest.States.STATE1, StateMachineTest.Events.EVENT1, new Context("2")); diff --git a/cola-components/cola-component-statemachine/src/test/java/com/alibaba/cola/test/StateMachinePlantUMLTest.java b/cola-components/cola-component-statemachine/src/test/java/com/alibaba/cola/test/StateMachinePlantUMLTest.java index 84e02d4d6..5a6792d57 100644 --- a/cola-components/cola-component-statemachine/src/test/java/com/alibaba/cola/test/StateMachinePlantUMLTest.java +++ b/cola-components/cola-component-statemachine/src/test/java/com/alibaba/cola/test/StateMachinePlantUMLTest.java @@ -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.Before; import org.junit.Test; @@ -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); diff --git a/cola-components/cola-component-statemachine/src/test/java/com/alibaba/cola/test/StateMachineTest.java b/cola-components/cola-component-statemachine/src/test/java/com/alibaba/cola/test/StateMachineTest.java index c20997edd..6baf6db9e 100644 --- a/cola-components/cola-component-statemachine/src/test/java/com/alibaba/cola/test/StateMachineTest.java +++ b/cola-components/cola-component-statemachine/src/test/java/com/alibaba/cola/test/StateMachineTest.java @@ -12,6 +12,7 @@ import org.junit.Assert; import org.junit.Test; +import java.util.UUID; import java.util.List; /** @@ -54,7 +55,8 @@ public void testExternalNormal() { .when(checkCondition()) .perform(doAction()); - StateMachine stateMachine = builder.build(MACHINE_ID); + String uniqueId = MACHINE_ID + UUID.randomUUID().toString(); + StateMachine stateMachine = builder.build(uniqueId); States target = stateMachine.fireEvent(States.STATE1, Events.EVENT1, new Context()); Assert.assertEquals(States.STATE2, target); } @@ -71,7 +73,8 @@ public void testFail() { builder.setFailCallback(new AlertFailCallback<>()); - StateMachine stateMachine = builder.build(MACHINE_ID + "-testFail"); + String uniqueId = MACHINE_ID + "-testFail" + UUID.randomUUID().toString(); + StateMachine stateMachine = builder.build(uniqueId); Assert.assertThrows(TransitionFailException.class, () -> stateMachine.fireEvent(States.STATE2, Events.EVENT1, new Context())); } @@ -86,7 +89,8 @@ public void testVerify() { .when(checkCondition()) .perform(doAction()); - StateMachine stateMachine = builder.build(MACHINE_ID + "-testVerify"); + String uniqueId = MACHINE_ID + "-testVerify" + UUID.randomUUID().toString(); + StateMachine stateMachine = builder.build(uniqueId); Assert.assertTrue(stateMachine.verify(States.STATE1, Events.EVENT1)); Assert.assertFalse(stateMachine.verify(States.STATE1, Events.EVENT2)); @@ -102,7 +106,8 @@ public void testExternalTransitionsNormal() { .when(checkCondition()) .perform(doAction()); - StateMachine stateMachine = builder.build(MACHINE_ID + "1"); + String uniqueId = MACHINE_ID + "1" + UUID.randomUUID().toString(); + StateMachine stateMachine = builder.build(uniqueId); States target = stateMachine.fireEvent(States.STATE2, Events.EVENT1, new Context()); Assert.assertEquals(States.STATE4, target); } @@ -115,7 +120,8 @@ public void testInternalNormal() { .on(Events.INTERNAL_EVENT) .when(checkCondition()) .perform(doAction()); - StateMachine stateMachine = builder.build(MACHINE_ID + "2"); + String uniqueId = MACHINE_ID + "2" + UUID.randomUUID().toString(); + StateMachine stateMachine = builder.build(uniqueId); stateMachine.fireEvent(States.STATE1, Events.EVENT1, new Context()); States target = stateMachine.fireEvent(States.STATE1, Events.INTERNAL_EVENT, new Context()); @@ -124,7 +130,8 @@ public void testInternalNormal() { @Test public void testExternalInternalNormal() { - StateMachine stateMachine = buildStateMachine("testExternalInternalNormal"); + String uniqueId = "testExternalInternalNormal" + UUID.randomUUID().toString(); + StateMachine stateMachine = buildStateMachine(uniqueId); Context context = new Context(); States target = stateMachine.fireEvent(States.STATE1, Events.EVENT1, context); @@ -182,7 +189,8 @@ private StateMachine 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(() -> { @@ -227,7 +235,8 @@ public void testParallel(){ .on(StateMachineTest.Events.EVENT2) .when(checkCondition()) .perform(doAction()); - StateMachine stateMachine = builder.build("ParallelMachine"); + String uniqueId = "ParallelMachine" + UUID.randomUUID().toString(); + StateMachine stateMachine = builder.build(uniqueId); System.out.println(stateMachine.generatePlantUML()); List states = stateMachine.fireParallelEvent(StateMachineTest.States.STATE1, StateMachineTest.Events.EVENT1, new Context()); for (StateMachineTest.States state : states) { diff --git a/cola-components/cola-component-statemachine/src/test/java/com/alibaba/cola/test/StateMachineUnNormalTest.java b/cola-components/cola-component-statemachine/src/test/java/com/alibaba/cola/test/StateMachineUnNormalTest.java index 9046d1e3c..0b07549f8 100644 --- a/cola-components/cola-component-statemachine/src/test/java/com/alibaba/cola/test/StateMachineUnNormalTest.java +++ b/cola-components/cola-component-statemachine/src/test/java/com/alibaba/cola/test/StateMachineUnNormalTest.java @@ -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.Assert; import org.junit.Test; @@ -29,7 +30,8 @@ public void testConditionNotMeet(){ .when(checkConditionFalse()) .perform(doAction()); - StateMachine stateMachine = builder.build("NotMeetConditionMachine"); + String uniqueId = "NotMeetConditionMachine" + UUID.randomUUID().toString(); + StateMachine stateMachine = builder.build(uniqueId); StateMachineTest.States target = stateMachine.fireEvent(StateMachineTest.States.STATE1, StateMachineTest.Events.EVENT1, new StateMachineTest.Context()); Assert.assertEquals(StateMachineTest.States.STATE1,target); }