Skip to content

feat: add additional test for PollingEventSource #1409

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 24, 2022
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