Expose prestartAllCoreThreads on ExecutorService

See gh-1246
This commit is contained in:
Federico Donnarumma
2016-11-25 17:33:49 -03:00
committed by Stephane Nicoll
parent 2271b6078e
commit 2c53e9e308
2 changed files with 83 additions and 2 deletions

View File

@@ -16,17 +16,25 @@
package org.springframework.scheduling.concurrent;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.FutureTask;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import org.junit.jupiter.api.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
/**
* @author Juergen Hoeller
@@ -44,6 +52,21 @@ class ThreadPoolExecutorFactoryBeanTests {
context.close();
}
@Test
public void executorWithPreStartedThreads() throws Exception {
ConfigurableApplicationContext context = new AnnotationConfigApplicationContext(ExecutorConfigWithPreStartedThreads.class);
ThreadPoolExecutor executor = context.getBean("childExecutor", ThreadPoolExecutor.class);
verify(executor).prestartAllCoreThreads();
}
@Test
public void executorWithNoPreStartedThreads() throws Exception {
ConfigurableApplicationContext context = new AnnotationConfigApplicationContext(ExecutorConfigWithNoPreStartedThreads.class);
ThreadPoolExecutor executor = context.getBean("childExecutor", ThreadPoolExecutor.class);
verify(executor, never()).prestartAllCoreThreads();
}
@Configuration
static class ExecutorConfig {
@@ -55,4 +78,45 @@ class ThreadPoolExecutorFactoryBeanTests {
}
@Configuration
public static class ExecutorConfigWithPreStartedThreads {
@Bean
public ThreadPoolExecutorFactoryBean executorChildFactory() {
ThreadPoolExecutorFactoryBeanMockingChild threadPoolExecutorFactoryBeanMockingChild = new ThreadPoolExecutorFactoryBeanMockingChild();
threadPoolExecutorFactoryBeanMockingChild.setPrestartAllCoreThreads(true);
return threadPoolExecutorFactoryBeanMockingChild;
}
@Bean
public ExecutorService childExecutor() {
return executorChildFactory().getObject();
}
}
@Configuration
public static class ExecutorConfigWithNoPreStartedThreads {
@Bean
public ThreadPoolExecutorFactoryBean executorChildFactory() {
return new ThreadPoolExecutorFactoryBeanMockingChild();
}
@Bean
public ExecutorService childExecutor() {
return executorChildFactory().getObject();
}
}
private static class ThreadPoolExecutorFactoryBeanMockingChild extends ThreadPoolExecutorFactoryBean {
@Override
protected ThreadPoolExecutor createExecutor(
int corePoolSize, int maxPoolSize, int keepAliveSeconds, BlockingQueue<Runnable> queue,
ThreadFactory threadFactory, RejectedExecutionHandler rejectedExecutionHandler) {
return mock(ThreadPoolExecutor.class);
}
}
}