tkalkirill commented on code in PR #4713: URL: https://github.com/apache/ignite-3/pull/4713#discussion_r1841729673
########## 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); Review Comment: Yeah, maybe I can fix it. -- 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