rpuch commented on code in PR #4713: URL: https://github.com/apache/ignite-3/pull/4713#discussion_r1842047768
########## modules/core/src/testFixtures/java/org/apache/ignite/internal/testframework/ExecutorServiceExtension.java: ########## @@ -0,0 +1,245 @@ +/* + * 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.ignite.internal.testframework; + +import static java.lang.reflect.Modifier.isStatic; +import static java.util.concurrent.Executors.newFixedThreadPool; +import static java.util.concurrent.Executors.newScheduledThreadPool; + +import java.lang.reflect.Field; +import java.util.List; +import java.util.Set; +import java.util.concurrent.CopyOnWriteArrayList; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.ThreadFactory; +import java.util.concurrent.TimeUnit; +import java.util.stream.Stream; +import org.apache.ignite.internal.logger.Loggers; +import org.apache.ignite.internal.thread.IgniteThreadFactory; +import org.apache.ignite.internal.thread.ThreadOperation; +import org.apache.ignite.internal.util.IgniteUtils; +import org.junit.jupiter.api.extension.AfterAllCallback; +import org.junit.jupiter.api.extension.AfterEachCallback; +import org.junit.jupiter.api.extension.BeforeAllCallback; +import org.junit.jupiter.api.extension.BeforeEachCallback; +import org.junit.jupiter.api.extension.ExtensionContext; +import org.junit.jupiter.api.extension.ExtensionContext.Namespace; +import org.junit.jupiter.api.extension.ParameterContext; +import org.junit.jupiter.api.extension.ParameterResolutionException; +import org.junit.jupiter.api.extension.ParameterResolver; +import org.junit.platform.commons.support.AnnotationSupport; +import org.junit.platform.commons.support.HierarchyTraversalMode; + +/** + * JUnit extension for injecting temporary {@link ExecutorService}'s into test classes. + * + * @see InjectExecutorService + */ +public class ExecutorServiceExtension implements BeforeAllCallback, AfterAllCallback, BeforeEachCallback, AfterEachCallback, + ParameterResolver { + private static final int CPUS = Runtime.getRuntime().availableProcessors(); + + private static final Namespace NAMESPACE = Namespace.create(ExecutorServiceExtension.class); + + private static final Set<Class<?>> SUPPORTED_FIELD_TYPES = Set.of(ScheduledExecutorService.class, ExecutorService.class); + + private static final Object STATIC_EXECUTORS_KEY = new Object(); + + private static final Object INSTANCE_EXECUTORS_KEY = new Object(); + + @Override + public void beforeAll(ExtensionContext context) throws Exception { + injectFields(context, true); + } + + @Override + public void afterAll(ExtensionContext context) throws Exception { + shutdownExecutors(context, true); + } + + @Override + public void beforeEach(ExtensionContext context) throws Exception { + injectFields(context, false); + } + + @Override + public void afterEach(ExtensionContext context) throws Exception { + shutdownExecutors(context, false); + } + + @Override + public boolean supportsParameter( + ParameterContext parameterContext, + ExtensionContext extensionContext + ) throws ParameterResolutionException { + return parameterContext.isAnnotated(InjectExecutorService.class) + && isFieldTypeIsSupported(parameterContext.getParameter().getType()); + } + + @Override + public Object resolveParameter( + ParameterContext parameterContext, + ExtensionContext extensionContext + ) throws ParameterResolutionException { + if (!supportsParameter(parameterContext, extensionContext)) { + throw new ParameterResolutionException("Unknown parameter:" + parameterContext.getParameter()); + } + + List<ExecutorService> executorServices = getOrCreateExecutorServiceListInStore(extensionContext, false); + + InjectExecutorService injectExecutorService = parameterContext.findAnnotation(InjectExecutorService.class).orElse(null); + + assert injectExecutorService != null : parameterContext.getParameter(); + + ExecutorService executorService = createExecutorService( + injectExecutorService, + parameterContext.getParameter().getName(), + parameterContext.getParameter().getType(), + extensionContext.getRequiredTestClass() + ); + + executorServices.add(executorService); + + return executorService; + } + + private static void injectFields(ExtensionContext context, boolean forStatic) throws Exception { + Class<?> testClass = context.getRequiredTestClass(); + Object testInstance = context.getTestInstance().orElse(null); + + List<Field> fields = collectFields(testClass, forStatic); + + if (fields.isEmpty()) { + return; + } + + List<ExecutorService> executorServices = getOrCreateExecutorServiceListInStore(context, forStatic); + + for (Field field : fields) { + checkFieldTypeIsSupported(field); + + ExecutorService executorService = createExecutorService(field); + + executorServices.add(executorService); + + field.setAccessible(true); + + field.set(forStatic ? null : testInstance, executorService); + } + + context.getStore(NAMESPACE).put(storeKey(forStatic), executorServices); + } + + private static void shutdownExecutors(ExtensionContext context, boolean forStatic) throws Exception { + List<ExecutorService> removed = (List<ExecutorService>) context.getStore(NAMESPACE).remove(storeKey(forStatic)); + + if (removed == null || removed.isEmpty()) { + return; + } + + Stream<AutoCloseable> autoCloseableStream = removed.stream() + .map(executorService -> () -> IgniteUtils.shutdownAndAwaitTermination(executorService, 10, TimeUnit.SECONDS)); + + IgniteUtils.closeAll(autoCloseableStream); + } + + private static List<Field> collectFields(Class<?> testClass, boolean forStatic) { + return AnnotationSupport.findAnnotatedFields( + testClass, + InjectExecutorService.class, + field -> isStatic(field.getModifiers()) == forStatic, + HierarchyTraversalMode.TOP_DOWN + ); + } + + private static void checkFieldTypeIsSupported(Field field) { + if (!isFieldTypeIsSupported(field.getType())) { + throw new IllegalStateException( + String.format("Unsupported field type: [field=%s, supportedFieldTypes=%s]", field, SUPPORTED_FIELD_TYPES) + ); + } + } + + private static boolean isFieldTypeIsSupported(Class<?> fieldType) { + for (Class<?> supportedFieldType : SUPPORTED_FIELD_TYPES) { + if (fieldType.equals(supportedFieldType)) { + return true; + } + } + + return false; + } + + private static ExecutorService createExecutorService(Field field) { + InjectExecutorService injectExecutorService = field.getAnnotation(InjectExecutorService.class); + + assert injectExecutorService != null : field; + + return createExecutorService(injectExecutorService, field.getName(), field.getType(), field.getDeclaringClass()); Review Comment: Well, I asked a question :) What is already in code makes some sense already. Or we could use the real test class to obtain a logger. Up to you to decide. -- 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: notifications-unsubscr...@ignite.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org