Skip to content

Commit 91fffc3

Browse files
committed
additional test
1 parent 631759b commit 91fffc3

File tree

2 files changed

+23
-17
lines changed

2 files changed

+23
-17
lines changed

operator-framework-core/src/main/java/io/javaoperatorsdk/operator/processing/event/source/polling/PerResourcePollingEventSource.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -119,15 +119,11 @@ public void onResourceDeleted(P resource) {
119119

120120
// This method is always called from the same Thread for the same resource,
121121
// since events from ResourceEventAware are propagated from the thread of the informer. This is
122-
// important
123-
// because otherwise there will be a race condition related to the timerTasks.
124-
@SuppressWarnings("unchecked")
122+
// important because otherwise there will be a race condition related to the timerTasks.
125123
private void checkAndRegisterTask(P resource) {
126124
var primaryID = ResourceID.fromResource(resource);
127125
if (scheduledFutures.get(primaryID) == null && (registerPredicate == null
128126
|| registerPredicate.test(resource))) {
129-
130-
131127
var cachedResources = cache.get(primaryID);
132128
var actualResources =
133129
cachedResources == null ? null : new HashSet<>(cachedResources.values());
@@ -154,7 +150,6 @@ public void run() {
154150
var primary = resourceCache.get(primaryID);
155151
if (primary.isEmpty()) {
156152
log.warn("No resource in cache for resource ID: {}", primaryID);
157-
// todo think through + test
158153
// no new execution is scheduled in this case, a on delete event should be received shortly
159154
} else {
160155
var actualResources = primary.map(p -> getAndCacheResource(p, false));

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

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -150,23 +150,34 @@ void supportsDynamicPollingDelay() {
150150

151151
await().pollDelay(Duration.ofMillis(PERIOD)).atMost(Duration.ofMillis((long) (1.5 * PERIOD)))
152152
.pollInterval(Duration.ofMillis(20))
153-
.untilAsserted(() -> {
154-
verify(supplier,times(1)).fetchResources(any());
155-
});
156-
153+
.untilAsserted(() -> verify(supplier,times(1)).fetchResources(any()));
157154
// verifying that it is not called as with normal interval
158155
await().pollDelay(Duration.ofMillis(PERIOD)).atMost(Duration.ofMillis((long) (1.5*PERIOD)))
159156
.pollInterval(Duration.ofMillis(20))
160-
.untilAsserted(() -> {
161-
verify(supplier,times(1)).fetchResources(any());
162-
});
163-
157+
.untilAsserted(() -> verify(supplier,times(1)).fetchResources(any()));
164158
await().pollDelay(Duration.ofMillis(PERIOD)).atMost(Duration.ofMillis(2 * PERIOD))
165159
.pollInterval(Duration.ofMillis(20))
166-
.untilAsserted(() -> {
167-
verify(supplier,times(2)).fetchResources(any());
168-
});
160+
.untilAsserted(() -> verify(supplier,times(2)).fetchResources(any()));
161+
}
162+
163+
@Test
164+
void deleteEventCancelsTheScheduling() {
165+
when(supplier.fetchResources(any()))
166+
.thenReturn(Set.of(SampleExternalResource.testResource1()));
167+
168+
source.onResourceCreated(testCustomResource);
169+
170+
await().pollDelay(Duration.ofMillis(PERIOD))
171+
.atMost(Duration.ofMillis((2* PERIOD)))
172+
.pollInterval(Duration.ofMillis(20))
173+
.untilAsserted(() -> verify(supplier,times(1)).fetchResources(any()));
174+
175+
source.onResourceDeleted(testCustomResource);
169176

177+
// check if not called again.
178+
await().pollDelay(Duration.ofMillis(PERIOD))
179+
.atMost(Duration.ofMillis((2* PERIOD)))
180+
.untilAsserted(() -> verify(supplier,times(1)).fetchResources(any()));
170181
}
171182

172183
}

0 commit comments

Comments
 (0)