diff --git a/operator-framework-core/src/test/java/io/javaoperatorsdk/operator/processing/event/source/polling/PollingEventSourceTest.java b/operator-framework-core/src/test/java/io/javaoperatorsdk/operator/processing/event/source/polling/PollingEventSourceTest.java index bbfa18a4ed..0a777f0cbd 100644 --- a/operator-framework-core/src/test/java/io/javaoperatorsdk/operator/processing/event/source/polling/PollingEventSourceTest.java +++ b/operator-framework-core/src/test/java/io/javaoperatorsdk/operator/processing/event/source/polling/PollingEventSourceTest.java @@ -20,10 +20,12 @@ class PollingEventSourceTest extends AbstractEventSourceTestBase, EventHandler> { + public static final int DEFAULT_WAIT_PERIOD = 100; + private PollingEventSource.GenericResourceFetcher resourceFetcher = mock(PollingEventSource.GenericResourceFetcher.class); private final PollingEventSource pollingEventSource = - new PollingEventSource<>(resourceFetcher, 50L, SampleExternalResource.class, + new PollingEventSource<>(resourceFetcher, 30L, SampleExternalResource.class, (SampleExternalResource er) -> er.getName() + "#" + er.getValue()); @BeforeEach @@ -35,7 +37,7 @@ public void setup() { void pollsAndProcessesEvents() throws InterruptedException { when(resourceFetcher.fetchResources()).thenReturn(testResponseWithTwoValues()); pollingEventSource.start(); - Thread.sleep(100); + Thread.sleep(DEFAULT_WAIT_PERIOD); verify(eventHandler, times(2)).handleEvent(any()); } @@ -45,7 +47,7 @@ void propagatesEventForRemovedResources() throws InterruptedException { when(resourceFetcher.fetchResources()).thenReturn(testResponseWithTwoValues()) .thenReturn(testResponseWithOneValue()); pollingEventSource.start(); - Thread.sleep(150); + Thread.sleep(DEFAULT_WAIT_PERIOD); verify(eventHandler, times(3)).handleEvent(any()); } @@ -54,11 +56,29 @@ void propagatesEventForRemovedResources() throws InterruptedException { void doesNotPropagateEventIfResourceNotChanged() throws InterruptedException { when(resourceFetcher.fetchResources()).thenReturn(testResponseWithTwoValues()); pollingEventSource.start(); - Thread.sleep(250); + Thread.sleep(DEFAULT_WAIT_PERIOD); + + verify(eventHandler, times(2)).handleEvent(any()); + } + + @Test + void propagatesEventOnNewResourceForPrimary() throws InterruptedException { + when(resourceFetcher.fetchResources()) + .thenReturn(testResponseWithOneValue()) + .thenReturn(testResponseWithTwoValueForSameId()); + + pollingEventSource.start(); + Thread.sleep(DEFAULT_WAIT_PERIOD); verify(eventHandler, times(2)).handleEvent(any()); } + private Map> testResponseWithTwoValueForSameId() { + Map> res = new HashMap<>(); + res.put(primaryID1(), Set.of(testResource1(), testResource2())); + return res; + } + private Map> testResponseWithOneValue() { Map> res = new HashMap<>(); res.put(primaryID1(), Set.of(testResource1()));