rpuch commented on code in PR #4713: URL: https://github.com/apache/ignite-3/pull/4713#discussion_r1840354589
########## modules/core/src/test/java/org/apache/ignite/internal/testframework/ExecutorServiceExtensionTest.java: ########## @@ -0,0 +1,207 @@ +/* + * 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.util.concurrent.CompletableFuture.runAsync; +import static org.apache.ignite.internal.testframework.JunitExtensionTestUtils.assertExecutesSuccessfully; +import static org.apache.ignite.internal.testframework.JunitExtensionTestUtils.assertExecutesWithFailure; +import static org.apache.ignite.internal.testframework.matchers.CompletableFutureMatcher.willCompleteSuccessfully; +import static org.apache.ignite.internal.thread.ThreadOperation.PROCESS_RAFT_REQ; +import static org.apache.ignite.internal.thread.ThreadOperation.STORAGE_READ; +import static org.apache.ignite.internal.thread.ThreadOperation.STORAGE_WRITE; +import static org.apache.ignite.internal.thread.ThreadOperation.TX_STATE_STORAGE_ACCESS; +import static org.apache.ignite.internal.thread.ThreadOperation.WAIT; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.containsInAnyOrder; +import static org.hamcrest.Matchers.containsString; +import static org.hamcrest.Matchers.instanceOf; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.fail; +import static org.junit.platform.testkit.engine.TestExecutionResultConditions.instanceOf; +import static org.junit.platform.testkit.engine.TestExecutionResultConditions.message; + +import java.util.concurrent.ExecutorService; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.ScheduledThreadPoolExecutor; +import java.util.concurrent.ThreadPoolExecutor; +import org.apache.ignite.internal.thread.IgniteThread; +import org.apache.ignite.internal.thread.ThreadOperation; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; + +/** For {@link ExecutorServiceExtension} testing. */ +public class ExecutorServiceExtensionTest { + private static final int CPU = Runtime.getRuntime().availableProcessors(); Review Comment: ```suggestion private static final int CPUS = Runtime.getRuntime().availableProcessors(); ``` ########## modules/core/src/test/java/org/apache/ignite/internal/testframework/ExecutorServiceExtensionTest.java: ########## @@ -0,0 +1,207 @@ +/* + * 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.util.concurrent.CompletableFuture.runAsync; +import static org.apache.ignite.internal.testframework.JunitExtensionTestUtils.assertExecutesSuccessfully; +import static org.apache.ignite.internal.testframework.JunitExtensionTestUtils.assertExecutesWithFailure; +import static org.apache.ignite.internal.testframework.matchers.CompletableFutureMatcher.willCompleteSuccessfully; +import static org.apache.ignite.internal.thread.ThreadOperation.PROCESS_RAFT_REQ; +import static org.apache.ignite.internal.thread.ThreadOperation.STORAGE_READ; +import static org.apache.ignite.internal.thread.ThreadOperation.STORAGE_WRITE; +import static org.apache.ignite.internal.thread.ThreadOperation.TX_STATE_STORAGE_ACCESS; +import static org.apache.ignite.internal.thread.ThreadOperation.WAIT; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.containsInAnyOrder; +import static org.hamcrest.Matchers.containsString; +import static org.hamcrest.Matchers.instanceOf; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.fail; +import static org.junit.platform.testkit.engine.TestExecutionResultConditions.instanceOf; +import static org.junit.platform.testkit.engine.TestExecutionResultConditions.message; + +import java.util.concurrent.ExecutorService; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.ScheduledThreadPoolExecutor; +import java.util.concurrent.ThreadPoolExecutor; +import org.apache.ignite.internal.thread.IgniteThread; +import org.apache.ignite.internal.thread.ThreadOperation; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; + +/** For {@link ExecutorServiceExtension} testing. */ +public class ExecutorServiceExtensionTest { + private static final int CPU = Runtime.getRuntime().availableProcessors(); + + @ExtendWith(ExecutorServiceExtension.class) + static class NormalFieldInjectionTest { + @InjectExecutorService + private static ExecutorService staticExecutorServiceWithDefaults; + + @InjectExecutorService(threadCount = 2, threadPrefix = "test-foo-static-executor", allowedOperations = TX_STATE_STORAGE_ACCESS) + private static ExecutorService staticExecutorService; + + @InjectExecutorService + private static ScheduledExecutorService staticScheduledExecutorServiceWithDefaults; + + @InjectExecutorService( + threadCount = 3, + threadPrefix = "test-bar-static-executor", + allowedOperations = {STORAGE_READ, STORAGE_WRITE} + ) + private static ScheduledExecutorService staticScheduledExecutorService; + + @InjectExecutorService + private ExecutorService fieldExecutorServiceWithDefaults; + + @InjectExecutorService(threadCount = 4, threadPrefix = "test-foo-field-executor", allowedOperations = STORAGE_WRITE) + private ExecutorService fieldExecutorService; Review Comment: ```suggestion private ExecutorService instanceFieldExecutorService; ``` ########## modules/core/src/test/java/org/apache/ignite/internal/testframework/ExecutorServiceExtensionTest.java: ########## @@ -0,0 +1,207 @@ +/* + * 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.util.concurrent.CompletableFuture.runAsync; +import static org.apache.ignite.internal.testframework.JunitExtensionTestUtils.assertExecutesSuccessfully; +import static org.apache.ignite.internal.testframework.JunitExtensionTestUtils.assertExecutesWithFailure; +import static org.apache.ignite.internal.testframework.matchers.CompletableFutureMatcher.willCompleteSuccessfully; +import static org.apache.ignite.internal.thread.ThreadOperation.PROCESS_RAFT_REQ; +import static org.apache.ignite.internal.thread.ThreadOperation.STORAGE_READ; +import static org.apache.ignite.internal.thread.ThreadOperation.STORAGE_WRITE; +import static org.apache.ignite.internal.thread.ThreadOperation.TX_STATE_STORAGE_ACCESS; +import static org.apache.ignite.internal.thread.ThreadOperation.WAIT; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.containsInAnyOrder; +import static org.hamcrest.Matchers.containsString; +import static org.hamcrest.Matchers.instanceOf; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.fail; +import static org.junit.platform.testkit.engine.TestExecutionResultConditions.instanceOf; +import static org.junit.platform.testkit.engine.TestExecutionResultConditions.message; + +import java.util.concurrent.ExecutorService; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.ScheduledThreadPoolExecutor; +import java.util.concurrent.ThreadPoolExecutor; +import org.apache.ignite.internal.thread.IgniteThread; +import org.apache.ignite.internal.thread.ThreadOperation; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; + +/** For {@link ExecutorServiceExtension} testing. */ +public class ExecutorServiceExtensionTest { + private static final int CPU = Runtime.getRuntime().availableProcessors(); + + @ExtendWith(ExecutorServiceExtension.class) + static class NormalFieldInjectionTest { + @InjectExecutorService + private static ExecutorService staticExecutorServiceWithDefaults; + + @InjectExecutorService(threadCount = 2, threadPrefix = "test-foo-static-executor", allowedOperations = TX_STATE_STORAGE_ACCESS) + private static ExecutorService staticExecutorService; + + @InjectExecutorService + private static ScheduledExecutorService staticScheduledExecutorServiceWithDefaults; + + @InjectExecutorService( + threadCount = 3, + threadPrefix = "test-bar-static-executor", + allowedOperations = {STORAGE_READ, STORAGE_WRITE} + ) + private static ScheduledExecutorService staticScheduledExecutorService; + + @InjectExecutorService + private ExecutorService fieldExecutorServiceWithDefaults; + + @InjectExecutorService(threadCount = 4, threadPrefix = "test-foo-field-executor", allowedOperations = STORAGE_WRITE) + private ExecutorService fieldExecutorService; + + @InjectExecutorService + private ScheduledExecutorService fieldScheduledExecutorServiceWithDefaults; Review Comment: ```suggestion private ScheduledExecutorService instanceFieldScheduledExecutorServiceWithDefaults; ``` ########## 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: If a field is declared in a superclass, then `field.getDeclaringClass()` will not be the actual test class. It will then be used to get logger, for example. Is this intended? ########## 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) { Review Comment: ```suggestion private static boolean isFieldTypeSupported(Class<?> fieldType) { ``` ########## modules/core/src/test/java/org/apache/ignite/internal/testframework/ExecutorServiceExtensionTest.java: ########## @@ -0,0 +1,207 @@ +/* + * 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.util.concurrent.CompletableFuture.runAsync; +import static org.apache.ignite.internal.testframework.JunitExtensionTestUtils.assertExecutesSuccessfully; +import static org.apache.ignite.internal.testframework.JunitExtensionTestUtils.assertExecutesWithFailure; +import static org.apache.ignite.internal.testframework.matchers.CompletableFutureMatcher.willCompleteSuccessfully; +import static org.apache.ignite.internal.thread.ThreadOperation.PROCESS_RAFT_REQ; +import static org.apache.ignite.internal.thread.ThreadOperation.STORAGE_READ; +import static org.apache.ignite.internal.thread.ThreadOperation.STORAGE_WRITE; +import static org.apache.ignite.internal.thread.ThreadOperation.TX_STATE_STORAGE_ACCESS; +import static org.apache.ignite.internal.thread.ThreadOperation.WAIT; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.containsInAnyOrder; +import static org.hamcrest.Matchers.containsString; +import static org.hamcrest.Matchers.instanceOf; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.fail; +import static org.junit.platform.testkit.engine.TestExecutionResultConditions.instanceOf; +import static org.junit.platform.testkit.engine.TestExecutionResultConditions.message; + +import java.util.concurrent.ExecutorService; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.ScheduledThreadPoolExecutor; +import java.util.concurrent.ThreadPoolExecutor; +import org.apache.ignite.internal.thread.IgniteThread; +import org.apache.ignite.internal.thread.ThreadOperation; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; + +/** For {@link ExecutorServiceExtension} testing. */ +public class ExecutorServiceExtensionTest { + private static final int CPU = Runtime.getRuntime().availableProcessors(); + + @ExtendWith(ExecutorServiceExtension.class) + static class NormalFieldInjectionTest { + @InjectExecutorService + private static ExecutorService staticExecutorServiceWithDefaults; + + @InjectExecutorService(threadCount = 2, threadPrefix = "test-foo-static-executor", allowedOperations = TX_STATE_STORAGE_ACCESS) + private static ExecutorService staticExecutorService; + + @InjectExecutorService + private static ScheduledExecutorService staticScheduledExecutorServiceWithDefaults; + + @InjectExecutorService( + threadCount = 3, + threadPrefix = "test-bar-static-executor", + allowedOperations = {STORAGE_READ, STORAGE_WRITE} + ) + private static ScheduledExecutorService staticScheduledExecutorService; + + @InjectExecutorService + private ExecutorService fieldExecutorServiceWithDefaults; + + @InjectExecutorService(threadCount = 4, threadPrefix = "test-foo-field-executor", allowedOperations = STORAGE_WRITE) + private ExecutorService fieldExecutorService; + + @InjectExecutorService + private ScheduledExecutorService fieldScheduledExecutorServiceWithDefaults; + + @InjectExecutorService(threadCount = 5, threadPrefix = "test-bar-field-executor", allowedOperations = PROCESS_RAFT_REQ) + private ScheduledExecutorService fieldScheduledExecutorService; + + @Test + void test( + @InjectExecutorService + ExecutorService parameterExecutorServiceWithDefaults, + @InjectExecutorService(threadCount = 6, threadPrefix = "test-foo-param-executor", allowedOperations = WAIT) + ExecutorService parameterExecutorService, + @InjectExecutorService + ScheduledExecutorService parameterScheduledExecutorServiceWithDefaults, + @InjectExecutorService(threadCount = 7, threadPrefix = "test-bar-param-executor", allowedOperations = WAIT) + ScheduledExecutorService parameterScheduledExecutorService + ) { + checkExecutorService( + staticExecutorServiceWithDefaults, + CPU, + "test-ExecutorService-staticExecutorServiceWithDefaults" + ); + checkScheduledExecutorService( + staticScheduledExecutorServiceWithDefaults, + 1, + "test-ScheduledExecutorService-staticScheduledExecutorServiceWithDefaults" + ); + checkExecutorService( + fieldExecutorServiceWithDefaults, + CPU, + "test-ExecutorService-fieldExecutorServiceWithDefaults" + ); + checkScheduledExecutorService( + fieldScheduledExecutorServiceWithDefaults, + 1, + "test-ScheduledExecutorService-fieldScheduledExecutorServiceWithDefaults" + ); + checkExecutorService( + parameterExecutorServiceWithDefaults, + CPU, + "test-ExecutorService-arg" + ); + checkScheduledExecutorService( + parameterScheduledExecutorServiceWithDefaults, + 1, + "test-ScheduledExecutorService-arg" + ); + + checkExecutorService(staticExecutorService, 2, "test-foo-static-executor", TX_STATE_STORAGE_ACCESS); + checkScheduledExecutorService(staticScheduledExecutorService, 3, "test-bar-static-executor", STORAGE_READ, STORAGE_WRITE); + checkExecutorService(fieldExecutorService, 4, "test-foo-field-executor", STORAGE_WRITE); + checkScheduledExecutorService(fieldScheduledExecutorService, 5, "test-bar-field-executor", PROCESS_RAFT_REQ); + checkExecutorService(parameterExecutorService, 6, "test-foo-param-executor", WAIT); + checkScheduledExecutorService(parameterScheduledExecutorService, 7, "test-bar-param-executor", WAIT); + } + } + + @ExtendWith(ExecutorServiceExtension.class) + static class ErrorFieldInjectionTest { + @InjectExecutorService + private static Integer staticExecutorService; + + @InjectExecutorService + private String fieldExecutorService; + + @Test + public void test( + @InjectExecutorService + Boolean parameterExecutorService + ) { + fail("Should not reach here"); + } + } + + @Test + void testFieldInjection() { + assertExecutesSuccessfully(NormalFieldInjectionTest.class); + } + + @Test + void testErrorStaticFieldInjection() { + assertExecutesWithFailure( + ErrorFieldInjectionTest.class, + instanceOf(IllegalStateException.class), + message(m -> m.contains("Unsupported field type")) + ); + } + + private static void checkExecutorService( + ExecutorService service, + int expCorePoolSize, + String expThreadPrefix, + ThreadOperation... expThreadOperations + ) { + assertThat(service, instanceOf(ThreadPoolExecutor.class)); + + checkThreadPoolExecutor((ThreadPoolExecutor) service, expCorePoolSize, expThreadPrefix, expThreadOperations); + } + + private static void checkScheduledExecutorService( + ScheduledExecutorService service, + int expCorePoolSize, + String expThreadPrefix, + ThreadOperation... expThreadOperations + ) { + assertThat(service, instanceOf(ScheduledThreadPoolExecutor.class)); + + checkThreadPoolExecutor((ScheduledThreadPoolExecutor) service, expCorePoolSize, expThreadPrefix, expThreadOperations); + } + + private static void checkThreadPoolExecutor( + ThreadPoolExecutor executor, + int expCorePoolSize, + String expThreadPrefix, + ThreadOperation... expThreadOperations + ) { + assertEquals(executor.getCorePoolSize(), expCorePoolSize); + + assertThat( + runAsync(() -> { + Thread thread = Thread.currentThread(); + + assertThat(thread, instanceOf(IgniteThread.class)); + + IgniteThread thread1 = (IgniteThread) thread; Review Comment: ```suggestion IgniteThread igniteThread = (IgniteThread) thread; ``` ########## modules/core/src/test/java/org/apache/ignite/internal/testframework/ExecutorServiceExtensionTest.java: ########## @@ -0,0 +1,207 @@ +/* + * 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.util.concurrent.CompletableFuture.runAsync; +import static org.apache.ignite.internal.testframework.JunitExtensionTestUtils.assertExecutesSuccessfully; +import static org.apache.ignite.internal.testframework.JunitExtensionTestUtils.assertExecutesWithFailure; +import static org.apache.ignite.internal.testframework.matchers.CompletableFutureMatcher.willCompleteSuccessfully; +import static org.apache.ignite.internal.thread.ThreadOperation.PROCESS_RAFT_REQ; +import static org.apache.ignite.internal.thread.ThreadOperation.STORAGE_READ; +import static org.apache.ignite.internal.thread.ThreadOperation.STORAGE_WRITE; +import static org.apache.ignite.internal.thread.ThreadOperation.TX_STATE_STORAGE_ACCESS; +import static org.apache.ignite.internal.thread.ThreadOperation.WAIT; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.containsInAnyOrder; +import static org.hamcrest.Matchers.containsString; +import static org.hamcrest.Matchers.instanceOf; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.fail; +import static org.junit.platform.testkit.engine.TestExecutionResultConditions.instanceOf; +import static org.junit.platform.testkit.engine.TestExecutionResultConditions.message; + +import java.util.concurrent.ExecutorService; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.ScheduledThreadPoolExecutor; +import java.util.concurrent.ThreadPoolExecutor; +import org.apache.ignite.internal.thread.IgniteThread; +import org.apache.ignite.internal.thread.ThreadOperation; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; + +/** For {@link ExecutorServiceExtension} testing. */ +public class ExecutorServiceExtensionTest { + private static final int CPU = Runtime.getRuntime().availableProcessors(); + + @ExtendWith(ExecutorServiceExtension.class) + static class NormalFieldInjectionTest { + @InjectExecutorService + private static ExecutorService staticExecutorServiceWithDefaults; + + @InjectExecutorService(threadCount = 2, threadPrefix = "test-foo-static-executor", allowedOperations = TX_STATE_STORAGE_ACCESS) + private static ExecutorService staticExecutorService; + + @InjectExecutorService + private static ScheduledExecutorService staticScheduledExecutorServiceWithDefaults; + + @InjectExecutorService( + threadCount = 3, + threadPrefix = "test-bar-static-executor", + allowedOperations = {STORAGE_READ, STORAGE_WRITE} + ) + private static ScheduledExecutorService staticScheduledExecutorService; + + @InjectExecutorService + private ExecutorService fieldExecutorServiceWithDefaults; Review Comment: ```suggestion private ExecutorService instanceFieldExecutorServiceWithDefaults; ``` ########## modules/runner/src/main/java/org/apache/ignite/internal/app/IgniteImpl.java: ########## @@ -691,7 +691,8 @@ public class IgniteImpl implements Ignite { name, metastorageWorkDir.dbPath(), failureManager, - readOperationForCompactionTracker + readOperationForCompactionTracker, + threadPoolsManager.commonScheduler() Review Comment: `threadPoolsManager.commonScheduler()` should only be used for light, fast tasks, never doing I/O, but here it's used for I/O, as it seems. Let's use another pool here ########## modules/core/src/test/java/org/apache/ignite/internal/testframework/ExecutorServiceExtensionTest.java: ########## @@ -0,0 +1,207 @@ +/* + * 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.util.concurrent.CompletableFuture.runAsync; +import static org.apache.ignite.internal.testframework.JunitExtensionTestUtils.assertExecutesSuccessfully; +import static org.apache.ignite.internal.testframework.JunitExtensionTestUtils.assertExecutesWithFailure; +import static org.apache.ignite.internal.testframework.matchers.CompletableFutureMatcher.willCompleteSuccessfully; +import static org.apache.ignite.internal.thread.ThreadOperation.PROCESS_RAFT_REQ; +import static org.apache.ignite.internal.thread.ThreadOperation.STORAGE_READ; +import static org.apache.ignite.internal.thread.ThreadOperation.STORAGE_WRITE; +import static org.apache.ignite.internal.thread.ThreadOperation.TX_STATE_STORAGE_ACCESS; +import static org.apache.ignite.internal.thread.ThreadOperation.WAIT; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.containsInAnyOrder; +import static org.hamcrest.Matchers.containsString; +import static org.hamcrest.Matchers.instanceOf; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.fail; +import static org.junit.platform.testkit.engine.TestExecutionResultConditions.instanceOf; +import static org.junit.platform.testkit.engine.TestExecutionResultConditions.message; + +import java.util.concurrent.ExecutorService; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.ScheduledThreadPoolExecutor; +import java.util.concurrent.ThreadPoolExecutor; +import org.apache.ignite.internal.thread.IgniteThread; +import org.apache.ignite.internal.thread.ThreadOperation; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; + +/** For {@link ExecutorServiceExtension} testing. */ +public class ExecutorServiceExtensionTest { + private static final int CPU = Runtime.getRuntime().availableProcessors(); + + @ExtendWith(ExecutorServiceExtension.class) + static class NormalFieldInjectionTest { + @InjectExecutorService + private static ExecutorService staticExecutorServiceWithDefaults; + + @InjectExecutorService(threadCount = 2, threadPrefix = "test-foo-static-executor", allowedOperations = TX_STATE_STORAGE_ACCESS) + private static ExecutorService staticExecutorService; + + @InjectExecutorService + private static ScheduledExecutorService staticScheduledExecutorServiceWithDefaults; + + @InjectExecutorService( + threadCount = 3, + threadPrefix = "test-bar-static-executor", + allowedOperations = {STORAGE_READ, STORAGE_WRITE} + ) + private static ScheduledExecutorService staticScheduledExecutorService; + + @InjectExecutorService + private ExecutorService fieldExecutorServiceWithDefaults; + + @InjectExecutorService(threadCount = 4, threadPrefix = "test-foo-field-executor", allowedOperations = STORAGE_WRITE) + private ExecutorService fieldExecutorService; + + @InjectExecutorService + private ScheduledExecutorService fieldScheduledExecutorServiceWithDefaults; + + @InjectExecutorService(threadCount = 5, threadPrefix = "test-bar-field-executor", allowedOperations = PROCESS_RAFT_REQ) + private ScheduledExecutorService fieldScheduledExecutorService; + + @Test + void test( + @InjectExecutorService + ExecutorService parameterExecutorServiceWithDefaults, + @InjectExecutorService(threadCount = 6, threadPrefix = "test-foo-param-executor", allowedOperations = WAIT) + ExecutorService parameterExecutorService, + @InjectExecutorService + ScheduledExecutorService parameterScheduledExecutorServiceWithDefaults, + @InjectExecutorService(threadCount = 7, threadPrefix = "test-bar-param-executor", allowedOperations = WAIT) + ScheduledExecutorService parameterScheduledExecutorService + ) { + checkExecutorService( + staticExecutorServiceWithDefaults, + CPU, + "test-ExecutorService-staticExecutorServiceWithDefaults" + ); + checkScheduledExecutorService( + staticScheduledExecutorServiceWithDefaults, + 1, + "test-ScheduledExecutorService-staticScheduledExecutorServiceWithDefaults" + ); + checkExecutorService( + fieldExecutorServiceWithDefaults, + CPU, + "test-ExecutorService-fieldExecutorServiceWithDefaults" + ); + checkScheduledExecutorService( + fieldScheduledExecutorServiceWithDefaults, + 1, + "test-ScheduledExecutorService-fieldScheduledExecutorServiceWithDefaults" + ); + checkExecutorService( + parameterExecutorServiceWithDefaults, + CPU, + "test-ExecutorService-arg" + ); + checkScheduledExecutorService( + parameterScheduledExecutorServiceWithDefaults, + 1, + "test-ScheduledExecutorService-arg" + ); + + checkExecutorService(staticExecutorService, 2, "test-foo-static-executor", TX_STATE_STORAGE_ACCESS); + checkScheduledExecutorService(staticScheduledExecutorService, 3, "test-bar-static-executor", STORAGE_READ, STORAGE_WRITE); + checkExecutorService(fieldExecutorService, 4, "test-foo-field-executor", STORAGE_WRITE); + checkScheduledExecutorService(fieldScheduledExecutorService, 5, "test-bar-field-executor", PROCESS_RAFT_REQ); + checkExecutorService(parameterExecutorService, 6, "test-foo-param-executor", WAIT); + checkScheduledExecutorService(parameterScheduledExecutorService, 7, "test-bar-param-executor", WAIT); + } + } + + @ExtendWith(ExecutorServiceExtension.class) + static class ErrorFieldInjectionTest { + @InjectExecutorService + private static Integer staticExecutorService; + + @InjectExecutorService + private String fieldExecutorService; + + @Test + public void test( + @InjectExecutorService + Boolean parameterExecutorService + ) { + fail("Should not reach here"); + } + } + + @Test + void testFieldInjection() { + assertExecutesSuccessfully(NormalFieldInjectionTest.class); + } + + @Test + void testErrorStaticFieldInjection() { + assertExecutesWithFailure( + ErrorFieldInjectionTest.class, + instanceOf(IllegalStateException.class), + message(m -> m.contains("Unsupported field type")) + ); + } + + private static void checkExecutorService( + ExecutorService service, + int expCorePoolSize, + String expThreadPrefix, + ThreadOperation... expThreadOperations + ) { + assertThat(service, instanceOf(ThreadPoolExecutor.class)); + + checkThreadPoolExecutor((ThreadPoolExecutor) service, expCorePoolSize, expThreadPrefix, expThreadOperations); + } + + private static void checkScheduledExecutorService( + ScheduledExecutorService service, + int expCorePoolSize, + String expThreadPrefix, + ThreadOperation... expThreadOperations + ) { + assertThat(service, instanceOf(ScheduledThreadPoolExecutor.class)); + + checkThreadPoolExecutor((ScheduledThreadPoolExecutor) service, expCorePoolSize, expThreadPrefix, expThreadOperations); + } + + private static void checkThreadPoolExecutor( + ThreadPoolExecutor executor, + int expCorePoolSize, + String expThreadPrefix, + ThreadOperation... expThreadOperations + ) { + assertEquals(executor.getCorePoolSize(), expCorePoolSize); + + assertThat( + runAsync(() -> { + Thread thread = Thread.currentThread(); + + assertThat(thread, instanceOf(IgniteThread.class)); + + IgniteThread thread1 = (IgniteThread) thread; + + assertThat(thread1.getName(), containsString(expThreadPrefix)); Review Comment: ```suggestion assertThat(thread1.getName(), startsWith(expThreadPrefix)); ``` ########## modules/core/src/test/java/org/apache/ignite/internal/testframework/ExecutorServiceExtensionTest.java: ########## @@ -0,0 +1,207 @@ +/* + * 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.util.concurrent.CompletableFuture.runAsync; +import static org.apache.ignite.internal.testframework.JunitExtensionTestUtils.assertExecutesSuccessfully; +import static org.apache.ignite.internal.testframework.JunitExtensionTestUtils.assertExecutesWithFailure; +import static org.apache.ignite.internal.testframework.matchers.CompletableFutureMatcher.willCompleteSuccessfully; +import static org.apache.ignite.internal.thread.ThreadOperation.PROCESS_RAFT_REQ; +import static org.apache.ignite.internal.thread.ThreadOperation.STORAGE_READ; +import static org.apache.ignite.internal.thread.ThreadOperation.STORAGE_WRITE; +import static org.apache.ignite.internal.thread.ThreadOperation.TX_STATE_STORAGE_ACCESS; +import static org.apache.ignite.internal.thread.ThreadOperation.WAIT; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.containsInAnyOrder; +import static org.hamcrest.Matchers.containsString; +import static org.hamcrest.Matchers.instanceOf; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.fail; +import static org.junit.platform.testkit.engine.TestExecutionResultConditions.instanceOf; +import static org.junit.platform.testkit.engine.TestExecutionResultConditions.message; + +import java.util.concurrent.ExecutorService; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.ScheduledThreadPoolExecutor; +import java.util.concurrent.ThreadPoolExecutor; +import org.apache.ignite.internal.thread.IgniteThread; +import org.apache.ignite.internal.thread.ThreadOperation; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; + +/** For {@link ExecutorServiceExtension} testing. */ +public class ExecutorServiceExtensionTest { + private static final int CPU = Runtime.getRuntime().availableProcessors(); + + @ExtendWith(ExecutorServiceExtension.class) + static class NormalFieldInjectionTest { + @InjectExecutorService + private static ExecutorService staticExecutorServiceWithDefaults; + + @InjectExecutorService(threadCount = 2, threadPrefix = "test-foo-static-executor", allowedOperations = TX_STATE_STORAGE_ACCESS) + private static ExecutorService staticExecutorService; + + @InjectExecutorService + private static ScheduledExecutorService staticScheduledExecutorServiceWithDefaults; + + @InjectExecutorService( + threadCount = 3, + threadPrefix = "test-bar-static-executor", + allowedOperations = {STORAGE_READ, STORAGE_WRITE} + ) + private static ScheduledExecutorService staticScheduledExecutorService; + + @InjectExecutorService + private ExecutorService fieldExecutorServiceWithDefaults; + + @InjectExecutorService(threadCount = 4, threadPrefix = "test-foo-field-executor", allowedOperations = STORAGE_WRITE) + private ExecutorService fieldExecutorService; + + @InjectExecutorService + private ScheduledExecutorService fieldScheduledExecutorServiceWithDefaults; + + @InjectExecutorService(threadCount = 5, threadPrefix = "test-bar-field-executor", allowedOperations = PROCESS_RAFT_REQ) + private ScheduledExecutorService fieldScheduledExecutorService; Review Comment: ```suggestion private ScheduledExecutorService instanceFieldScheduledExecutorService; ``` ########## modules/core/src/test/java/org/apache/ignite/internal/testframework/ExecutorServiceExtensionTest.java: ########## @@ -0,0 +1,207 @@ +/* + * 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.util.concurrent.CompletableFuture.runAsync; +import static org.apache.ignite.internal.testframework.JunitExtensionTestUtils.assertExecutesSuccessfully; +import static org.apache.ignite.internal.testframework.JunitExtensionTestUtils.assertExecutesWithFailure; +import static org.apache.ignite.internal.testframework.matchers.CompletableFutureMatcher.willCompleteSuccessfully; +import static org.apache.ignite.internal.thread.ThreadOperation.PROCESS_RAFT_REQ; +import static org.apache.ignite.internal.thread.ThreadOperation.STORAGE_READ; +import static org.apache.ignite.internal.thread.ThreadOperation.STORAGE_WRITE; +import static org.apache.ignite.internal.thread.ThreadOperation.TX_STATE_STORAGE_ACCESS; +import static org.apache.ignite.internal.thread.ThreadOperation.WAIT; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.containsInAnyOrder; +import static org.hamcrest.Matchers.containsString; +import static org.hamcrest.Matchers.instanceOf; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.fail; +import static org.junit.platform.testkit.engine.TestExecutionResultConditions.instanceOf; +import static org.junit.platform.testkit.engine.TestExecutionResultConditions.message; + +import java.util.concurrent.ExecutorService; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.ScheduledThreadPoolExecutor; +import java.util.concurrent.ThreadPoolExecutor; +import org.apache.ignite.internal.thread.IgniteThread; +import org.apache.ignite.internal.thread.ThreadOperation; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; + +/** For {@link ExecutorServiceExtension} testing. */ +public class ExecutorServiceExtensionTest { + private static final int CPU = Runtime.getRuntime().availableProcessors(); + + @ExtendWith(ExecutorServiceExtension.class) + static class NormalFieldInjectionTest { + @InjectExecutorService + private static ExecutorService staticExecutorServiceWithDefaults; + + @InjectExecutorService(threadCount = 2, threadPrefix = "test-foo-static-executor", allowedOperations = TX_STATE_STORAGE_ACCESS) + private static ExecutorService staticExecutorService; + + @InjectExecutorService + private static ScheduledExecutorService staticScheduledExecutorServiceWithDefaults; + + @InjectExecutorService( + threadCount = 3, + threadPrefix = "test-bar-static-executor", + allowedOperations = {STORAGE_READ, STORAGE_WRITE} + ) + private static ScheduledExecutorService staticScheduledExecutorService; + + @InjectExecutorService + private ExecutorService fieldExecutorServiceWithDefaults; + + @InjectExecutorService(threadCount = 4, threadPrefix = "test-foo-field-executor", allowedOperations = STORAGE_WRITE) + private ExecutorService fieldExecutorService; + + @InjectExecutorService + private ScheduledExecutorService fieldScheduledExecutorServiceWithDefaults; + + @InjectExecutorService(threadCount = 5, threadPrefix = "test-bar-field-executor", allowedOperations = PROCESS_RAFT_REQ) + private ScheduledExecutorService fieldScheduledExecutorService; + + @Test + void test( + @InjectExecutorService + ExecutorService parameterExecutorServiceWithDefaults, + @InjectExecutorService(threadCount = 6, threadPrefix = "test-foo-param-executor", allowedOperations = WAIT) + ExecutorService parameterExecutorService, + @InjectExecutorService + ScheduledExecutorService parameterScheduledExecutorServiceWithDefaults, + @InjectExecutorService(threadCount = 7, threadPrefix = "test-bar-param-executor", allowedOperations = WAIT) + ScheduledExecutorService parameterScheduledExecutorService + ) { + checkExecutorService( + staticExecutorServiceWithDefaults, + CPU, + "test-ExecutorService-staticExecutorServiceWithDefaults" + ); + checkScheduledExecutorService( + staticScheduledExecutorServiceWithDefaults, + 1, + "test-ScheduledExecutorService-staticScheduledExecutorServiceWithDefaults" + ); + checkExecutorService( + fieldExecutorServiceWithDefaults, + CPU, + "test-ExecutorService-fieldExecutorServiceWithDefaults" + ); + checkScheduledExecutorService( + fieldScheduledExecutorServiceWithDefaults, + 1, + "test-ScheduledExecutorService-fieldScheduledExecutorServiceWithDefaults" + ); + checkExecutorService( + parameterExecutorServiceWithDefaults, + CPU, + "test-ExecutorService-arg" + ); + checkScheduledExecutorService( + parameterScheduledExecutorServiceWithDefaults, + 1, + "test-ScheduledExecutorService-arg" + ); + + checkExecutorService(staticExecutorService, 2, "test-foo-static-executor", TX_STATE_STORAGE_ACCESS); + checkScheduledExecutorService(staticScheduledExecutorService, 3, "test-bar-static-executor", STORAGE_READ, STORAGE_WRITE); + checkExecutorService(fieldExecutorService, 4, "test-foo-field-executor", STORAGE_WRITE); + checkScheduledExecutorService(fieldScheduledExecutorService, 5, "test-bar-field-executor", PROCESS_RAFT_REQ); + checkExecutorService(parameterExecutorService, 6, "test-foo-param-executor", WAIT); + checkScheduledExecutorService(parameterScheduledExecutorService, 7, "test-bar-param-executor", WAIT); + } + } + + @ExtendWith(ExecutorServiceExtension.class) + static class ErrorFieldInjectionTest { + @InjectExecutorService + private static Integer staticExecutorService; Review Comment: ```suggestion private static Integer staticWrongTypeTarget; ``` ########## modules/core/src/test/java/org/apache/ignite/internal/testframework/ExecutorServiceExtensionTest.java: ########## @@ -0,0 +1,207 @@ +/* + * 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.util.concurrent.CompletableFuture.runAsync; +import static org.apache.ignite.internal.testframework.JunitExtensionTestUtils.assertExecutesSuccessfully; +import static org.apache.ignite.internal.testframework.JunitExtensionTestUtils.assertExecutesWithFailure; +import static org.apache.ignite.internal.testframework.matchers.CompletableFutureMatcher.willCompleteSuccessfully; +import static org.apache.ignite.internal.thread.ThreadOperation.PROCESS_RAFT_REQ; +import static org.apache.ignite.internal.thread.ThreadOperation.STORAGE_READ; +import static org.apache.ignite.internal.thread.ThreadOperation.STORAGE_WRITE; +import static org.apache.ignite.internal.thread.ThreadOperation.TX_STATE_STORAGE_ACCESS; +import static org.apache.ignite.internal.thread.ThreadOperation.WAIT; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.containsInAnyOrder; +import static org.hamcrest.Matchers.containsString; +import static org.hamcrest.Matchers.instanceOf; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.fail; +import static org.junit.platform.testkit.engine.TestExecutionResultConditions.instanceOf; +import static org.junit.platform.testkit.engine.TestExecutionResultConditions.message; + +import java.util.concurrent.ExecutorService; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.ScheduledThreadPoolExecutor; +import java.util.concurrent.ThreadPoolExecutor; +import org.apache.ignite.internal.thread.IgniteThread; +import org.apache.ignite.internal.thread.ThreadOperation; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; + +/** For {@link ExecutorServiceExtension} testing. */ +public class ExecutorServiceExtensionTest { + private static final int CPU = Runtime.getRuntime().availableProcessors(); + + @ExtendWith(ExecutorServiceExtension.class) + static class NormalFieldInjectionTest { + @InjectExecutorService + private static ExecutorService staticExecutorServiceWithDefaults; + + @InjectExecutorService(threadCount = 2, threadPrefix = "test-foo-static-executor", allowedOperations = TX_STATE_STORAGE_ACCESS) + private static ExecutorService staticExecutorService; + + @InjectExecutorService + private static ScheduledExecutorService staticScheduledExecutorServiceWithDefaults; + + @InjectExecutorService( + threadCount = 3, + threadPrefix = "test-bar-static-executor", + allowedOperations = {STORAGE_READ, STORAGE_WRITE} + ) + private static ScheduledExecutorService staticScheduledExecutorService; + + @InjectExecutorService + private ExecutorService fieldExecutorServiceWithDefaults; + + @InjectExecutorService(threadCount = 4, threadPrefix = "test-foo-field-executor", allowedOperations = STORAGE_WRITE) + private ExecutorService fieldExecutorService; + + @InjectExecutorService + private ScheduledExecutorService fieldScheduledExecutorServiceWithDefaults; + + @InjectExecutorService(threadCount = 5, threadPrefix = "test-bar-field-executor", allowedOperations = PROCESS_RAFT_REQ) + private ScheduledExecutorService fieldScheduledExecutorService; + + @Test + void test( + @InjectExecutorService + ExecutorService parameterExecutorServiceWithDefaults, + @InjectExecutorService(threadCount = 6, threadPrefix = "test-foo-param-executor", allowedOperations = WAIT) + ExecutorService parameterExecutorService, + @InjectExecutorService + ScheduledExecutorService parameterScheduledExecutorServiceWithDefaults, + @InjectExecutorService(threadCount = 7, threadPrefix = "test-bar-param-executor", allowedOperations = WAIT) + ScheduledExecutorService parameterScheduledExecutorService + ) { + checkExecutorService( + staticExecutorServiceWithDefaults, + CPU, + "test-ExecutorService-staticExecutorServiceWithDefaults" + ); + checkScheduledExecutorService( + staticScheduledExecutorServiceWithDefaults, + 1, + "test-ScheduledExecutorService-staticScheduledExecutorServiceWithDefaults" + ); + checkExecutorService( + fieldExecutorServiceWithDefaults, + CPU, + "test-ExecutorService-fieldExecutorServiceWithDefaults" + ); + checkScheduledExecutorService( + fieldScheduledExecutorServiceWithDefaults, + 1, + "test-ScheduledExecutorService-fieldScheduledExecutorServiceWithDefaults" + ); + checkExecutorService( + parameterExecutorServiceWithDefaults, + CPU, + "test-ExecutorService-arg" + ); + checkScheduledExecutorService( + parameterScheduledExecutorServiceWithDefaults, + 1, + "test-ScheduledExecutorService-arg" + ); + + checkExecutorService(staticExecutorService, 2, "test-foo-static-executor", TX_STATE_STORAGE_ACCESS); + checkScheduledExecutorService(staticScheduledExecutorService, 3, "test-bar-static-executor", STORAGE_READ, STORAGE_WRITE); + checkExecutorService(fieldExecutorService, 4, "test-foo-field-executor", STORAGE_WRITE); + checkScheduledExecutorService(fieldScheduledExecutorService, 5, "test-bar-field-executor", PROCESS_RAFT_REQ); + checkExecutorService(parameterExecutorService, 6, "test-foo-param-executor", WAIT); + checkScheduledExecutorService(parameterScheduledExecutorService, 7, "test-bar-param-executor", WAIT); + } + } + + @ExtendWith(ExecutorServiceExtension.class) + static class ErrorFieldInjectionTest { + @InjectExecutorService + private static Integer staticExecutorService; + + @InjectExecutorService + private String fieldExecutorService; + + @Test + public void test( + @InjectExecutorService + Boolean parameterExecutorService Review Comment: ```suggestion Boolean parameterWrongTypeTarget ``` ########## 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); Review Comment: `getOrCreateExecutorServiceListInStore()` seems to already put the executors to the context ########## 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()); + } + + private static ExecutorService createExecutorService( + InjectExecutorService injectExecutorService, + String fieldName, + Class<?> fieldType, + Class<?> testClass + ) { + int threadCount = injectExecutorService.threadCount(); + String threadPrefix = threadPrefix(injectExecutorService, fieldName, fieldType); + ThreadOperation[] allowedOperations = injectExecutorService.allowedOperations(); + + ThreadFactory threadFactory = IgniteThreadFactory.withPrefix(threadPrefix, Loggers.forClass(testClass), allowedOperations); + + if (fieldType.equals(ScheduledExecutorService.class)) { + return newScheduledThreadPool(threadCount == 0 ? 1 : threadCount, threadFactory); + } else if (fieldType.equals(ExecutorService.class)) { + return newFixedThreadPool(threadCount == 0 ? CPUS : threadCount, threadFactory); + } + + throw new AssertionError( + String.format("Unsupported field type: [field=%s, supportedFieldTypes=%s]", fieldName, SUPPORTED_FIELD_TYPES) + ); + } + + private static String threadPrefix(InjectExecutorService injectExecutorService, String fieldName, Class<?> fieldType) { + String threadPrefix = injectExecutorService.threadPrefix(); + + if (threadPrefix != null && !"".equals(threadPrefix)) { + return threadPrefix; + } + + return String.format("test-%s-%s", fieldType.getSimpleName(), fieldName); Review Comment: What if there are two fields named identically in a superclass and subclass? It seems their executors will get the same prefix ########## modules/core/src/test/java/org/apache/ignite/internal/testframework/ExecutorServiceExtensionTest.java: ########## @@ -0,0 +1,207 @@ +/* + * 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.util.concurrent.CompletableFuture.runAsync; +import static org.apache.ignite.internal.testframework.JunitExtensionTestUtils.assertExecutesSuccessfully; +import static org.apache.ignite.internal.testframework.JunitExtensionTestUtils.assertExecutesWithFailure; +import static org.apache.ignite.internal.testframework.matchers.CompletableFutureMatcher.willCompleteSuccessfully; +import static org.apache.ignite.internal.thread.ThreadOperation.PROCESS_RAFT_REQ; +import static org.apache.ignite.internal.thread.ThreadOperation.STORAGE_READ; +import static org.apache.ignite.internal.thread.ThreadOperation.STORAGE_WRITE; +import static org.apache.ignite.internal.thread.ThreadOperation.TX_STATE_STORAGE_ACCESS; +import static org.apache.ignite.internal.thread.ThreadOperation.WAIT; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.containsInAnyOrder; +import static org.hamcrest.Matchers.containsString; +import static org.hamcrest.Matchers.instanceOf; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.fail; +import static org.junit.platform.testkit.engine.TestExecutionResultConditions.instanceOf; +import static org.junit.platform.testkit.engine.TestExecutionResultConditions.message; + +import java.util.concurrent.ExecutorService; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.ScheduledThreadPoolExecutor; +import java.util.concurrent.ThreadPoolExecutor; +import org.apache.ignite.internal.thread.IgniteThread; +import org.apache.ignite.internal.thread.ThreadOperation; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; + +/** For {@link ExecutorServiceExtension} testing. */ +public class ExecutorServiceExtensionTest { + private static final int CPU = Runtime.getRuntime().availableProcessors(); + + @ExtendWith(ExecutorServiceExtension.class) + static class NormalFieldInjectionTest { + @InjectExecutorService + private static ExecutorService staticExecutorServiceWithDefaults; + + @InjectExecutorService(threadCount = 2, threadPrefix = "test-foo-static-executor", allowedOperations = TX_STATE_STORAGE_ACCESS) + private static ExecutorService staticExecutorService; + + @InjectExecutorService + private static ScheduledExecutorService staticScheduledExecutorServiceWithDefaults; + + @InjectExecutorService( + threadCount = 3, + threadPrefix = "test-bar-static-executor", + allowedOperations = {STORAGE_READ, STORAGE_WRITE} + ) + private static ScheduledExecutorService staticScheduledExecutorService; + + @InjectExecutorService + private ExecutorService fieldExecutorServiceWithDefaults; + + @InjectExecutorService(threadCount = 4, threadPrefix = "test-foo-field-executor", allowedOperations = STORAGE_WRITE) + private ExecutorService fieldExecutorService; + + @InjectExecutorService + private ScheduledExecutorService fieldScheduledExecutorServiceWithDefaults; + + @InjectExecutorService(threadCount = 5, threadPrefix = "test-bar-field-executor", allowedOperations = PROCESS_RAFT_REQ) + private ScheduledExecutorService fieldScheduledExecutorService; + + @Test + void test( + @InjectExecutorService + ExecutorService parameterExecutorServiceWithDefaults, + @InjectExecutorService(threadCount = 6, threadPrefix = "test-foo-param-executor", allowedOperations = WAIT) + ExecutorService parameterExecutorService, + @InjectExecutorService + ScheduledExecutorService parameterScheduledExecutorServiceWithDefaults, + @InjectExecutorService(threadCount = 7, threadPrefix = "test-bar-param-executor", allowedOperations = WAIT) + ScheduledExecutorService parameterScheduledExecutorService + ) { + checkExecutorService( + staticExecutorServiceWithDefaults, + CPU, + "test-ExecutorService-staticExecutorServiceWithDefaults" + ); + checkScheduledExecutorService( + staticScheduledExecutorServiceWithDefaults, + 1, + "test-ScheduledExecutorService-staticScheduledExecutorServiceWithDefaults" + ); + checkExecutorService( + fieldExecutorServiceWithDefaults, + CPU, + "test-ExecutorService-fieldExecutorServiceWithDefaults" + ); + checkScheduledExecutorService( + fieldScheduledExecutorServiceWithDefaults, + 1, + "test-ScheduledExecutorService-fieldScheduledExecutorServiceWithDefaults" + ); + checkExecutorService( + parameterExecutorServiceWithDefaults, + CPU, + "test-ExecutorService-arg" + ); + checkScheduledExecutorService( + parameterScheduledExecutorServiceWithDefaults, + 1, + "test-ScheduledExecutorService-arg" + ); + + checkExecutorService(staticExecutorService, 2, "test-foo-static-executor", TX_STATE_STORAGE_ACCESS); + checkScheduledExecutorService(staticScheduledExecutorService, 3, "test-bar-static-executor", STORAGE_READ, STORAGE_WRITE); + checkExecutorService(fieldExecutorService, 4, "test-foo-field-executor", STORAGE_WRITE); + checkScheduledExecutorService(fieldScheduledExecutorService, 5, "test-bar-field-executor", PROCESS_RAFT_REQ); + checkExecutorService(parameterExecutorService, 6, "test-foo-param-executor", WAIT); + checkScheduledExecutorService(parameterScheduledExecutorService, 7, "test-bar-param-executor", WAIT); + } + } + + @ExtendWith(ExecutorServiceExtension.class) + static class ErrorFieldInjectionTest { + @InjectExecutorService + private static Integer staticExecutorService; + + @InjectExecutorService + private String fieldExecutorService; + + @Test + public void test( + @InjectExecutorService + Boolean parameterExecutorService + ) { + fail("Should not reach here"); + } + } + + @Test + void testFieldInjection() { + assertExecutesSuccessfully(NormalFieldInjectionTest.class); + } + + @Test + void testErrorStaticFieldInjection() { Review Comment: ```suggestion void testWrongTypeInjection() { ``` ########## modules/metastorage/src/main/java/org/apache/ignite/internal/metastorage/server/persistence/RocksDbKeyValueStorage.java: ########## @@ -299,16 +301,12 @@ public RocksDbKeyValueStorage( ); this.dbPath = dbPath; + this.scheduledExecutor = scheduledExecutor; Review Comment: Is this executor used for scheduling absolutely any tasks, or for some specific tasks (like handling RocksDB-related stuff)? If the latter is true, would it make sense to rename it to reflect its purpose? ########## modules/core/src/test/java/org/apache/ignite/internal/testframework/ExecutorServiceExtensionTest.java: ########## @@ -0,0 +1,207 @@ +/* + * 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.util.concurrent.CompletableFuture.runAsync; +import static org.apache.ignite.internal.testframework.JunitExtensionTestUtils.assertExecutesSuccessfully; +import static org.apache.ignite.internal.testframework.JunitExtensionTestUtils.assertExecutesWithFailure; +import static org.apache.ignite.internal.testframework.matchers.CompletableFutureMatcher.willCompleteSuccessfully; +import static org.apache.ignite.internal.thread.ThreadOperation.PROCESS_RAFT_REQ; +import static org.apache.ignite.internal.thread.ThreadOperation.STORAGE_READ; +import static org.apache.ignite.internal.thread.ThreadOperation.STORAGE_WRITE; +import static org.apache.ignite.internal.thread.ThreadOperation.TX_STATE_STORAGE_ACCESS; +import static org.apache.ignite.internal.thread.ThreadOperation.WAIT; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.containsInAnyOrder; +import static org.hamcrest.Matchers.containsString; +import static org.hamcrest.Matchers.instanceOf; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.fail; +import static org.junit.platform.testkit.engine.TestExecutionResultConditions.instanceOf; +import static org.junit.platform.testkit.engine.TestExecutionResultConditions.message; + +import java.util.concurrent.ExecutorService; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.ScheduledThreadPoolExecutor; +import java.util.concurrent.ThreadPoolExecutor; +import org.apache.ignite.internal.thread.IgniteThread; +import org.apache.ignite.internal.thread.ThreadOperation; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; + +/** For {@link ExecutorServiceExtension} testing. */ +public class ExecutorServiceExtensionTest { + private static final int CPU = Runtime.getRuntime().availableProcessors(); + + @ExtendWith(ExecutorServiceExtension.class) + static class NormalFieldInjectionTest { + @InjectExecutorService + private static ExecutorService staticExecutorServiceWithDefaults; + + @InjectExecutorService(threadCount = 2, threadPrefix = "test-foo-static-executor", allowedOperations = TX_STATE_STORAGE_ACCESS) + private static ExecutorService staticExecutorService; + + @InjectExecutorService + private static ScheduledExecutorService staticScheduledExecutorServiceWithDefaults; + + @InjectExecutorService( + threadCount = 3, + threadPrefix = "test-bar-static-executor", + allowedOperations = {STORAGE_READ, STORAGE_WRITE} + ) + private static ScheduledExecutorService staticScheduledExecutorService; + + @InjectExecutorService + private ExecutorService fieldExecutorServiceWithDefaults; + + @InjectExecutorService(threadCount = 4, threadPrefix = "test-foo-field-executor", allowedOperations = STORAGE_WRITE) + private ExecutorService fieldExecutorService; + + @InjectExecutorService + private ScheduledExecutorService fieldScheduledExecutorServiceWithDefaults; + + @InjectExecutorService(threadCount = 5, threadPrefix = "test-bar-field-executor", allowedOperations = PROCESS_RAFT_REQ) + private ScheduledExecutorService fieldScheduledExecutorService; + + @Test + void test( + @InjectExecutorService + ExecutorService parameterExecutorServiceWithDefaults, + @InjectExecutorService(threadCount = 6, threadPrefix = "test-foo-param-executor", allowedOperations = WAIT) + ExecutorService parameterExecutorService, + @InjectExecutorService + ScheduledExecutorService parameterScheduledExecutorServiceWithDefaults, + @InjectExecutorService(threadCount = 7, threadPrefix = "test-bar-param-executor", allowedOperations = WAIT) + ScheduledExecutorService parameterScheduledExecutorService + ) { + checkExecutorService( + staticExecutorServiceWithDefaults, + CPU, + "test-ExecutorService-staticExecutorServiceWithDefaults" + ); + checkScheduledExecutorService( + staticScheduledExecutorServiceWithDefaults, + 1, + "test-ScheduledExecutorService-staticScheduledExecutorServiceWithDefaults" + ); + checkExecutorService( + fieldExecutorServiceWithDefaults, + CPU, + "test-ExecutorService-fieldExecutorServiceWithDefaults" + ); + checkScheduledExecutorService( + fieldScheduledExecutorServiceWithDefaults, + 1, + "test-ScheduledExecutorService-fieldScheduledExecutorServiceWithDefaults" + ); + checkExecutorService( + parameterExecutorServiceWithDefaults, + CPU, + "test-ExecutorService-arg" + ); + checkScheduledExecutorService( + parameterScheduledExecutorServiceWithDefaults, + 1, + "test-ScheduledExecutorService-arg" + ); + + checkExecutorService(staticExecutorService, 2, "test-foo-static-executor", TX_STATE_STORAGE_ACCESS); + checkScheduledExecutorService(staticScheduledExecutorService, 3, "test-bar-static-executor", STORAGE_READ, STORAGE_WRITE); + checkExecutorService(fieldExecutorService, 4, "test-foo-field-executor", STORAGE_WRITE); + checkScheduledExecutorService(fieldScheduledExecutorService, 5, "test-bar-field-executor", PROCESS_RAFT_REQ); + checkExecutorService(parameterExecutorService, 6, "test-foo-param-executor", WAIT); + checkScheduledExecutorService(parameterScheduledExecutorService, 7, "test-bar-param-executor", WAIT); + } + } + + @ExtendWith(ExecutorServiceExtension.class) + static class ErrorFieldInjectionTest { + @InjectExecutorService + private static Integer staticExecutorService; + + @InjectExecutorService + private String fieldExecutorService; Review Comment: ```suggestion private String instanceFieldWrongTypeTarget; ``` ########## 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() Review Comment: Should we shut them down in parallel? ########## 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: I wonder if parameter resolvers can be used on static methods (like `@BeforeAll`). If they can, this `false` might be wrong -- 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