Skip to content

Commit f443cf9

Browse files
committed
Introduce NoOpTaskScheduler for test setups
Closes gh-28073
1 parent c9292f8 commit f443cf9

File tree

1 file changed

+112
-0
lines changed

1 file changed

+112
-0
lines changed
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
/*
2+
* Copyright 2002-2023 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.scheduling.support;
18+
19+
import java.time.Duration;
20+
import java.time.Instant;
21+
import java.util.concurrent.CancellationException;
22+
import java.util.concurrent.Delayed;
23+
import java.util.concurrent.ScheduledFuture;
24+
import java.util.concurrent.TimeUnit;
25+
26+
import org.springframework.lang.Nullable;
27+
import org.springframework.scheduling.TaskScheduler;
28+
import org.springframework.scheduling.Trigger;
29+
30+
/**
31+
* A basic, no operation {@link TaskScheduler} implementation suitable
32+
* for disabling scheduling, typically used for test setups.
33+
*
34+
* <p>Will accept any scheduling request but never actually execute it.
35+
*
36+
* @author Juergen Hoeller
37+
* @since 6.1.3
38+
*/
39+
public class NoOpTaskScheduler implements TaskScheduler {
40+
41+
@Override
42+
@Nullable
43+
public ScheduledFuture<?> schedule(Runnable task, Trigger trigger) {
44+
Instant nextExecution = trigger.nextExecution(new SimpleTriggerContext(getClock()));
45+
return (nextExecution != null ? new NoOpScheduledFuture<>() : null);
46+
}
47+
48+
@Override
49+
public ScheduledFuture<?> schedule(Runnable task, Instant startTime) {
50+
return new NoOpScheduledFuture<>();
51+
}
52+
53+
@Override
54+
public ScheduledFuture<?> scheduleAtFixedRate(Runnable task, Instant startTime, Duration period) {
55+
return new NoOpScheduledFuture<>();
56+
}
57+
58+
@Override
59+
public ScheduledFuture<?> scheduleAtFixedRate(Runnable task, Duration period) {
60+
return new NoOpScheduledFuture<>();
61+
}
62+
63+
@Override
64+
public ScheduledFuture<?> scheduleWithFixedDelay(Runnable task, Instant startTime, Duration delay) {
65+
return new NoOpScheduledFuture<>();
66+
}
67+
68+
@Override
69+
public ScheduledFuture<?> scheduleWithFixedDelay(Runnable task, Duration delay) {
70+
return new NoOpScheduledFuture<>();
71+
}
72+
73+
74+
private static class NoOpScheduledFuture<V> implements ScheduledFuture<V> {
75+
76+
@Override
77+
public boolean cancel(boolean mayInterruptIfRunning) {
78+
return true;
79+
}
80+
81+
@Override
82+
public boolean isCancelled() {
83+
return true;
84+
}
85+
86+
@Override
87+
public boolean isDone() {
88+
return true;
89+
}
90+
91+
@Override
92+
public V get() {
93+
throw new CancellationException("No-op");
94+
}
95+
96+
@Override
97+
public V get(long timeout, TimeUnit unit) {
98+
throw new CancellationException("No-op");
99+
}
100+
101+
@Override
102+
public long getDelay(TimeUnit unit) {
103+
return 0;
104+
}
105+
106+
@Override
107+
public int compareTo(Delayed other) {
108+
return 0;
109+
}
110+
}
111+
112+
}

0 commit comments

Comments
 (0)