|
16 | 16 |
|
17 | 17 | package org.springframework.scheduling.concurrent;
|
18 | 18 |
|
| 19 | +import java.util.concurrent.TimeUnit; |
| 20 | + |
| 21 | +import org.junit.jupiter.api.Test; |
19 | 22 | import org.springframework.core.task.AsyncListenableTaskExecutor;
|
20 | 23 |
|
| 24 | +import static org.assertj.core.api.Assertions.assertThat; |
| 25 | +import static org.assertj.core.api.Assertions.assertThatThrownBy; |
| 26 | + |
21 | 27 | /**
|
22 | 28 | * @author Juergen Hoeller
|
23 | 29 | * @since 5.0.5
|
24 | 30 | */
|
25 | 31 | class ThreadPoolTaskExecutorTests extends AbstractSchedulingTaskExecutorTests {
|
26 | 32 |
|
| 33 | + private final ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); |
| 34 | + |
27 | 35 | @Override
|
28 | 36 | protected AsyncListenableTaskExecutor buildExecutor() {
|
29 |
| - ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); |
30 | 37 | executor.setThreadNamePrefix(this.threadNamePrefix);
|
31 | 38 | executor.setMaxPoolSize(1);
|
32 | 39 | executor.afterPropertiesSet();
|
33 | 40 | return executor;
|
34 | 41 | }
|
35 | 42 |
|
| 43 | + @Test |
| 44 | + void modifyCorePoolSizeWhileRunning() { |
| 45 | + assertThat(executor.getCorePoolSize()).isEqualTo(1); |
| 46 | + assertThat(executor.getThreadPoolExecutor().getCorePoolSize()).isEqualTo(1); |
| 47 | + |
| 48 | + executor.setCorePoolSize(0); |
| 49 | + |
| 50 | + assertThat(executor.getCorePoolSize()).isEqualTo(0); |
| 51 | + assertThat(executor.getThreadPoolExecutor().getCorePoolSize()).isEqualTo(0); |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + void modifyCorePoolSizeWithInvalidValueWhileRunning() { |
| 56 | + assertThat(executor.getCorePoolSize()).isEqualTo(1); |
| 57 | + assertThat(executor.getThreadPoolExecutor().getCorePoolSize()).isEqualTo(1); |
| 58 | + |
| 59 | + assertThatThrownBy(() -> executor.setCorePoolSize(-1)) |
| 60 | + .isInstanceOf(IllegalArgumentException.class); |
| 61 | + |
| 62 | + assertThat(executor.getCorePoolSize()).isEqualTo(1); |
| 63 | + assertThat(executor.getThreadPoolExecutor().getCorePoolSize()).isEqualTo(1); |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + void modifyMaxPoolSizeWhileRunning() { |
| 68 | + assertThat(executor.getMaxPoolSize()).isEqualTo(1); |
| 69 | + assertThat(executor.getThreadPoolExecutor().getMaximumPoolSize()).isEqualTo(1); |
| 70 | + |
| 71 | + executor.setMaxPoolSize(5); |
| 72 | + |
| 73 | + assertThat(executor.getMaxPoolSize()).isEqualTo(5); |
| 74 | + assertThat(executor.getThreadPoolExecutor().getMaximumPoolSize()).isEqualTo(5); |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + void modifyMaxPoolSizeWithInvalidValueWhileRunning() { |
| 79 | + assertThat(executor.getMaxPoolSize()).isEqualTo(1); |
| 80 | + assertThat(executor.getThreadPoolExecutor().getMaximumPoolSize()).isEqualTo(1); |
| 81 | + |
| 82 | + assertThatThrownBy(() -> executor.setMaxPoolSize(0)) |
| 83 | + .isInstanceOf(IllegalArgumentException.class); |
| 84 | + |
| 85 | + assertThat(executor.getMaxPoolSize()).isEqualTo(1); |
| 86 | + assertThat(executor.getThreadPoolExecutor().getMaximumPoolSize()).isEqualTo(1); |
| 87 | + } |
| 88 | + |
| 89 | + @Test |
| 90 | + void modifyKeepAliveSecondsWhileRunning() { |
| 91 | + assertThat(executor.getKeepAliveSeconds()).isEqualTo(60); |
| 92 | + assertThat(executor.getThreadPoolExecutor().getKeepAliveTime(TimeUnit.SECONDS)).isEqualTo(60); |
| 93 | + |
| 94 | + executor.setKeepAliveSeconds(10); |
| 95 | + |
| 96 | + assertThat(executor.getKeepAliveSeconds()).isEqualTo(10); |
| 97 | + assertThat(executor.getThreadPoolExecutor().getKeepAliveTime(TimeUnit.SECONDS)).isEqualTo(10); |
| 98 | + } |
| 99 | + |
| 100 | + @Test |
| 101 | + void modifyKeepAliveSecondsWithInvalidValueWhileRunning() { |
| 102 | + assertThat(executor.getKeepAliveSeconds()).isEqualTo(60); |
| 103 | + assertThat(executor.getThreadPoolExecutor().getKeepAliveTime(TimeUnit.SECONDS)).isEqualTo(60); |
| 104 | + |
| 105 | + assertThatThrownBy(() -> executor.setKeepAliveSeconds(-10)) |
| 106 | + .isInstanceOf(IllegalArgumentException.class); |
| 107 | + |
| 108 | + assertThat(executor.getKeepAliveSeconds()).isEqualTo(60); |
| 109 | + assertThat(executor.getThreadPoolExecutor().getKeepAliveTime(TimeUnit.SECONDS)).isEqualTo(60); |
| 110 | + } |
36 | 111 | }
|
0 commit comments