SaaiVenkat opened a new issue, #13359: URL: https://github.com/apache/dubbo/issues/13359
<!-- If you need to report a security issue please visit https://github.com/apache/dubbo/security/policy --> - [X] I have searched the [issues](https://github.com/apache/dubbo/issues) of this repository and believe that this is not a duplicate. ### Issue - In `dubbo-common`, the unit test `org.apache.dubbo.common.threadlocal.InternalThreadLocalTest.testConstructionWithIndex()` can trigger `java.lang.ClassCastException` when being run before any of the tests in the same class. - Since Junit 5 doesn't guarantee the [order of test executions](https://www.baeldung.com/junit-5-test-order), this could trigger the `internalThreadLocal()` before other tests, thereby resulting in test failure. ### Reason - The reason for this dependency is that the test `InternalThreadLocalTest.testConstructionWithIndex()` pollutes the state `InternalThreadLocal.index` at https://github.com/apache/dubbo/blob/7c3e592ebb01be108e4794bf2e8194ecafe0bced/dubbo-common/src/test/java/org/apache/dubbo/common/threadlocal/InternalThreadLocalTest.java#L260 - When the first `new InternalThreadLocal<>()` is called at https://github.com/apache/dubbo/blob/7c3e592ebb01be108e4794bf2e8194ecafe0bced/dubbo-common/src/test/java/org/apache/dubbo/common/threadlocal/InternalThreadLocalTest.java#L246, the value of `InternalThreadLocalMap.NEXT_INDEX` value is incremented at https://github.com/apache/dubbo/blob/7c3e592ebb01be108e4794bf2e8194ecafe0bced/dubbo-common/src/main/java/org/apache/dubbo/common/threadlocal/InternalThreadLocalMap.java#L95-L101 - However, when restoring `InternalThreadLocalMap.NEXT_INDEX` to its initial value at https://github.com/apache/dubbo/blob/7c3e592ebb01be108e4794bf2e8194ecafe0bced/dubbo-common/src/test/java/org/apache/dubbo/common/threadlocal/InternalThreadLocalTest.java#L259-L260, it's restored to `0` instead of the incremented value `1` - When other test instantiate `new InternalThreadLocal<>()`, the value of `index` is fetched from this polluted state at https://github.com/apache/dubbo/blob/7c3e592ebb01be108e4794bf2e8194ecafe0bced/dubbo-common/src/main/java/org/apache/dubbo/common/threadlocal/InternalThreadLocal.java#L41 - Accessing the `InternalThreadLocal` with this polluted `index` will result in the above failure. Exact stack trace for one of the failures is given in the last section. ### Environment * Dubbo version: `3.2.8` * Operating System version: `Mac OS X 14.0 aarch64` * Java version: `1.8.0_381` ### Steps to reproduce this issue - Annotate the test class `org.apache.dubbo.common.threadlocal.InternalThreadLocalTest` with `@org.junit.jupiter.api.TestMethodOrder(org.junit.jupiter.api.MethodOrderer.OrderAnnotation.class)` - Annotate the polluter unit test `testConstructionWithIndex()` with `org.junit.jupiter.api.Order(1)` - Annotate any of the following tests in the same class with `org.junit.jupiter.api.Order(2)` - `org.apache.dubbo.common.threadlocal.InternalThreadLocalTest.testRemoveAll()` - `org.apache.dubbo.common.threadlocal.InternalThreadLocalTest.testSize()` - `org.apache.dubbo.common.threadlocal.InternalThreadLocalTest.testSetAndGet()` - `org.apache.dubbo.common.threadlocal.InternalThreadLocalTest.testRemove()` - `org.apache.dubbo.common.threadlocal.InternalThreadLocalTest.testOnRemove()` - Annotating any of the other tests that's not mentioned above with `org.junit.jupiter.api.Order(2)` will also result in throwing `java.lang.ClassCastException` without failing. However, the remaining tests will fail to start. ### Expected Behavior <!-- What do you expect from the above steps?--> - Executing the tests in any order should not fail and should not throw any unexpected Exceptions. ### Actual Behavior <!-- What actually happens? --> - Running the tests in with `testConstructionWithIndex()` first and then running any of the above mentioned tests results in the failure of the second test. - Also, the second tests throw `java.lang.ClassCastException` ### Stack Trace - The stack trace element `#3` - `at org.apache.dubbo.common.threadlocal.InternalThreadLocalTest.testRemove(InternalThreadLocalTest.java:113)` will change based on the second test. ``` java.lang.ClassCastException: class java.lang.Integer cannot be cast to class java.util.Set (java.lang.Integer and java.util.Set are in module java.base of loader 'bootstrap') at org.apache.dubbo.common.threadlocal.InternalThreadLocal.addToVariablesToRemove(InternalThreadLocal.java:95) at org.apache.dubbo.common.threadlocal.InternalThreadLocal.set(InternalThreadLocal.java:162) at org.apache.dubbo.common.threadlocal.InternalThreadLocalTest.testRemove(InternalThreadLocalTest.java:113) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.base/java.lang.reflect.Method.invoke(Method.java:568) at org.junit.platform.commons.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:727) at org.junit.jupiter.engine.execution.MethodInvocation.proceed(MethodInvocation.java:60) ``` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
