Skip to content

Commit

Permalink
Remove race condition in TaskSchedulingAutoConfigurationTests
Browse files Browse the repository at this point in the history
  • Loading branch information
wilkinsona committed Apr 24, 2019
1 parent 6ae7274 commit ba0279b
Showing 1 changed file with 12 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -18,8 +18,10 @@

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;

Expand Down Expand Up @@ -63,7 +65,7 @@ public void enableSchedulingWithNoTaskExecutorAutoConfiguresOne() {
.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-"));
});
Expand All @@ -79,7 +81,7 @@ public void enableSchedulingWithNoTaskExecutorAppliesCustomizers() {
.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-"));
});
Expand All @@ -93,7 +95,7 @@ public void enableSchedulingWithExistingTaskSchedulerBacksOff() {
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");
});
}
Expand All @@ -105,7 +107,7 @@ public void enableSchedulingWithExistingScheduledExecutorServiceBacksOff() {
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-"));
});
Expand All @@ -117,7 +119,7 @@ public void enableSchedulingWithConfigurerBacksOff() {
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");
});
}
Expand Down Expand Up @@ -185,9 +187,12 @@ static class TestBean {

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

}
Expand Down

0 comments on commit ba0279b

Please sign in to comment.