alibaba / Sentinel

A powerful flow control component enabling reliability, resilience and monitoring for microservices. (面向云原生微服务的高可用流控防护组件)
https://sentinelguard.io/
Apache License 2.0
22.46k stars 8.04k forks source link

Fix test failure under JDK 17 | 修复 JDK 17 下测试框架及用例报错的问题 #3015

Open sczyh30 opened 1 year ago

sczyh30 commented 1 year ago

Issue Description

Type: bug report

Describe what happened (or what feature you want)

The test cases will fail under JDK 17, due to JDK-compatibility of some test frameworks (e.g. PowerMock).

JDK 17 下测试用例会失败,部分原因是我们测试框架不支持 JDK 17 导致的(可能是版本太低,或确实不支持)。欢迎社区修复解决。

Some error messages:

com.alibaba.csp.sentinel.slots.statistic.base.LeapArrayTest

java.lang.ExceptionInInitializerError
    at jdk.internal.reflect.GeneratedSerializationConstructorAccessor4.newInstance(Unknown Source)
    at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:499)
    at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:480)
    at org.objenesis.instantiator.sun.SunReflectionFactoryInstantiator.newInstance(SunReflectionFactoryInstantiator.java:48)
    at org.objenesis.ObjenesisBase.newInstance(ObjenesisBase.java:73)
    at org.mockito.internal.creation.instance.ObjenesisInstantiator.newInstance(ObjenesisInstantiator.java:19)
    at org.mockito.internal.creation.bytebuddy.SubclassByteBuddyMockMaker.createMock(SubclassByteBuddyMockMaker.java:47)
    at org.mockito.internal.creation.bytebuddy.ByteBuddyMockMaker.createMock(ByteBuddyMockMaker.java:25)
    at org.powermock.api.mockito.mockmaker.PowerMockMaker.createMock(PowerMockMaker.java:41)
    at org.mockito.internal.util.MockUtil.createMock(MockUtil.java:35)
    at org.mockito.internal.MockitoCore.mock(MockitoCore.java:62)
    at org.mockito.Mockito.mock(Mockito.java:1908)
    at org.powermock.api.mockito.internal.mockcreation.DefaultMockCreator.createMethodInvocationControl(DefaultMockCreator.java:108)
    at org.powermock.api.mockito.internal.mockcreation.DefaultMockCreator.doCreateMock(DefaultMockCreator.java:61)
    at org.powermock.api.mockito.internal.mockcreation.DefaultMockCreator.createMock(DefaultMockCreator.java:53)
    at org.powermock.api.mockito.internal.mockcreation.DefaultMockCreator.mock(DefaultMockCreator.java:40)
    at org.powermock.api.mockito.PowerMockito.mockStatic(PowerMockito.java:62)
    at com.alibaba.csp.sentinel.test.AbstractTimeBasedTest.<init>(AbstractTimeBasedTest.java:43)
    at com.alibaba.csp.sentinel.slots.statistic.base.LeapArrayTest.<init>(LeapArrayTest.java:29)
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:77)
    at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:499)
    at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:480)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.createTestInstance(PowerMockJUnit44RunnerDelegateImpl.java:197)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.createTest(PowerMockJUnit44RunnerDelegateImpl.java:182)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.invokeTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:204)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.runMethods(PowerMockJUnit44RunnerDelegateImpl.java:160)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$1.run(PowerMockJUnit44RunnerDelegateImpl.java:134)
    at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:34)
    at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:44)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.run(PowerMockJUnit44RunnerDelegateImpl.java:136)
    at org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.run(JUnit4TestSuiteChunkerImpl.java:117)
    at org.powermock.modules.junit4.common.internal.impl.AbstractCommonPowerMockRunner.run(AbstractCommonPowerMockRunner.java:57)
    at org.powermock.modules.junit4.PowerMockRunner.run(PowerMockRunner.java:59)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69)
    at com.intellij.rt.junit.IdeaTestRunner$Repeater$1.execute(IdeaTestRunner.java:38)
    at com.intellij.rt.execution.junit.TestsRepeater.repeat(TestsRepeater.java:11)
    at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:35)
    at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:235)
    at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:54)
Caused by: java.lang.reflect.InaccessibleObjectException: Unable to make private static void java.lang.System.initPhase1() accessible: module java.base does not "opens java.lang" to unnamed module @2e5d6d97
    at java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:354)
    at java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:297)
    at java.base/java.lang.reflect.Method.checkCanSetAccessible(Method.java:199)
    at java.base/java.lang.reflect.Method.setAccessible(Method.java:193)
    at org.powermock.reflect.internal.WhiteboxImpl.doGetAllMethods(WhiteboxImpl.java:1499)
    at org.powermock.reflect.internal.WhiteboxImpl.getAllMethods(WhiteboxImpl.java:1473)
    at org.powermock.reflect.internal.WhiteboxImpl.getMethods(WhiteboxImpl.java:1741)
    at org.powermock.reflect.internal.WhiteboxImpl.getMethods(WhiteboxImpl.java:1780)
    at org.powermock.reflect.internal.WhiteboxImpl.getBestMethodCandidate(WhiteboxImpl.java:999)
    at org.powermock.core.MockInvocation.findMethodToInvoke(MockInvocation.java:58)
    at org.powermock.core.MockInvocation.init(MockInvocation.java:35)
    at org.powermock.core.MockInvocation.<init>(MockInvocation.java:22)
    at org.powermock.core.MockGateway.doMethodCall(MockGateway.java:155)
    at org.powermock.core.MockGateway.methodCall(MockGateway.java:138)
    at com.alibaba.csp.sentinel.util.TimeUtil.<init>(TimeUtil.java:99)
    at com.alibaba.csp.sentinel.util.TimeUtil.<clinit>(TimeUtil.java:79)
    ... 42 more

Tell us your environment

JDK 17, Sentinel 2.0.0-SNAPSHOT

sczyh30 commented 1 year ago

NOTE: 我们的 JDK 11/17 的用例需要在不加 --add-opens 参数的情况下跑通。

Our test cases should pass without --add-opens args under JDK 11/17.

xiangtianyu commented 1 year ago

NOTE: 我们的 JDK 11/17 的用例需要在不加 --add-opens 参数的情况下跑通。

Our test cases should pass without --add-opens args under JDK 11/17.

actually, the latest version of powermock is 2.0.9 in 2020, so if we wank to support jdk 17 without using --add-opens args, we may rewrite all cases used powermock

sczyh30 commented 1 year ago

NOTE: 我们的 JDK 11/17 的用例需要在不加 --add-opens 参数的情况下跑通。 Our test cases should pass without --add-opens args under JDK 11/17.

actually, the latest version of powermock is 2.0.9 in 2020, so if we wank to support jdk 17 without using --add-opens args, we may rewrite all cases used powermock

Is it possible if we replace powermock with other available mock libs? We mainly used it for mock static methods (e.g. methods in System).

xiangtianyu commented 1 year ago

NOTE: 我们的 JDK 11/17 的用例需要在不加 --add-opens 参数的情况下跑通。 Our test cases should pass without --add-opens args under JDK 11/17.

actually, the latest version of powermock is 2.0.9 in 2020, so if we wank to support jdk 17 without using --add-opens args, we may rewrite all cases used powermock

Is it possible if we replace powermock with other available mock libs? We mainly used it for mock static methods (e.g. methods in System).

not only the powermock, there may have some other sdk like this. It's seems a bit tough.