ruanhang1993 commented on a change in pull request #17556: URL: https://github.com/apache/flink/pull/17556#discussion_r747252860
########## File path: flink-test-utils-parent/flink-test-utils-junit/src/main/java/org/apache/flink/testutils/junit/SharedObjectsExtension.java ########## @@ -0,0 +1,178 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.flink.testutils.junit; + +import org.junit.jupiter.api.extension.AfterEachCallback; +import org.junit.jupiter.api.extension.BeforeEachCallback; +import org.junit.jupiter.api.extension.ExtensionContext; + +import java.util.Map; +import java.util.Objects; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.atomic.AtomicInteger; + +/** + * This rule allows objects to be used both in the main test case as well as in UDFs by using + * serializable {@link SharedReference}s. Usage: + * + * <pre><code> + * {@literal @RegisterExtension} Review comment: I think the test owner have to get some knowledge about Junit5 and be careful, there is not some information for us to find incorrect usages like this. The misuse will not make tests fail. I write a test about it. I misuse the `AllCallbackWrapper` in a non-static field, and the `EachCallbackWrapper` in a static field. ```java public class Test { @RegisterExtension AllCallbackWrapper allCallbackWrapper = new AllCallbackWrapper(new TestExtension("all")); @RegisterExtension static EachCallbackWrapper eachCallbackWrapper = new EachCallbackWrapper(new TestExtension("each")); @BeforeAll public static void ba() { System.out.println("[method] before all"); } @BeforeEach public void be() { System.out.println("[method] before each"); } @AfterAll public static void aa() { System.out.println("[method] after all"); } @AfterEach public void ae() { System.out.println("[method] after each"); } @Test public void test() { System.out.println("[self] code"); } static class TestExtension implements CustomExtension{ private String id; public TestExtension(String id) { this.id = id; } public void before(ExtensionContext context) throws Exception { System.out.println("[do before]:" + id); } public void after(ExtensionContext context) throws Exception { System.out.println("[do after]:" + id); } } } ``` The result is as following. ``` [method] before all [do before]:each [method] before each [self] code [method] after each [do after]:each [method] after all ``` This misuse will not make tests fail. The misuse for `AllCallbackWrapper` in a non-static field causes the `beforeAll/afterAll` in `AllCallbackWrapper` not execute. But static fields are not limited. The result is the same as [the junit5 user guide about this usage](https://junit.org/junit5/docs/current/user-guide/#extensions-registration-programmatic-static-fields). So we should change `@Rule` to a non-static field and `@ClassRule` to a static field to make sure everything go well. -- 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]
