diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/task/TaskSchedulingAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/task/TaskSchedulingAutoConfigurationTests.java index fa20e4b3767..9b20241635c 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/task/TaskSchedulingAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/task/TaskSchedulingAutoConfigurationTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2018 the original author or authors. + * Copyright 2012-2019 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -18,8 +18,10 @@ package org.springframework.boot.autoconfigure.task; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.CountDownLatch; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.TimeUnit; import org.junit.Test; @@ -63,7 +65,7 @@ public class TaskSchedulingAutoConfigurationTests { .withUserConfiguration(SchedulingConfiguration.class).run((context) -> { assertThat(context).hasSingleBean(TaskExecutor.class); TestBean bean = context.getBean(TestBean.class); - Thread.sleep(15); + assertThat(bean.latch.await(30, TimeUnit.SECONDS)).isTrue(); assertThat(bean.threadNames) .allMatch((name) -> name.contains("scheduling-test-")); }); @@ -79,7 +81,7 @@ public class TaskSchedulingAutoConfigurationTests { .run((context) -> { assertThat(context).hasSingleBean(TaskExecutor.class); TestBean bean = context.getBean(TestBean.class); - Thread.sleep(15); + assertThat(bean.latch.await(30, TimeUnit.SECONDS)).isTrue(); assertThat(bean.threadNames) .allMatch((name) -> name.contains("customized-scheduler-")); }); @@ -93,7 +95,7 @@ public class TaskSchedulingAutoConfigurationTests { assertThat(context.getBean(TaskScheduler.class)) .isInstanceOf(TestTaskScheduler.class); TestBean bean = context.getBean(TestBean.class); - Thread.sleep(15); + assertThat(bean.latch.await(30, TimeUnit.SECONDS)).isTrue(); assertThat(bean.threadNames).containsExactly("test-1"); }); } @@ -105,7 +107,7 @@ public class TaskSchedulingAutoConfigurationTests { assertThat(context).doesNotHaveBean(TaskScheduler.class); assertThat(context).hasSingleBean(ScheduledExecutorService.class); TestBean bean = context.getBean(TestBean.class); - Thread.sleep(15); + assertThat(bean.latch.await(30, TimeUnit.SECONDS)).isTrue(); assertThat(bean.threadNames) .allMatch((name) -> name.contains("pool-")); }); @@ -117,7 +119,7 @@ public class TaskSchedulingAutoConfigurationTests { SchedulingConfigurerConfiguration.class).run((context) -> { assertThat(context).doesNotHaveBean(TaskScheduler.class); TestBean bean = context.getBean(TestBean.class); - Thread.sleep(15); + assertThat(bean.latch.await(30, TimeUnit.SECONDS)).isTrue(); assertThat(bean.threadNames).containsExactly("test-1"); }); } @@ -185,9 +187,12 @@ public class TaskSchedulingAutoConfigurationTests { private final Set threadNames = ConcurrentHashMap.newKeySet(); - @Scheduled(fixedRate = 10) + private final CountDownLatch latch = new CountDownLatch(1); + + @Scheduled(fixedRate = 60000) public void accumulate() { this.threadNames.add(Thread.currentThread().getName()); + this.latch.countDown(); } }