Skip to content

Commit d563d63

Browse files
authored
feat: add additional test for PollingEventSource (#1409)
1 parent 34334ce commit d563d63

File tree

1 file changed

+24
-4
lines changed

1 file changed

+24
-4
lines changed

operator-framework-core/src/test/java/io/javaoperatorsdk/operator/processing/event/source/polling/PollingEventSourceTest.java

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,12 @@ class PollingEventSourceTest
2020
extends
2121
AbstractEventSourceTestBase<PollingEventSource<SampleExternalResource, HasMetadata>, EventHandler> {
2222

23+
public static final int DEFAULT_WAIT_PERIOD = 100;
24+
2325
private PollingEventSource.GenericResourceFetcher<SampleExternalResource> resourceFetcher =
2426
mock(PollingEventSource.GenericResourceFetcher.class);
2527
private final PollingEventSource<SampleExternalResource, HasMetadata> pollingEventSource =
26-
new PollingEventSource<>(resourceFetcher, 50L, SampleExternalResource.class,
28+
new PollingEventSource<>(resourceFetcher, 30L, SampleExternalResource.class,
2729
(SampleExternalResource er) -> er.getName() + "#" + er.getValue());
2830

2931
@BeforeEach
@@ -35,7 +37,7 @@ public void setup() {
3537
void pollsAndProcessesEvents() throws InterruptedException {
3638
when(resourceFetcher.fetchResources()).thenReturn(testResponseWithTwoValues());
3739
pollingEventSource.start();
38-
Thread.sleep(100);
40+
Thread.sleep(DEFAULT_WAIT_PERIOD);
3941

4042
verify(eventHandler, times(2)).handleEvent(any());
4143
}
@@ -45,7 +47,7 @@ void propagatesEventForRemovedResources() throws InterruptedException {
4547
when(resourceFetcher.fetchResources()).thenReturn(testResponseWithTwoValues())
4648
.thenReturn(testResponseWithOneValue());
4749
pollingEventSource.start();
48-
Thread.sleep(150);
50+
Thread.sleep(DEFAULT_WAIT_PERIOD);
4951

5052
verify(eventHandler, times(3)).handleEvent(any());
5153
}
@@ -54,11 +56,29 @@ void propagatesEventForRemovedResources() throws InterruptedException {
5456
void doesNotPropagateEventIfResourceNotChanged() throws InterruptedException {
5557
when(resourceFetcher.fetchResources()).thenReturn(testResponseWithTwoValues());
5658
pollingEventSource.start();
57-
Thread.sleep(250);
59+
Thread.sleep(DEFAULT_WAIT_PERIOD);
60+
61+
verify(eventHandler, times(2)).handleEvent(any());
62+
}
63+
64+
@Test
65+
void propagatesEventOnNewResourceForPrimary() throws InterruptedException {
66+
when(resourceFetcher.fetchResources())
67+
.thenReturn(testResponseWithOneValue())
68+
.thenReturn(testResponseWithTwoValueForSameId());
69+
70+
pollingEventSource.start();
71+
Thread.sleep(DEFAULT_WAIT_PERIOD);
5872

5973
verify(eventHandler, times(2)).handleEvent(any());
6074
}
6175

76+
private Map<ResourceID, Set<SampleExternalResource>> testResponseWithTwoValueForSameId() {
77+
Map<ResourceID, Set<SampleExternalResource>> res = new HashMap<>();
78+
res.put(primaryID1(), Set.of(testResource1(), testResource2()));
79+
return res;
80+
}
81+
6282
private Map<ResourceID, Set<SampleExternalResource>> testResponseWithOneValue() {
6383
Map<ResourceID, Set<SampleExternalResource>> res = new HashMap<>();
6484
res.put(primaryID1(), Set.of(testResource1()));

0 commit comments

Comments
 (0)