Skip to content

Commit ba0279b

Browse files
committed
Remove race condition in TaskSchedulingAutoConfigurationTests
Closes gh-16640
1 parent 6ae7274 commit ba0279b

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/task/TaskSchedulingAutoConfigurationTests.java

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2018 the original author or authors.
2+
* Copyright 2012-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,8 +18,10 @@
1818

1919
import java.util.Set;
2020
import java.util.concurrent.ConcurrentHashMap;
21+
import java.util.concurrent.CountDownLatch;
2122
import java.util.concurrent.Executors;
2223
import java.util.concurrent.ScheduledExecutorService;
24+
import java.util.concurrent.TimeUnit;
2325

2426
import org.junit.Test;
2527

@@ -63,7 +65,7 @@ public void enableSchedulingWithNoTaskExecutorAutoConfiguresOne() {
6365
.withUserConfiguration(SchedulingConfiguration.class).run((context) -> {
6466
assertThat(context).hasSingleBean(TaskExecutor.class);
6567
TestBean bean = context.getBean(TestBean.class);
66-
Thread.sleep(15);
68+
assertThat(bean.latch.await(30, TimeUnit.SECONDS)).isTrue();
6769
assertThat(bean.threadNames)
6870
.allMatch((name) -> name.contains("scheduling-test-"));
6971
});
@@ -79,7 +81,7 @@ public void enableSchedulingWithNoTaskExecutorAppliesCustomizers() {
7981
.run((context) -> {
8082
assertThat(context).hasSingleBean(TaskExecutor.class);
8183
TestBean bean = context.getBean(TestBean.class);
82-
Thread.sleep(15);
84+
assertThat(bean.latch.await(30, TimeUnit.SECONDS)).isTrue();
8385
assertThat(bean.threadNames)
8486
.allMatch((name) -> name.contains("customized-scheduler-"));
8587
});
@@ -93,7 +95,7 @@ public void enableSchedulingWithExistingTaskSchedulerBacksOff() {
9395
assertThat(context.getBean(TaskScheduler.class))
9496
.isInstanceOf(TestTaskScheduler.class);
9597
TestBean bean = context.getBean(TestBean.class);
96-
Thread.sleep(15);
98+
assertThat(bean.latch.await(30, TimeUnit.SECONDS)).isTrue();
9799
assertThat(bean.threadNames).containsExactly("test-1");
98100
});
99101
}
@@ -105,7 +107,7 @@ public void enableSchedulingWithExistingScheduledExecutorServiceBacksOff() {
105107
assertThat(context).doesNotHaveBean(TaskScheduler.class);
106108
assertThat(context).hasSingleBean(ScheduledExecutorService.class);
107109
TestBean bean = context.getBean(TestBean.class);
108-
Thread.sleep(15);
110+
assertThat(bean.latch.await(30, TimeUnit.SECONDS)).isTrue();
109111
assertThat(bean.threadNames)
110112
.allMatch((name) -> name.contains("pool-"));
111113
});
@@ -117,7 +119,7 @@ public void enableSchedulingWithConfigurerBacksOff() {
117119
SchedulingConfigurerConfiguration.class).run((context) -> {
118120
assertThat(context).doesNotHaveBean(TaskScheduler.class);
119121
TestBean bean = context.getBean(TestBean.class);
120-
Thread.sleep(15);
122+
assertThat(bean.latch.await(30, TimeUnit.SECONDS)).isTrue();
121123
assertThat(bean.threadNames).containsExactly("test-1");
122124
});
123125
}
@@ -185,9 +187,12 @@ static class TestBean {
185187

186188
private final Set<String> threadNames = ConcurrentHashMap.newKeySet();
187189

188-
@Scheduled(fixedRate = 10)
190+
private final CountDownLatch latch = new CountDownLatch(1);
191+
192+
@Scheduled(fixedRate = 60000)
189193
public void accumulate() {
190194
this.threadNames.add(Thread.currentThread().getName());
195+
this.latch.countDown();
191196
}
192197

193198
}

0 commit comments

Comments
 (0)