Skip to content

Commit a1a06cd

Browse files
committed
@EnableScheduling tests do not expect exception in case of scheduler ambiguity anymore
Issue: SPR-14030
1 parent 5045579 commit a1a06cd

File tree

1 file changed

+115
-140
lines changed

1 file changed

+115
-140
lines changed

spring-context/src/test/java/org/springframework/scheduling/annotation/EnableSchedulingTests.java

Lines changed: 115 additions & 140 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
import java.util.Date;
2020
import java.util.concurrent.atomic.AtomicInteger;
2121

22-
import org.junit.Ignore;
22+
import org.junit.After;
2323
import org.junit.Rule;
2424
import org.junit.Test;
2525
import org.junit.rules.ExpectedException;
@@ -36,12 +36,8 @@
3636
import org.springframework.tests.Assume;
3737
import org.springframework.tests.TestGroup;
3838

39-
import static org.hamcrest.Matchers.both;
40-
import static org.hamcrest.Matchers.greaterThan;
41-
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
42-
import static org.hamcrest.Matchers.lessThanOrEqualTo;
43-
import static org.hamcrest.Matchers.startsWith;
44-
import static org.junit.Assert.assertThat;
39+
import static org.hamcrest.Matchers.*;
40+
import static org.junit.Assert.*;
4541

4642
/**
4743
* Tests use of @EnableScheduling on @Configuration classes.
@@ -55,62 +51,151 @@ public class EnableSchedulingTests {
5551
@Rule
5652
public final ExpectedException exception = ExpectedException.none();
5753

54+
private AnnotationConfigApplicationContext ctx;
55+
56+
57+
@After
58+
public void tearDown() {
59+
if (ctx != null) {
60+
ctx.close();
61+
}
62+
}
63+
5864

5965
@Test
6066
public void withFixedRateTask() throws InterruptedException {
6167
Assume.group(TestGroup.PERFORMANCE);
6268

63-
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(FixedRateTaskConfig.class);
69+
ctx = new AnnotationConfigApplicationContext(FixedRateTaskConfig.class);
6470

6571
Thread.sleep(100);
6672
assertThat(ctx.getBean(AtomicInteger.class).get(), greaterThanOrEqualTo(10));
67-
ctx.close();
6873
}
6974

75+
@Test
76+
public void withSubclass() throws InterruptedException {
77+
Assume.group(TestGroup.PERFORMANCE);
7078

71-
@Configuration
72-
@EnableScheduling
73-
static class FixedRateTaskConfig {
79+
ctx = new AnnotationConfigApplicationContext(FixedRateTaskConfigSubclass.class);
7480

75-
@Bean
76-
public AtomicInteger counter() {
77-
return new AtomicInteger();
78-
}
81+
Thread.sleep(100);
82+
assertThat(ctx.getBean(AtomicInteger.class).get(), greaterThanOrEqualTo(10));
83+
}
7984

80-
@Scheduled(fixedRate = 10)
81-
public void task() {
82-
counter().incrementAndGet();
83-
}
85+
@Test
86+
public void withExplicitScheduler() throws InterruptedException {
87+
Assume.group(TestGroup.PERFORMANCE);
88+
89+
ctx = new AnnotationConfigApplicationContext(ExplicitSchedulerConfig.class);
90+
91+
Thread.sleep(100);
92+
assertThat(ctx.getBean(AtomicInteger.class).get(), greaterThanOrEqualTo(10));
93+
assertThat(ctx.getBean(ExplicitSchedulerConfig.class).threadName, startsWith("explicitScheduler-"));
8494
}
8595

96+
@Test
97+
public void withExplicitSchedulerAmbiguity_andSchedulingEnabled() {
98+
// No exception raised as of 4.3, aligned with the behavior for @Async methods (SPR-14030)
99+
ctx = new AnnotationConfigApplicationContext(AmbiguousExplicitSchedulerConfig.class);
100+
}
86101

87102
@Test
88-
public void withSubclass() throws InterruptedException {
103+
public void withExplicitScheduledTaskRegistrar() throws InterruptedException {
89104
Assume.group(TestGroup.PERFORMANCE);
90105

91-
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(FixedRateTaskConfigSubclass.class);
106+
ctx = new AnnotationConfigApplicationContext(ExplicitScheduledTaskRegistrarConfig.class);
92107

93108
Thread.sleep(100);
94109
assertThat(ctx.getBean(AtomicInteger.class).get(), greaterThanOrEqualTo(10));
95-
ctx.close();
110+
assertThat(ctx.getBean(ExplicitScheduledTaskRegistrarConfig.class).threadName, startsWith("explicitScheduler1"));
96111
}
97112

113+
@Test
114+
public void withAmbiguousTaskSchedulers_butNoActualTasks() {
115+
ctx = new AnnotationConfigApplicationContext(SchedulingEnabled_withAmbiguousTaskSchedulers_butNoActualTasks.class);
116+
}
98117

99-
@Configuration
100-
static class FixedRateTaskConfigSubclass extends FixedRateTaskConfig {
118+
@Test
119+
public void withAmbiguousTaskSchedulers_andSingleTask() {
120+
// No exception raised as of 4.3, aligned with the behavior for @Async methods (SPR-14030)
121+
ctx = new AnnotationConfigApplicationContext(SchedulingEnabled_withAmbiguousTaskSchedulers_andSingleTask.class);
101122
}
102123

124+
@Test
125+
public void withAmbiguousTaskSchedulers_andSingleTask_disambiguatedByScheduledTaskRegistrarBean() throws InterruptedException {
126+
Assume.group(TestGroup.PERFORMANCE);
127+
128+
ctx = new AnnotationConfigApplicationContext(
129+
SchedulingEnabled_withAmbiguousTaskSchedulers_andSingleTask_disambiguatedByScheduledTaskRegistrar.class);
130+
131+
Thread.sleep(100);
132+
assertThat(ctx.getBean(ThreadAwareWorker.class).executedByThread, startsWith("explicitScheduler2-"));
133+
}
103134

104135
@Test
105-
public void withExplicitScheduler() throws InterruptedException {
136+
public void withAmbiguousTaskSchedulers_andSingleTask_disambiguatedBySchedulerNameAttribute() throws InterruptedException {
106137
Assume.group(TestGroup.PERFORMANCE);
107138

108-
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(ExplicitSchedulerConfig.class);
139+
ctx = new AnnotationConfigApplicationContext(
140+
SchedulingEnabled_withAmbiguousTaskSchedulers_andSingleTask_disambiguatedBySchedulerNameAttribute.class);
109141

110142
Thread.sleep(100);
111-
assertThat(ctx.getBean(AtomicInteger.class).get(), greaterThanOrEqualTo(10));
112-
assertThat(ctx.getBean(ExplicitSchedulerConfig.class).threadName, startsWith("explicitScheduler-"));
113-
ctx.close();
143+
assertThat(ctx.getBean(ThreadAwareWorker.class).executedByThread, startsWith("explicitScheduler2-"));
144+
}
145+
146+
@Test
147+
public void withTaskAddedVia_configureTasks() throws InterruptedException {
148+
Assume.group(TestGroup.PERFORMANCE);
149+
150+
ctx = new AnnotationConfigApplicationContext(SchedulingEnabled_withTaskAddedVia_configureTasks.class);
151+
152+
Thread.sleep(100);
153+
assertThat(ctx.getBean(ThreadAwareWorker.class).executedByThread, startsWith("taskScheduler-"));
154+
}
155+
156+
@Test
157+
public void withTriggerTask() throws InterruptedException {
158+
Assume.group(TestGroup.PERFORMANCE);
159+
160+
ctx = new AnnotationConfigApplicationContext(TriggerTaskConfig.class);
161+
162+
Thread.sleep(100);
163+
assertThat(ctx.getBean(AtomicInteger.class).get(), greaterThan(1));
164+
}
165+
166+
@Test
167+
public void withInitiallyDelayedFixedRateTask() throws InterruptedException {
168+
Assume.group(TestGroup.PERFORMANCE);
169+
170+
ctx = new AnnotationConfigApplicationContext(FixedRateTaskConfig_withInitialDelay.class);
171+
172+
Thread.sleep(1950);
173+
AtomicInteger counter = ctx.getBean(AtomicInteger.class);
174+
175+
// The @Scheduled method should have been called at least once but
176+
// not more times than the delay allows.
177+
assertThat(counter.get(), both(greaterThan(0)).and(lessThanOrEqualTo(10)));
178+
}
179+
180+
181+
@Configuration
182+
@EnableScheduling
183+
static class FixedRateTaskConfig {
184+
185+
@Bean
186+
public AtomicInteger counter() {
187+
return new AtomicInteger();
188+
}
189+
190+
@Scheduled(fixedRate = 10)
191+
public void task() {
192+
counter().incrementAndGet();
193+
}
194+
}
195+
196+
197+
@Configuration
198+
static class FixedRateTaskConfigSubclass extends FixedRateTaskConfig {
114199
}
115200

116201

@@ -140,16 +225,6 @@ public void task() {
140225
}
141226

142227

143-
@Ignore("Disabled until SPR-14030 is resolved")
144-
@Test
145-
@SuppressWarnings("resource")
146-
public void withExplicitSchedulerAmbiguity_andSchedulingEnabled() {
147-
exception.expect(IllegalStateException.class);
148-
exception.expectMessage(startsWith("More than one TaskScheduler bean exists within the context"));
149-
new AnnotationConfigApplicationContext(AmbiguousExplicitSchedulerConfig.class);
150-
}
151-
152-
153228
@Configuration
154229
@EnableScheduling
155230
static class AmbiguousExplicitSchedulerConfig {
@@ -174,20 +249,6 @@ public void task() {
174249
}
175250

176251

177-
@Test
178-
public void withExplicitScheduledTaskRegistrar() throws InterruptedException {
179-
Assume.group(TestGroup.PERFORMANCE);
180-
181-
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(
182-
ExplicitScheduledTaskRegistrarConfig.class);
183-
184-
Thread.sleep(100);
185-
assertThat(ctx.getBean(AtomicInteger.class).get(), greaterThanOrEqualTo(10));
186-
assertThat(ctx.getBean(ExplicitScheduledTaskRegistrarConfig.class).threadName, startsWith("explicitScheduler1"));
187-
ctx.close();
188-
}
189-
190-
191252
@Configuration
192253
@EnableScheduling
193254
static class ExplicitScheduledTaskRegistrarConfig implements SchedulingConfigurer {
@@ -230,14 +291,6 @@ public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
230291
}
231292

232293

233-
@Test
234-
public void withAmbiguousTaskSchedulers_butNoActualTasks() {
235-
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(
236-
SchedulingEnabled_withAmbiguousTaskSchedulers_butNoActualTasks.class);
237-
ctx.close();
238-
}
239-
240-
241294
@Configuration
242295
@EnableScheduling
243296
static class SchedulingEnabled_withAmbiguousTaskSchedulers_butNoActualTasks {
@@ -258,16 +311,6 @@ public TaskScheduler taskScheduler2() {
258311
}
259312

260313

261-
@Ignore("Disabled until SPR-14030 is resolved")
262-
@Test
263-
@SuppressWarnings("resource")
264-
public void withAmbiguousTaskSchedulers_andSingleTask() {
265-
exception.expect(IllegalStateException.class);
266-
exception.expectMessage(startsWith("More than one TaskScheduler bean exists within the context"));
267-
new AnnotationConfigApplicationContext(SchedulingEnabled_withAmbiguousTaskSchedulers_andSingleTask.class);
268-
}
269-
270-
271314
@Configuration
272315
@EnableScheduling
273316
static class SchedulingEnabled_withAmbiguousTaskSchedulers_andSingleTask {
@@ -292,19 +335,6 @@ public TaskScheduler taskScheduler2() {
292335
}
293336

294337

295-
@Test
296-
public void withAmbiguousTaskSchedulers_andSingleTask_disambiguatedByScheduledTaskRegistrarBean() throws InterruptedException {
297-
Assume.group(TestGroup.PERFORMANCE);
298-
299-
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(
300-
SchedulingEnabled_withAmbiguousTaskSchedulers_andSingleTask_disambiguatedByScheduledTaskRegistrar.class);
301-
302-
Thread.sleep(20);
303-
assertThat(ctx.getBean(ThreadAwareWorker.class).executedByThread, startsWith("explicitScheduler2-"));
304-
ctx.close();
305-
}
306-
307-
308338
static class ThreadAwareWorker {
309339

310340
String executedByThread;
@@ -346,19 +376,6 @@ public TaskScheduler taskScheduler2() {
346376
}
347377

348378

349-
@Test
350-
public void withAmbiguousTaskSchedulers_andSingleTask_disambiguatedBySchedulerNameAttribute() throws InterruptedException {
351-
Assume.group(TestGroup.PERFORMANCE);
352-
353-
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(
354-
SchedulingEnabled_withAmbiguousTaskSchedulers_andSingleTask_disambiguatedBySchedulerNameAttribute.class);
355-
356-
Thread.sleep(20);
357-
assertThat(ctx.getBean(ThreadAwareWorker.class).executedByThread, startsWith("explicitScheduler2-"));
358-
ctx.close();
359-
}
360-
361-
362379
@Configuration
363380
@EnableScheduling
364381
static class SchedulingEnabled_withAmbiguousTaskSchedulers_andSingleTask_disambiguatedBySchedulerNameAttribute implements SchedulingConfigurer {
@@ -394,19 +411,6 @@ public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
394411
}
395412

396413

397-
@Test
398-
public void withTaskAddedVia_configureTasks() throws InterruptedException {
399-
Assume.group(TestGroup.PERFORMANCE);
400-
401-
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(
402-
SchedulingEnabled_withTaskAddedVia_configureTasks.class);
403-
404-
Thread.sleep(20);
405-
assertThat(ctx.getBean(ThreadAwareWorker.class).executedByThread, startsWith("taskScheduler-"));
406-
ctx.close();
407-
}
408-
409-
410414
@Configuration
411415
@EnableScheduling
412416
static class SchedulingEnabled_withTaskAddedVia_configureTasks implements SchedulingConfigurer {
@@ -436,18 +440,6 @@ public void run() {
436440
}
437441

438442

439-
@Test
440-
public void withTriggerTask() throws InterruptedException {
441-
Assume.group(TestGroup.PERFORMANCE);
442-
443-
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(TriggerTaskConfig.class);
444-
445-
Thread.sleep(100);
446-
assertThat(ctx.getBean(AtomicInteger.class).get(), greaterThan(1));
447-
ctx.close();
448-
}
449-
450-
451443
@Configuration
452444
static class TriggerTaskConfig {
453445

@@ -478,23 +470,6 @@ public Date nextExecutionTime(TriggerContext triggerContext) {
478470
}
479471

480472

481-
@Test
482-
public void withInitiallyDelayedFixedRateTask() throws InterruptedException {
483-
Assume.group(TestGroup.PERFORMANCE);
484-
485-
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(
486-
FixedRateTaskConfig_withInitialDelay.class);
487-
488-
Thread.sleep(1950);
489-
AtomicInteger counter = ctx.getBean(AtomicInteger.class);
490-
ctx.close();
491-
492-
// The @Scheduled method should have been called at least once but
493-
// not more times than the delay allows.
494-
assertThat(counter.get(), both(greaterThan(0)).and(lessThanOrEqualTo(10)));
495-
}
496-
497-
498473
@Configuration
499474
@EnableScheduling
500475
static class FixedRateTaskConfig_withInitialDelay {

0 commit comments

Comments
 (0)