Skip to content

Commit b732811

Browse files
luukveenischemicL
andauthored
Call init() on delegate from TimedScheduler (#3876)
The `TimedScheduler` class doesn't currently provide an override for the `init()` method, which means it calls the default implementation on the interface that delegates to `start()`. Most existing schedulers have an implementation of `start()` and it's not a problem for them. However, the newer `BoundedElasticThreadPerTaskScheduler` throws an error for `start()`, so wrapping it in a `TimedScheduler` causes it to crash immediately when `init()` gets called. With this change, the wrapped scheduler's `init()` method is called instead and allows users to get metrics for the virtual thread schedulers. --------- Co-authored-by: Dariusz Jędrzejczyk <[email protected]>
1 parent 63d25e9 commit b732811

File tree

2 files changed

+59
-3
lines changed

2 files changed

+59
-3
lines changed

reactor-core-micrometer/src/main/java/reactor/core/observability/micrometer/TimedScheduler.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,11 @@ public void start() {
135135
delegate.start();
136136
}
137137

138+
@Override
139+
public void init() {
140+
delegate.init();
141+
}
142+
138143
static final class TimedWorker implements Worker {
139144

140145
final TimedScheduler parent;

reactor-core-micrometer/src/test/java/reactor/core/observability/micrometer/TimedSchedulerTest.java

Lines changed: 54 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,10 @@
1616

1717
package reactor.core.observability.micrometer;
1818

19-
import java.lang.reflect.Field;
2019
import java.time.Duration;
2120
import java.util.concurrent.ArrayBlockingQueue;
2221
import java.util.concurrent.CountDownLatch;
2322
import java.util.concurrent.ExecutorService;
24-
import java.util.concurrent.Executors;
2523
import java.util.concurrent.RejectedExecutionException;
2624
import java.util.concurrent.ThreadPoolExecutor;
2725
import java.util.concurrent.TimeUnit;
@@ -30,7 +28,6 @@
3028
import io.micrometer.core.instrument.LongTaskTimer;
3129
import io.micrometer.core.instrument.MockClock;
3230
import io.micrometer.core.instrument.Tags;
33-
import io.micrometer.core.instrument.internal.DefaultLongTaskTimer;
3431
import io.micrometer.core.instrument.search.RequiredSearch;
3532
import io.micrometer.core.instrument.simple.SimpleConfig;
3633
import io.micrometer.core.instrument.simple.SimpleMeterRegistry;
@@ -72,6 +69,60 @@ void closeRegistry() {
7269
registry.close();
7370
}
7471

72+
@Test
73+
void supportsBothDeprecatedAndNonRestartableSchedulers() {
74+
Scheduler deprecatedScheduler = new Scheduler() {
75+
76+
@Override
77+
public Disposable schedule(Runnable task) {
78+
return Disposables.disposed();
79+
}
80+
81+
@Override
82+
public Worker createWorker() {
83+
throw new UnsupportedOperationException();
84+
}
85+
};
86+
87+
Scheduler nonRestartableScheduler = new Scheduler() {
88+
@Override
89+
public Disposable schedule(Runnable task) {
90+
return Disposables.disposed();
91+
}
92+
93+
@Override
94+
public Worker createWorker() {
95+
throw new UnsupportedOperationException();
96+
}
97+
98+
@SuppressWarnings("deprecation")
99+
@Override
100+
public void start() {
101+
throw new UnsupportedOperationException();
102+
}
103+
104+
@Override
105+
public void init() {
106+
}
107+
};
108+
109+
TimedScheduler timedDeprecatedScheduler =
110+
new TimedScheduler(deprecatedScheduler, registry, "test", Tags.empty());
111+
112+
TimedScheduler timedNonRestartableScheduler =
113+
new TimedScheduler(nonRestartableScheduler, registry, "test", Tags.empty());
114+
115+
assertThatNoException().isThrownBy(() -> {
116+
timedDeprecatedScheduler.init();
117+
timedDeprecatedScheduler.start();
118+
});
119+
120+
assertThatNoException().isThrownBy(timedNonRestartableScheduler::init);
121+
122+
assertThatExceptionOfType(UnsupportedOperationException.class)
123+
.isThrownBy(timedNonRestartableScheduler::start);
124+
}
125+
75126
@Test
76127
void aDotIsAddedToPrefix() {
77128
TimedScheduler test = new TimedScheduler(Schedulers.immediate(), registry, "noDot", Tags.empty());

0 commit comments

Comments
 (0)