Skip to content

Commit ff28baa

Browse files
lburgazzolimetacosm
authored andcommitted
chore: generify TimerEventSource to reduce the noise about raw use of parameters
1 parent 646f396 commit ff28baa

File tree

4 files changed

+18
-17
lines changed

4 files changed

+18
-17
lines changed

operator-framework-core/src/main/java/io/javaoperatorsdk/operator/processing/event/DefaultEventSourceManager.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,13 @@ public class DefaultEventSourceManager<R extends CustomResource<?, ?>>
3434
private final ReentrantLock lock = new ReentrantLock();
3535
private final Map<String, EventSource> eventSources = new ConcurrentHashMap<>();
3636
private final DefaultEventHandler<R> defaultEventHandler;
37-
private TimerEventSource retryTimerEventSource;
37+
private TimerEventSource<R> retryTimerEventSource;
3838

3939
DefaultEventSourceManager(DefaultEventHandler<R> defaultEventHandler, boolean supportRetry) {
4040
this.defaultEventHandler = defaultEventHandler;
4141
defaultEventHandler.setEventSourceManager(this);
4242
if (supportRetry) {
43-
this.retryTimerEventSource = new TimerEventSource();
43+
this.retryTimerEventSource = new TimerEventSource<>();
4444
registerEventSource(RETRY_TIMER_EVENT_SOURCE_NAME, retryTimerEventSource);
4545
}
4646
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@
1212
import org.slf4j.Logger;
1313
import org.slf4j.LoggerFactory;
1414

15-
public class TimerEventSource extends AbstractEventSource {
15+
public class TimerEventSource<R extends CustomResource<?, ?>> extends AbstractEventSource {
16+
private static final Logger log = LoggerFactory.getLogger(TimerEventSource.class);
1617

1718
private final Timer timer = new Timer();
1819
private final AtomicBoolean running = new AtomicBoolean();
1920
private final Map<String, EventProducerTimeTask> onceTasks = new ConcurrentHashMap<>();
2021
private final Map<String, EventProducerTimeTask> timerTasks = new ConcurrentHashMap<>();
21-
private Logger log = LoggerFactory.getLogger(TimerEventSource.class);
2222

23-
public void schedule(CustomResource customResource, long delay, long period) {
23+
public void schedule(R customResource, long delay, long period) {
2424
if (!running.get()) {
2525
throw new IllegalStateException("The TimerEventSource is not running");
2626
}
@@ -34,7 +34,7 @@ public void schedule(CustomResource customResource, long delay, long period) {
3434
timer.schedule(task, delay, period);
3535
}
3636

37-
public void scheduleOnce(CustomResource customResource, long delay) {
37+
public void scheduleOnce(R customResource, long delay) {
3838
if (!running.get()) {
3939
throw new IllegalStateException("The TimerEventSource is not running");
4040
}

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44
import static org.assertj.core.api.Assertions.assertThat;
55
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
66

7-
import io.fabric8.kubernetes.client.CustomResource;
87
import io.javaoperatorsdk.operator.TestUtils;
98
import io.javaoperatorsdk.operator.processing.KubernetesResourceUtils;
109
import io.javaoperatorsdk.operator.processing.event.Event;
1110
import io.javaoperatorsdk.operator.processing.event.EventHandler;
11+
import io.javaoperatorsdk.operator.sample.simple.TestCustomResource;
1212
import java.io.IOException;
1313
import java.util.List;
1414
import java.util.concurrent.CopyOnWriteArrayList;
@@ -25,21 +25,21 @@ class TimerEventSourceTest {
2525
public static final int PERIOD = 50;
2626
public static final int TESTING_TIME_SLACK = 40;
2727

28-
private TimerEventSource timerEventSource;
28+
private TimerEventSource<TestCustomResource> timerEventSource;
2929
private CapturingEventHandler eventHandlerMock;
3030

3131
@BeforeEach
3232
public void setup() {
3333
eventHandlerMock = new CapturingEventHandler();
3434

35-
timerEventSource = new TimerEventSource();
35+
timerEventSource = new TimerEventSource<>();
3636
timerEventSource.setEventHandler(eventHandlerMock);
3737
timerEventSource.start();
3838
}
3939

4040
@Test
4141
public void producesEventsPeriodically() {
42-
CustomResource customResource = TestUtils.testCustomResource();
42+
TestCustomResource customResource = TestUtils.testCustomResource();
4343
timerEventSource.schedule(customResource, INITIAL_DELAY, PERIOD);
4444

4545
untilAsserted(() -> {
@@ -54,7 +54,7 @@ public void producesEventsPeriodically() {
5454

5555
@Test
5656
public void deRegistersPeriodicalEventSources() {
57-
CustomResource customResource = TestUtils.testCustomResource();
57+
TestCustomResource customResource = TestUtils.testCustomResource();
5858

5959
timerEventSource.schedule(customResource, INITIAL_DELAY, PERIOD);
6060
untilAsserted(() -> assertThat(eventHandlerMock.events).hasSizeGreaterThan(1));
@@ -67,7 +67,7 @@ public void deRegistersPeriodicalEventSources() {
6767

6868
@Test
6969
public void schedulesOnce() {
70-
CustomResource customResource = TestUtils.testCustomResource();
70+
TestCustomResource customResource = TestUtils.testCustomResource();
7171

7272
timerEventSource.scheduleOnce(customResource, PERIOD);
7373

@@ -77,7 +77,7 @@ public void schedulesOnce() {
7777

7878
@Test
7979
public void canCancelOnce() {
80-
CustomResource customResource = TestUtils.testCustomResource();
80+
TestCustomResource customResource = TestUtils.testCustomResource();
8181

8282
timerEventSource.scheduleOnce(customResource, PERIOD);
8383
timerEventSource.cancelOnceSchedule(KubernetesResourceUtils.getUID(customResource));
@@ -87,7 +87,7 @@ public void canCancelOnce() {
8787

8888
@Test
8989
public void canRescheduleOnceEvent() {
90-
CustomResource customResource = TestUtils.testCustomResource();
90+
TestCustomResource customResource = TestUtils.testCustomResource();
9191

9292
timerEventSource.scheduleOnce(customResource, PERIOD);
9393
timerEventSource.scheduleOnce(customResource, 2 * PERIOD);
@@ -97,7 +97,7 @@ public void canRescheduleOnceEvent() {
9797

9898
@Test
9999
public void deRegistersOnceEventSources() {
100-
CustomResource customResource = TestUtils.testCustomResource();
100+
TestCustomResource customResource = TestUtils.testCustomResource();
101101

102102
timerEventSource.scheduleOnce(customResource, PERIOD);
103103
timerEventSource.eventSourceDeRegisteredForResource(getUID(customResource));
@@ -107,7 +107,7 @@ public void deRegistersOnceEventSources() {
107107

108108
@Test
109109
public void eventNotRegisteredIfStopped() throws IOException {
110-
CustomResource customResource = TestUtils.testCustomResource();
110+
TestCustomResource customResource = TestUtils.testCustomResource();
111111

112112
timerEventSource.close();
113113
assertThatExceptionOfType(IllegalStateException.class).isThrownBy(

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ public class EventSourceTestCustomResourceController
2525
public static final int TIMER_DELAY = 300;
2626
public static final int TIMER_PERIOD = 500;
2727
private final AtomicInteger numberOfExecutions = new AtomicInteger(0);
28-
private final TimerEventSource timerEventSource = new TimerEventSource();
28+
private final TimerEventSource<EventSourceTestCustomResource> timerEventSource =
29+
new TimerEventSource<>();
2930

3031
@Override
3132
public void init(EventSourceManager eventSourceManager) {

0 commit comments

Comments
 (0)