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

Fix non-idempotent unit test ClassGeneratorTest.testMain #14135

Merged
merged 1 commit into from May 8, 2024

Conversation

kaiyaok2
Copy link
Contributor

Fixes #14134.

The Problem

The ClassGeneratorTest.testMain test is non-idempotent and fails upon repeated execution within the same JVM instance due to self-induced state pollution. Specifically, the test employs the ClassGenerator to configure a custom class with a predetermined name (Bean.class.getName() + "$Builder"). Subsequently, the test invokes ClassGenerator.toClass() to instantiate the desired Class<?> object. However, within ClassGenerator.toClass(), the invoked method javassist.ClassPool.makeClass() is called to create the class. Given that Javassist freezes a class upon its initial loading (ensuring classes cannot be altered or removed at runtime), repeated invocations of testMain() fail as the class name remains unchanged, leading to class generation errors.

Sample Failure Message (in the 2nd run of the test):

java.lang.RuntimeException: org.apache.dubbo.common.bytecode.Bean$Builder: frozen class (cannot edit)
	at javassist.ClassPool.checkNotFrozen(ClassPool.java:595)
	at javassist.ClassPool.makeClass(ClassPool.java:837)
	at javassist.ClassPool.makeClass(ClassPool.java:814)
	at org.apache.dubbo.common.bytecode.ClassGenerator.toClass(ClassGenerator.java:318)
	at org.apache.dubbo.common.bytecode.ClassGenerator.toClass(ClassGenerator.java:302)
	at org.apache.dubbo.common.bytecode.ClassGeneratorTest.testMain(ClassGeneratorTest.java:197)
	at java.base/java.lang.reflect.Method.invoke(Method.java:568)
	at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)
	at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)

What is the purpose of the change

Fixing this issue is recommended since unit tests should be idempotent (deterministically passing in repeated runs).

Brief changelog

Assign a unique ID to each class to be generated in the same classloader.

Verifying this change

After the patch, running the test multiple times within the same JVM will not lead to failures.

Additional Notes

There are some other non-idempotent tests detected in the test suite. I can open a new PR to address the others if you find this fix reasonable.

Checklist

  • Make sure there is a GitHub_issue field for the change (usually before you start working on it). Trivial changes like typos do not require a GitHub issue. Your pull request should address just this issue, without pulling in other changes - one PR resolves one issue.
  • Each commit in the pull request should have a meaningful subject line and body.
  • Write a pull request description that is detailed enough to understand what the pull request does, how, and why.
  • GitHub Actions works fine on your own branch.

Copy link

sonarcloud bot commented Apr 26, 2024

Quality Gate Passed Quality Gate passed

Issues
0 New issues
0 Accepted issues

Measures
0 Security Hotspots
No data about Coverage
0.0% Duplication on New Code

See analysis details on SonarCloud

@kaiyaok2
Copy link
Contributor Author

@chickenlj @EarthChen @AlbumenJ Please also let me know if I shall include fixes to other non-idempotent tests (all relatively simple, including resetting some shared states)

@AlbumenJ AlbumenJ merged commit 8419324 into apache:3.2 May 8, 2024
19 checks passed
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.

[Bug] Non-idempotent unit test ClassGeneratorTest.testMain
2 participants