Skip to content

Fixed non-idempotent unit tests#489

Open
kaiyaok2 wants to merge 2 commits intoalibaba:masterfrom
kaiyaok2:fix_NIO_tests
Open

Fixed non-idempotent unit tests#489
kaiyaok2 wants to merge 2 commits intoalibaba:masterfrom
kaiyaok2:fix_NIO_tests

Conversation

@kaiyaok2
Copy link
Copy Markdown

@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.

1 participant