Polish and simplify EnableSchedulingTests

This commit is contained in:
Sam Brannen 2015-07-20 14:49:35 +02:00
parent c3e36ad960
commit 0153913ef4
1 changed files with 59 additions and 88 deletions

View File

@ -20,7 +20,9 @@ import java.util.Date;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
import org.junit.Before; import org.junit.Before;
import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
@ -46,6 +48,10 @@ import static org.junit.Assert.*;
*/ */
public class EnableSchedulingTests { public class EnableSchedulingTests {
@Rule
public final ExpectedException exception = ExpectedException.none();
@Before @Before
public void setUp() { public void setUp() {
Assume.group(TestGroup.PERFORMANCE); Assume.group(TestGroup.PERFORMANCE);
@ -54,9 +60,7 @@ public class EnableSchedulingTests {
@Test @Test
public void withFixedRateTask() throws InterruptedException { public void withFixedRateTask() throws InterruptedException {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(FixedRateTaskConfig.class);
ctx.register(FixedRateTaskConfig.class);
ctx.refresh();
Thread.sleep(100); Thread.sleep(100);
assertThat(ctx.getBean(AtomicInteger.class).get(), greaterThanOrEqualTo(10)); assertThat(ctx.getBean(AtomicInteger.class).get(), greaterThanOrEqualTo(10));
@ -64,7 +68,8 @@ public class EnableSchedulingTests {
} }
@EnableScheduling @Configuration @Configuration
@EnableScheduling
static class FixedRateTaskConfig { static class FixedRateTaskConfig {
@Bean @Bean
@ -81,9 +86,7 @@ public class EnableSchedulingTests {
@Test @Test
public void withSubclass() throws InterruptedException { public void withSubclass() throws InterruptedException {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(FixedRateTaskConfigSubclass.class);
ctx.register(FixedRateTaskConfigSubclass.class);
ctx.refresh();
Thread.sleep(100); Thread.sleep(100);
assertThat(ctx.getBean(AtomicInteger.class).get(), greaterThanOrEqualTo(10)); assertThat(ctx.getBean(AtomicInteger.class).get(), greaterThanOrEqualTo(10));
@ -98,9 +101,7 @@ public class EnableSchedulingTests {
@Test @Test
public void withExplicitScheduler() throws InterruptedException { public void withExplicitScheduler() throws InterruptedException {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(ExplicitSchedulerConfig.class);
ctx.register(ExplicitSchedulerConfig.class);
ctx.refresh();
Thread.sleep(100); Thread.sleep(100);
assertThat(ctx.getBean(AtomicInteger.class).get(), greaterThanOrEqualTo(10)); assertThat(ctx.getBean(AtomicInteger.class).get(), greaterThanOrEqualTo(10));
@ -109,7 +110,8 @@ public class EnableSchedulingTests {
} }
@EnableScheduling @Configuration @Configuration
@EnableScheduling
static class ExplicitSchedulerConfig { static class ExplicitSchedulerConfig {
String threadName; String threadName;
@ -134,28 +136,19 @@ public class EnableSchedulingTests {
} }
@Test(expected=IllegalStateException.class) @Test
@SuppressWarnings("resource")
public void withExplicitSchedulerAmbiguity_andSchedulingEnabled() { public void withExplicitSchedulerAmbiguity_andSchedulingEnabled() {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); exception.expect(IllegalStateException.class);
ctx.register(AmbiguousExplicitSchedulerConfig.class); exception.expectMessage(startsWith("More than one TaskScheduler bean exists within the context"));
try { new AnnotationConfigApplicationContext(AmbiguousExplicitSchedulerConfig.class);
ctx.refresh();
}
catch (IllegalStateException ex) {
assertThat(ex.getMessage(), startsWith("More than one TaskScheduler"));
throw ex;
}
finally {
ctx.close();
}
} }
@EnableScheduling @Configuration @Configuration
@EnableScheduling
static class AmbiguousExplicitSchedulerConfig { static class AmbiguousExplicitSchedulerConfig {
String threadName;
@Bean @Bean
public TaskScheduler taskScheduler1() { public TaskScheduler taskScheduler1() {
ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler(); ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
@ -170,24 +163,16 @@ public class EnableSchedulingTests {
return scheduler; return scheduler;
} }
@Bean
public AtomicInteger counter() {
return new AtomicInteger();
}
@Scheduled(fixedRate = 10) @Scheduled(fixedRate = 10)
public void task() { public void task() {
threadName = Thread.currentThread().getName();
counter().incrementAndGet();
} }
} }
@Test @Test
public void withExplicitScheduledTaskRegistrar() throws InterruptedException { public void withExplicitScheduledTaskRegistrar() throws InterruptedException {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(
ctx.register(ExplicitScheduledTaskRegistrarConfig.class); ExplicitScheduledTaskRegistrarConfig.class);
ctx.refresh();
Thread.sleep(100); Thread.sleep(100);
assertThat(ctx.getBean(AtomicInteger.class).get(), greaterThanOrEqualTo(10)); assertThat(ctx.getBean(AtomicInteger.class).get(), greaterThanOrEqualTo(10));
@ -196,7 +181,8 @@ public class EnableSchedulingTests {
} }
@EnableScheduling @Configuration @Configuration
@EnableScheduling
static class ExplicitScheduledTaskRegistrarConfig implements SchedulingConfigurer { static class ExplicitScheduledTaskRegistrarConfig implements SchedulingConfigurer {
String threadName; String threadName;
@ -234,15 +220,13 @@ public class EnableSchedulingTests {
public void configureTasks(ScheduledTaskRegistrar taskRegistrar) { public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
taskRegistrar.setScheduler(taskScheduler1()); taskRegistrar.setScheduler(taskScheduler1());
} }
} }
@Test @Test
public void withAmbiguousTaskSchedulers_butNoActualTasks() { public void withAmbiguousTaskSchedulers_butNoActualTasks() {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(
ctx.register(SchedulingEnabled_withAmbiguousTaskSchedulers_butNoActualTasks.class); SchedulingEnabled_withAmbiguousTaskSchedulers_butNoActualTasks.class);
ctx.refresh();
ctx.close(); ctx.close();
} }
@ -267,20 +251,12 @@ public class EnableSchedulingTests {
} }
@Test(expected=IllegalStateException.class) @Test
@SuppressWarnings("resource")
public void withAmbiguousTaskSchedulers_andSingleTask() { public void withAmbiguousTaskSchedulers_andSingleTask() {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); exception.expect(IllegalStateException.class);
ctx.register(SchedulingEnabled_withAmbiguousTaskSchedulers_andSingleTask.class); exception.expectMessage(startsWith("More than one TaskScheduler bean exists within the context"));
try { new AnnotationConfigApplicationContext(SchedulingEnabled_withAmbiguousTaskSchedulers_andSingleTask.class);
ctx.refresh();
}
catch (IllegalStateException ex) {
assertThat(ex.getMessage(), startsWith("More than one TaskScheduler"));
throw ex;
}
finally {
ctx.close();
}
} }
@ -310,13 +286,12 @@ public class EnableSchedulingTests {
@Test @Test
public void withAmbiguousTaskSchedulers_andSingleTask_disambiguatedByScheduledTaskRegistrarBean() throws InterruptedException { public void withAmbiguousTaskSchedulers_andSingleTask_disambiguatedByScheduledTaskRegistrarBean() throws InterruptedException {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(
ctx.register(SchedulingEnabled_withAmbiguousTaskSchedulers_andSingleTask_disambiguatedByScheduledTaskRegistrar.class); SchedulingEnabled_withAmbiguousTaskSchedulers_andSingleTask_disambiguatedByScheduledTaskRegistrar.class);
ctx.refresh();
Thread.sleep(20); Thread.sleep(20);
ThreadAwareWorker worker = ctx.getBean(ThreadAwareWorker.class); assertThat(ctx.getBean(ThreadAwareWorker.class).executedByThread, startsWith("explicitScheduler2-"));
ctx.close(); ctx.close();
assertThat(worker.executedByThread, startsWith("explicitScheduler2-"));
} }
@ -324,7 +299,6 @@ public class EnableSchedulingTests {
String executedByThread; String executedByThread;
} }
@Configuration @Configuration
@EnableScheduling @EnableScheduling
static class SchedulingEnabled_withAmbiguousTaskSchedulers_andSingleTask_disambiguatedByScheduledTaskRegistrar implements SchedulingConfigurer { static class SchedulingEnabled_withAmbiguousTaskSchedulers_andSingleTask_disambiguatedByScheduledTaskRegistrar implements SchedulingConfigurer {
@ -362,13 +336,12 @@ public class EnableSchedulingTests {
@Test @Test
public void withAmbiguousTaskSchedulers_andSingleTask_disambiguatedBySchedulerNameAttribute() throws InterruptedException { public void withAmbiguousTaskSchedulers_andSingleTask_disambiguatedBySchedulerNameAttribute() throws InterruptedException {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(
ctx.register(SchedulingEnabled_withAmbiguousTaskSchedulers_andSingleTask_disambiguatedBySchedulerNameAttribute.class); SchedulingEnabled_withAmbiguousTaskSchedulers_andSingleTask_disambiguatedBySchedulerNameAttribute.class);
ctx.refresh();
Thread.sleep(20); Thread.sleep(20);
ThreadAwareWorker worker = ctx.getBean(ThreadAwareWorker.class); assertThat(ctx.getBean(ThreadAwareWorker.class).executedByThread, startsWith("explicitScheduler2-"));
ctx.close(); ctx.close();
assertThat(worker.executedByThread, startsWith("explicitScheduler2-"));
} }
@ -409,13 +382,12 @@ public class EnableSchedulingTests {
@Test @Test
public void withTaskAddedVia_configureTasks() throws InterruptedException { public void withTaskAddedVia_configureTasks() throws InterruptedException {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(
ctx.register(SchedulingEnabled_withTaskAddedVia_configureTasks.class); SchedulingEnabled_withTaskAddedVia_configureTasks.class);
ctx.refresh();
Thread.sleep(20); Thread.sleep(20);
ThreadAwareWorker worker = ctx.getBean(ThreadAwareWorker.class); assertThat(ctx.getBean(ThreadAwareWorker.class).executedByThread, startsWith("taskScheduler-"));
ctx.close(); ctx.close();
assertThat(worker.executedByThread, startsWith("taskScheduler-"));
} }
@ -450,9 +422,7 @@ public class EnableSchedulingTests {
@Test @Test
public void withTriggerTask() throws InterruptedException { public void withTriggerTask() throws InterruptedException {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(TriggerTaskConfig.class);
ctx.register(TriggerTaskConfig.class);
ctx.refresh();
Thread.sleep(100); Thread.sleep(100);
assertThat(ctx.getBean(AtomicInteger.class).get(), greaterThan(1)); assertThat(ctx.getBean(AtomicInteger.class).get(), greaterThan(1));
@ -492,20 +462,21 @@ public class EnableSchedulingTests {
@Test @Test
public void withInitiallyDelayedFixedRateTask() throws InterruptedException { public void withInitiallyDelayedFixedRateTask() throws InterruptedException {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(
ctx.register(FixedRateTaskConfig_withInitialDelay.class); FixedRateTaskConfig_withInitialDelay.class);
ctx.refresh();
Thread.sleep(1950); Thread.sleep(1950);
AtomicInteger counter = ctx.getBean(AtomicInteger.class); AtomicInteger counter = ctx.getBean(AtomicInteger.class);
ctx.close(); ctx.close();
assertThat(counter.get(), greaterThan(0)); // the @Scheduled method was called // The @Scheduled method should have been called at least once but
assertThat(counter.get(), lessThanOrEqualTo(10)); // but not more than times the delay allows // not more times than the delay allows.
assertThat(counter.get(), both(greaterThan(0)).and(lessThanOrEqualTo(10)));
} }
@EnableScheduling @Configuration @Configuration
@EnableScheduling
static class FixedRateTaskConfig_withInitialDelay { static class FixedRateTaskConfig_withInitialDelay {
@Bean @Bean