Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@ class PollingEventSourceTest
extends
AbstractEventSourceTestBase<PollingEventSource<SampleExternalResource, HasMetadata>, EventHandler> {

public static final int DEFAULT_WAIT_PERIOD = 100;

private PollingEventSource.GenericResourceFetcher<SampleExternalResource> resourceFetcher =
mock(PollingEventSource.GenericResourceFetcher.class);
private final PollingEventSource<SampleExternalResource, HasMetadata> pollingEventSource =
new PollingEventSource<>(resourceFetcher, 50L, SampleExternalResource.class,
new PollingEventSource<>(resourceFetcher, 30L, SampleExternalResource.class,
(SampleExternalResource er) -> er.getName() + "#" + er.getValue());

@BeforeEach
Expand All @@ -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());
}
Expand All @@ -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());
}
Expand All @@ -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<ResourceID, Set<SampleExternalResource>> testResponseWithTwoValueForSameId() {
Map<ResourceID, Set<SampleExternalResource>> res = new HashMap<>();
res.put(primaryID1(), Set.of(testResource1(), testResource2()));
return res;
}

private Map<ResourceID, Set<SampleExternalResource>> testResponseWithOneValue() {
Map<ResourceID, Set<SampleExternalResource>> res = new HashMap<>();
res.put(primaryID1(), Set.of(testResource1()));
Expand Down