Remove race condition in TaskSchedulingAutoConfigurationTests

Closes gh-16640
This commit is contained in:
Andy Wilkinson 2019-04-24 11:55:41 +01:00
parent 6ae7274289
commit ba0279be14
1 changed files with 12 additions and 7 deletions

View File

@ -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<String> 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();
}
}