1818
1919import java .util .Set ;
2020import java .util .concurrent .ConcurrentHashMap ;
21+ import java .util .concurrent .CountDownLatch ;
2122import java .util .concurrent .Executors ;
2223import java .util .concurrent .ScheduledExecutorService ;
24+ import java .util .concurrent .TimeUnit ;
2325
2426import org .junit .Test ;
2527
@@ -66,7 +68,7 @@ public void enableSchedulingWithNoTaskExecutorAutoConfiguresOne() {
6668 assertThat (context ).hasSingleBean (TaskExecutor .class );
6769 TaskExecutor taskExecutor = context .getBean (TaskExecutor .class );
6870 TestBean bean = context .getBean (TestBean .class );
69- Thread . sleep ( 15 );
71+ assertThat ( bean . latch . await ( 30 , TimeUnit . SECONDS )). isTrue ( );
7072 assertThat (taskExecutor ).hasFieldOrPropertyWithValue (
7173 "waitForTasksToCompleteOnShutdown" , true );
7274 assertThat (taskExecutor )
@@ -86,7 +88,7 @@ public void enableSchedulingWithNoTaskExecutorAppliesCustomizers() {
8688 .run ((context ) -> {
8789 assertThat (context ).hasSingleBean (TaskExecutor .class );
8890 TestBean bean = context .getBean (TestBean .class );
89- Thread . sleep ( 15 );
91+ assertThat ( bean . latch . await ( 30 , TimeUnit . SECONDS )). isTrue ( );
9092 assertThat (bean .threadNames )
9193 .allMatch ((name ) -> name .contains ("customized-scheduler-" ));
9294 });
@@ -100,7 +102,7 @@ public void enableSchedulingWithExistingTaskSchedulerBacksOff() {
100102 assertThat (context .getBean (TaskScheduler .class ))
101103 .isInstanceOf (TestTaskScheduler .class );
102104 TestBean bean = context .getBean (TestBean .class );
103- Thread . sleep ( 15 );
105+ assertThat ( bean . latch . await ( 30 , TimeUnit . SECONDS )). isTrue ( );
104106 assertThat (bean .threadNames ).containsExactly ("test-1" );
105107 });
106108 }
@@ -112,7 +114,7 @@ public void enableSchedulingWithExistingScheduledExecutorServiceBacksOff() {
112114 assertThat (context ).doesNotHaveBean (TaskScheduler .class );
113115 assertThat (context ).hasSingleBean (ScheduledExecutorService .class );
114116 TestBean bean = context .getBean (TestBean .class );
115- Thread . sleep ( 15 );
117+ assertThat ( bean . latch . await ( 30 , TimeUnit . SECONDS )). isTrue ( );
116118 assertThat (bean .threadNames )
117119 .allMatch ((name ) -> name .contains ("pool-" ));
118120 });
@@ -124,7 +126,7 @@ public void enableSchedulingWithConfigurerBacksOff() {
124126 SchedulingConfigurerConfiguration .class ).run ((context ) -> {
125127 assertThat (context ).doesNotHaveBean (TaskScheduler .class );
126128 TestBean bean = context .getBean (TestBean .class );
127- Thread . sleep ( 15 );
129+ assertThat ( bean . latch . await ( 30 , TimeUnit . SECONDS )). isTrue ( );
128130 assertThat (bean .threadNames ).containsExactly ("test-1" );
129131 });
130132 }
@@ -192,9 +194,12 @@ static class TestBean {
192194
193195 private final Set <String > threadNames = ConcurrentHashMap .newKeySet ();
194196
195- @ Scheduled (fixedRate = 10 )
197+ private final CountDownLatch latch = new CountDownLatch (1 );
198+
199+ @ Scheduled (fixedRate = 60000 )
196200 public void accumulate () {
197201 this .threadNames .add (Thread .currentThread ().getName ());
202+ this .latch .countDown ();
198203 }
199204
200205 }
0 commit comments