Extract out common code for starting threads into startThreads method
This commit is contained in:
parent
79af99356e
commit
9d9ad16636
|
@ -9,22 +9,26 @@ import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
|
||||||
|
|
||||||
public class ThreadPoolTaskExecutorUnitTest {
|
public class ThreadPoolTaskExecutorUnitTest {
|
||||||
|
|
||||||
@Test
|
private void startThreads(ThreadPoolTaskExecutor taskExecutor, CountDownLatch countDownLatch, int numThreads) {
|
||||||
public void whenUsingDefaults_thenSingleThread() {
|
for (int i = 0; i < numThreads; i++) {
|
||||||
ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
|
|
||||||
taskExecutor.afterPropertiesSet();
|
|
||||||
|
|
||||||
CountDownLatch countDownLatch = new CountDownLatch(10);
|
|
||||||
for (int i = 0; i < 10; i++) {
|
|
||||||
taskExecutor.execute(() -> {
|
taskExecutor.execute(() -> {
|
||||||
try {
|
try {
|
||||||
Thread.sleep(10L * ThreadLocalRandom.current().nextLong(1, 10));
|
Thread.sleep(100L * ThreadLocalRandom.current().nextLong(1, 10));
|
||||||
countDownLatch.countDown();
|
countDownLatch.countDown();
|
||||||
} catch (InterruptedException e) {
|
} catch (InterruptedException e) {
|
||||||
Thread.currentThread().interrupt();
|
Thread.currentThread().interrupt();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenUsingDefaults_thenSingleThread() {
|
||||||
|
ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
|
||||||
|
taskExecutor.afterPropertiesSet();
|
||||||
|
|
||||||
|
CountDownLatch countDownLatch = new CountDownLatch(10);
|
||||||
|
this.startThreads(taskExecutor, countDownLatch, 10);
|
||||||
|
|
||||||
while (countDownLatch.getCount() > 0) {
|
while (countDownLatch.getCount() > 0) {
|
||||||
Assert.assertEquals(1, taskExecutor.getPoolSize());
|
Assert.assertEquals(1, taskExecutor.getPoolSize());
|
||||||
|
@ -38,16 +42,7 @@ public class ThreadPoolTaskExecutorUnitTest {
|
||||||
taskExecutor.afterPropertiesSet();
|
taskExecutor.afterPropertiesSet();
|
||||||
|
|
||||||
CountDownLatch countDownLatch = new CountDownLatch(10);
|
CountDownLatch countDownLatch = new CountDownLatch(10);
|
||||||
for (int i = 0; i < 10; i++) {
|
this.startThreads(taskExecutor, countDownLatch, 10);
|
||||||
taskExecutor.execute(() -> {
|
|
||||||
try {
|
|
||||||
Thread.sleep(100L * ThreadLocalRandom.current().nextLong(1, 10));
|
|
||||||
countDownLatch.countDown();
|
|
||||||
} catch (InterruptedException e) {
|
|
||||||
Thread.currentThread().interrupt();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
while (countDownLatch.getCount() > 0) {
|
while (countDownLatch.getCount() > 0) {
|
||||||
Assert.assertEquals(5, taskExecutor.getPoolSize());
|
Assert.assertEquals(5, taskExecutor.getPoolSize());
|
||||||
|
@ -62,16 +57,7 @@ public class ThreadPoolTaskExecutorUnitTest {
|
||||||
taskExecutor.afterPropertiesSet();
|
taskExecutor.afterPropertiesSet();
|
||||||
|
|
||||||
CountDownLatch countDownLatch = new CountDownLatch(10);
|
CountDownLatch countDownLatch = new CountDownLatch(10);
|
||||||
for (int i = 0; i < 10; i++) {
|
this.startThreads(taskExecutor, countDownLatch, 10);
|
||||||
taskExecutor.execute(() -> {
|
|
||||||
try {
|
|
||||||
Thread.sleep(100L * ThreadLocalRandom.current().nextLong(1, 10));
|
|
||||||
countDownLatch.countDown();
|
|
||||||
} catch (InterruptedException e) {
|
|
||||||
Thread.currentThread().interrupt();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
while (countDownLatch.getCount() > 0) {
|
while (countDownLatch.getCount() > 0) {
|
||||||
Assert.assertEquals(5, taskExecutor.getPoolSize());
|
Assert.assertEquals(5, taskExecutor.getPoolSize());
|
||||||
|
@ -87,16 +73,7 @@ public class ThreadPoolTaskExecutorUnitTest {
|
||||||
taskExecutor.afterPropertiesSet();
|
taskExecutor.afterPropertiesSet();
|
||||||
|
|
||||||
CountDownLatch countDownLatch = new CountDownLatch(10);
|
CountDownLatch countDownLatch = new CountDownLatch(10);
|
||||||
for (int i = 0; i < 10; i++) {
|
this.startThreads(taskExecutor, countDownLatch, 10);
|
||||||
taskExecutor.execute(() -> {
|
|
||||||
try {
|
|
||||||
Thread.sleep(100L * ThreadLocalRandom.current().nextLong(1, 10));
|
|
||||||
countDownLatch.countDown();
|
|
||||||
} catch (InterruptedException e) {
|
|
||||||
Thread.currentThread().interrupt();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
while (countDownLatch.getCount() > 0) {
|
while (countDownLatch.getCount() > 0) {
|
||||||
Assert.assertEquals(10, taskExecutor.getPoolSize());
|
Assert.assertEquals(10, taskExecutor.getPoolSize());
|
||||||
|
@ -111,17 +88,8 @@ public class ThreadPoolTaskExecutorUnitTest {
|
||||||
taskExecutor.setQueueCapacity(10);
|
taskExecutor.setQueueCapacity(10);
|
||||||
taskExecutor.afterPropertiesSet();
|
taskExecutor.afterPropertiesSet();
|
||||||
|
|
||||||
CountDownLatch countDownLatch = new CountDownLatch(10);
|
CountDownLatch countDownLatch = new CountDownLatch(20);
|
||||||
for (int i = 0; i < 20; i++) {
|
this.startThreads(taskExecutor, countDownLatch, 20);
|
||||||
taskExecutor.execute(() -> {
|
|
||||||
try {
|
|
||||||
Thread.sleep(100L * ThreadLocalRandom.current().nextLong(1, 10));
|
|
||||||
countDownLatch.countDown();
|
|
||||||
} catch (InterruptedException e) {
|
|
||||||
Thread.currentThread().interrupt();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
while (countDownLatch.getCount() > 0) {
|
while (countDownLatch.getCount() > 0) {
|
||||||
Assert.assertEquals(10, taskExecutor.getPoolSize());
|
Assert.assertEquals(10, taskExecutor.getPoolSize());
|
||||||
|
|
Loading…
Reference in New Issue