Skip to content

Commit 8103a2e

Browse files
committed
timer event source tests, improvements
1 parent 1ce3316 commit 8103a2e

File tree

3 files changed

+90
-3
lines changed

3 files changed

+90
-3
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public interface EventSourceManager {
1414

1515
Optional<EventSource> deRegisterEventSource(String customResourceUid, String name);
1616

17-
Map<String, EventSource> getRegisteredEventSources(String customResource);
17+
Map<String, EventSource> getRegisteredEventSources(String customResourceUid);
1818

1919

2020
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@
99
import java.util.*;
1010
import java.util.concurrent.ConcurrentHashMap;
1111
import java.util.concurrent.locks.ReentrantLock;
12-
// todo tests
12+
1313
public class TimerEventSource extends AbstractEventSource {
1414

1515
private Logger log = LoggerFactory.getLogger(TimerEventSource.class);
1616

1717
private final Timer timer = new Timer();
1818
private final ReentrantLock lock = new ReentrantLock();
1919

20-
private Map<String, List<EvenProducerTimeTask>> timerTasks = new ConcurrentHashMap<>();
20+
private final Map<String, List<EvenProducerTimeTask>> timerTasks = new ConcurrentHashMap<>();
2121

2222
public void schedule(CustomResource customResource, long delay, long period) {
2323
String resourceUid = ProcessingUtils.getUID(customResource);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package io.javaoperatorsdk.operator.processing.event.internal;
2+
3+
import io.fabric8.kubernetes.client.CustomResource;
4+
import io.javaoperatorsdk.operator.TestUtils;
5+
import io.javaoperatorsdk.operator.processing.ProcessingUtils;
6+
import io.javaoperatorsdk.operator.processing.event.EventHandler;
7+
import io.javaoperatorsdk.operator.processing.event.EventSourceManager;
8+
import org.junit.jupiter.api.BeforeEach;
9+
import org.junit.jupiter.api.Test;
10+
import org.mockito.ArgumentCaptor;
11+
12+
import java.util.List;
13+
14+
import static org.assertj.core.api.Assertions.assertThat;
15+
import static org.mockito.Mockito.*;
16+
17+
class TimerEventSourceTest {
18+
19+
public static final int INITIAL_DELAY = 50;
20+
public static final int PERIOD = 50;
21+
public static final int TESTING_TIME_SLACK = 20;
22+
23+
private TimerEventSource timerEventSource;
24+
private EventHandler eventHandlerMock = mock(EventHandler.class);
25+
private EventSourceManager eventSourceManagerMock = mock(EventSourceManager.class);
26+
27+
@BeforeEach
28+
public void setup() {
29+
timerEventSource = new TimerEventSource();
30+
timerEventSource.setEventHandler(eventHandlerMock);
31+
timerEventSource.setEventSourceManager(eventSourceManagerMock);
32+
}
33+
34+
@Test
35+
public void producesOneTimeEvent() {
36+
CustomResource customResource = TestUtils.testCustomResource();
37+
38+
timerEventSource.scheduleOnce(customResource, INITIAL_DELAY);
39+
40+
ArgumentCaptor<TimerEvent> argumentCaptor = ArgumentCaptor.forClass(TimerEvent.class);
41+
verify(eventHandlerMock, timeout(100).times(1)).handleEvent(argumentCaptor.capture());
42+
TimerEvent event = argumentCaptor.getValue();
43+
assertThat(event.getRelatedCustomResourceUid()).isEqualTo(ProcessingUtils.getUID(customResource));
44+
assertThat(event.getEventSource()).isEqualTo(timerEventSource);
45+
}
46+
47+
@Test
48+
public void producesEventsPeriodically() {
49+
CustomResource customResource = TestUtils.testCustomResource();
50+
51+
timerEventSource.schedule(customResource, INITIAL_DELAY, PERIOD);
52+
53+
ArgumentCaptor<TimerEvent> argumentCaptor = ArgumentCaptor.forClass(TimerEvent.class);
54+
verify(eventHandlerMock, timeout(INITIAL_DELAY + PERIOD + TESTING_TIME_SLACK).times(2))
55+
.handleEvent(argumentCaptor.capture());
56+
List<TimerEvent> events = argumentCaptor.getAllValues();
57+
assertThat(events).allMatch(e -> e.getRelatedCustomResourceUid().equals(ProcessingUtils.getUID(customResource)));
58+
assertThat(events).allMatch(e -> e.getEventSource().equals(timerEventSource));
59+
}
60+
61+
@Test
62+
public void deRegistersPeriodicalEventSources() throws InterruptedException {
63+
CustomResource customResource = TestUtils.testCustomResource();
64+
65+
timerEventSource.schedule(customResource, INITIAL_DELAY, PERIOD);
66+
Thread.sleep(INITIAL_DELAY + PERIOD + TESTING_TIME_SLACK);
67+
timerEventSource.eventSourceDeRegisteredForResource(ProcessingUtils.getUID(customResource));
68+
Thread.sleep(PERIOD + TESTING_TIME_SLACK);
69+
70+
verify(eventHandlerMock, times(2))
71+
.handleEvent(any());
72+
}
73+
74+
@Test
75+
public void deRegistersOneTimeEventSource() throws InterruptedException {
76+
CustomResource customResource = TestUtils.testCustomResource();
77+
78+
timerEventSource.scheduleOnce(customResource, INITIAL_DELAY);
79+
Thread.sleep(TESTING_TIME_SLACK);
80+
timerEventSource.eventSourceDeRegisteredForResource(ProcessingUtils.getUID(customResource));
81+
Thread.sleep(PERIOD + TESTING_TIME_SLACK);
82+
83+
verify(eventHandlerMock, times(0))
84+
.handleEvent(any());
85+
}
86+
87+
}

0 commit comments

Comments
 (0)