Skip to content

Commit 450a1cc

Browse files
authored
fix!: remove periodic schedule from timer event source (#653)
1 parent c335dc8 commit 450a1cc

File tree

3 files changed

+1
-62
lines changed

3 files changed

+1
-62
lines changed

Diff for: operator-framework-core/src/main/java/io/javaoperatorsdk/operator/processing/event/internal/TimerEventSource.java

-23
Original file line numberDiff line numberDiff line change
@@ -20,21 +20,7 @@ public class TimerEventSource<R extends CustomResource<?, ?>> extends AbstractEv
2020
private final Timer timer = new Timer();
2121
private final AtomicBoolean running = new AtomicBoolean();
2222
private final Map<CustomResourceID, EventProducerTimeTask> onceTasks = new ConcurrentHashMap<>();
23-
private final Map<CustomResourceID, EventProducerTimeTask> timerTasks = new ConcurrentHashMap<>();
2423

25-
public void schedule(R customResource, long delay, long period) {
26-
if (!running.get()) {
27-
throw new IllegalStateException("The TimerEventSource is not running");
28-
}
29-
30-
CustomResourceID resourceUid = CustomResourceID.fromResource(customResource);
31-
if (timerTasks.containsKey(resourceUid)) {
32-
return;
33-
}
34-
EventProducerTimeTask task = new EventProducerTimeTask(resourceUid);
35-
timerTasks.put(resourceUid, task);
36-
timer.schedule(task, delay, period);
37-
}
3824

3925
public void scheduleOnce(R customResource, long delay) {
4026
if (!running.get()) {
@@ -51,17 +37,9 @@ public void scheduleOnce(R customResource, long delay) {
5137

5238
@Override
5339
public void cleanupForCustomResource(CustomResourceID customResourceUid) {
54-
cancelSchedule(customResourceUid);
5540
cancelOnceSchedule(customResourceUid);
5641
}
5742

58-
public void cancelSchedule(CustomResourceID customResourceID) {
59-
TimerTask timerTask = timerTasks.remove(customResourceID);
60-
if (timerTask != null) {
61-
timerTask.cancel();
62-
}
63-
}
64-
6543
public void cancelOnceSchedule(CustomResourceID customResourceUid) {
6644
TimerTask timerTask = onceTasks.remove(customResourceUid);
6745
if (timerTask != null) {
@@ -78,7 +56,6 @@ public void start() {
7856
public void stop() {
7957
running.set(false);
8058
onceTasks.keySet().forEach(this::cancelOnceSchedule);
81-
timerTasks.keySet().forEach(this::cancelSchedule);
8259
timer.cancel();
8360
}
8461

Diff for: operator-framework-core/src/test/java/io/javaoperatorsdk/operator/processing/event/internal/TimerEventSourceTest.java

-30
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ class TimerEventSourceTest {
2424

2525
public static final int INITIAL_DELAY = 50;
2626
public static final int PERIOD = 50;
27-
public static final int TESTING_TIME_SLACK = 40;
2827

2928
private TimerEventSource<TestCustomResource> timerEventSource;
3029
private CapturingEventHandler eventHandlerMock;
@@ -38,35 +37,6 @@ public void setup() {
3837
timerEventSource.start();
3938
}
4039

41-
@Test
42-
public void producesEventsPeriodically() {
43-
TestCustomResource customResource = TestUtils.testCustomResource();
44-
timerEventSource.schedule(customResource, INITIAL_DELAY, PERIOD);
45-
46-
untilAsserted(() -> {
47-
assertThat(eventHandlerMock.events)
48-
.hasSizeGreaterThan(2);
49-
assertThat(eventHandlerMock.events)
50-
.allMatch(e -> e.getRelatedCustomResourceID()
51-
.equals(CustomResourceID.fromResource(customResource)));
52-
53-
});
54-
}
55-
56-
@Test
57-
public void deRegistersPeriodicalEventSources() {
58-
TestCustomResource customResource = TestUtils.testCustomResource();
59-
60-
timerEventSource.schedule(customResource, INITIAL_DELAY, PERIOD);
61-
untilAsserted(() -> assertThat(eventHandlerMock.events).hasSizeGreaterThan(1));
62-
63-
timerEventSource
64-
.cleanupForCustomResource(CustomResourceID.fromResource(customResource));
65-
66-
int size = eventHandlerMock.events.size();
67-
untilAsserted(() -> assertThat(eventHandlerMock.events).hasSize(size));
68-
}
69-
7040
@Test
7141
public void schedulesOnce() {
7242
TestCustomResource customResource = TestUtils.testCustomResource();

Diff for: operator-framework/src/test/java/io/javaoperatorsdk/operator/sample/event/EventSourceTestCustomResourceController.java

+1-9
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@
22

33
import java.util.concurrent.atomic.AtomicInteger;
44

5-
import org.slf4j.Logger;
6-
import org.slf4j.LoggerFactory;
7-
85
import io.fabric8.kubernetes.client.CustomResource;
96
import io.javaoperatorsdk.operator.ControllerUtils;
107
import io.javaoperatorsdk.operator.api.Context;
@@ -24,9 +21,6 @@ public class EventSourceTestCustomResourceController
2421
public static final String FINALIZER_NAME =
2522
ControllerUtils.getDefaultFinalizerName(
2623
CustomResource.getCRDName(EventSourceTestCustomResource.class));
27-
private static final Logger log =
28-
LoggerFactory.getLogger(EventSourceTestCustomResourceController.class);
29-
public static final int TIMER_DELAY = 300;
3024
public static final int TIMER_PERIOD = 500;
3125
private final AtomicInteger numberOfExecutions = new AtomicInteger(0);
3226
private final TimerEventSource<EventSourceTestCustomResource> timerEventSource =
@@ -41,13 +35,11 @@ public void prepareEventSources(EventSourceManager eventSourceManager) {
4135
public UpdateControl<EventSourceTestCustomResource> createOrUpdateResource(
4236
EventSourceTestCustomResource resource, Context context) {
4337

44-
timerEventSource.schedule(resource, TIMER_DELAY, TIMER_PERIOD);
45-
4638
numberOfExecutions.addAndGet(1);
4739
ensureStatusExists(resource);
4840
resource.getStatus().setState(EventSourceTestCustomResourceStatus.State.SUCCESS);
4941

50-
return UpdateControl.updateStatusSubResource(resource);
42+
return UpdateControl.updateStatusSubResource(resource).rescheduleAfter(TIMER_PERIOD);
5143
}
5244

5345
private void ensureStatusExists(EventSourceTestCustomResource resource) {

0 commit comments

Comments
 (0)