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

Fixed non-idempotent unit tests #489

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

kaiyaok2
Copy link

@kaiyaok2 kaiyaok2 commented Apr 25, 2024

Motivation:

The following tests are not idempotent and fail in repeated runs in the same environment, because it pollutes a state reused by itself:

  • StateMachineUnNormalTest#testConditionNotMeet
  • StateMachinePlantUMLTest#testPlantUML
  • StateMachineChoiceTest#testChoice
  • StateMachineTest#testMultiThread
  • StateMachineTest#testParallel
  • StateMachineTest#testExternalTransitionsNormal
  • StateMachineTest#testExternalInternalNormal
  • StateMachineTest#testInternalNormal
  • StateMachineTest#testVerify
  • StateMachineTest#testExternalNormal
  • StateMachineTest#testFail

Take StateMachineUnNormalTest#testConditionNotMeet as an example: it constructs a state machine with a fixed ID "NotMeetConditionMachine". Then, in the second run, an error will be thrown since the state machine with ID "NotMeetConditionMachine" already exists. A fix is recommended since unit tests should be idempotent (deterministically passing in repeated runs).

Stacktrace of failure in the second run:

com.alibaba.cola.statemachine.impl.StateMachineException: The state machine with id [NotMeetConditionMachine] is already built, no need to build again
	at com.alibaba.cola.statemachine.StateMachineFactory.register(StateMachineFactory.java:20)
	at com.alibaba.cola.statemachine.builder.StateMachineBuilderImpl.build(StateMachineBuilderImpl.java:57)
	at com.alibaba.cola.test.StateMachineUnNormalTest.testConditionNotMeet(StateMachineUnNormalTest.java:32)
	at java.base/java.lang.reflect.Method.invoke(Method.java:568)

Reproduce:

Using the NIOInspector plugin that supports rerunning JUnit tests in the same environment. Use StateMachineTest#testMultiThread as an example:

cd cola-component-statemachine
mvn edu.illinois:NIOInspector:rerun -Dtest=com.alibaba.cola.test.StateMachineTest#testMultiThread

Proposed Fix

Assign a unique ID to each state machine used for testing.

@kaiyaok2 kaiyaok2 changed the title Fixed non-idempotent test StateMachineUnNormalTest.testConditionNotMeet Fixed non-idempotent unit tests May 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant