1515 */
1616package io .javaoperatorsdk .operator .processing .event .source .informer ;
1717
18- import java .time .Duration ;
1918import java .util .Optional ;
2019import java .util .Set ;
2120import java .util .concurrent .CountDownLatch ;
22- import java .util .concurrent .ExecutorService ;
23- import java .util .concurrent .Executors ;
24- import java .util .function .UnaryOperator ;
2521
2622import org .junit .jupiter .api .BeforeEach ;
2723import org .junit .jupiter .api .Test ;
2824import org .junit .jupiter .api .TestInstance ;
2925
30- import io .fabric8 .kubernetes .api .model .HasMetadata ;
3126import io .fabric8 .kubernetes .api .model .ObjectMeta ;
3227import io .fabric8 .kubernetes .api .model .apps .Deployment ;
3328import io .fabric8 .kubernetes .api .model .apps .DeploymentBuilder ;
4338import io .javaoperatorsdk .operator .api .config .informer .InformerEventSourceConfiguration ;
4439import io .javaoperatorsdk .operator .processing .event .EventHandler ;
4540import io .javaoperatorsdk .operator .processing .event .ResourceID ;
41+ import io .javaoperatorsdk .operator .processing .event .source .EventFilterTestUtils ;
4642import io .javaoperatorsdk .operator .processing .event .source .ResourceAction ;
4743import io .javaoperatorsdk .operator .processing .event .source .SecondaryToPrimaryMapper ;
4844import io .javaoperatorsdk .operator .processing .event .source .informer .TemporaryResourceCache .EventHandling ;
4945import io .javaoperatorsdk .operator .sample .simple .TestCustomResource ;
5046
5147import static io .javaoperatorsdk .operator .api .reconciler .Constants .DEFAULT_NAMESPACES_SET ;
48+ import static io .javaoperatorsdk .operator .processing .event .source .EventFilterTestUtils .withResourceVersion ;
5249import static org .assertj .core .api .Assertions .assertThat ;
5350import static org .awaitility .Awaitility .await ;
5451import static org .junit .jupiter .api .Assertions .assertThrows ;
@@ -72,8 +69,6 @@ class InformerEventSourceTest {
7269 private static final String PREV_RESOURCE_VERSION = "0" ;
7370 private static final String DEFAULT_RESOURCE_VERSION = "1" ;
7471
75- ExecutorService executorService = Executors .newCachedThreadPool ();
76-
7772 private InformerEventSource <Deployment , TestCustomResource > informerEventSource ;
7873 private final KubernetesClient clientMock = MockKubernetesClient .client (Deployment .class );
7974 private TemporaryResourceCache <Deployment > temporaryResourceCache =
@@ -224,12 +219,14 @@ void handlesPrevResourceVersionForUpdateInCaseOfException() {
224219 withRealTemporaryResourceCache ();
225220
226221 CountDownLatch latch =
227- sendForEventFilteringUpdate (
222+ EventFilterTestUtils .sendForEventFilteringUpdate (
223+ informerEventSource ,
228224 testDeployment (),
229225 r -> {
230226 throw new KubernetesClientException ("fake" );
231227 });
232- informerEventSource .onUpdate (testDeployment (), withResourceVersion (testDeployment (), 2 ));
228+ informerEventSource .onUpdate (
229+ deploymentWithResourceVersion (1 ), deploymentWithResourceVersion (2 ));
233230 latch .countDown ();
234231
235232 expectHandleEvent (2 , 1 );
@@ -239,7 +236,6 @@ void handlesPrevResourceVersionForUpdateInCaseOfException() {
239236 void handlesPrevResourceVersionForUpdateInCaseOfMultipleUpdates () {
240237 withRealTemporaryResourceCache ();
241238
242- withRealTemporaryResourceCache ();
243239 var deployment = testDeployment ();
244240 CountDownLatch latch = sendForEventFilteringUpdate (deployment , 2 );
245241 informerEventSource .onUpdate (
@@ -252,20 +248,16 @@ void handlesPrevResourceVersionForUpdateInCaseOfMultipleUpdates() {
252248 }
253249
254250 @ Test
255- void doesNotPropagateEventIfReceivedBeforeUpdate () {
251+ void doesNotPropagateEventIfReceivedBeforeUpdate () throws InterruptedException {
256252 withRealTemporaryResourceCache ();
257- var deployment = testDeployment ();
258- CountDownLatch latch = sendForEventFilteringUpdate (deployment , 2 );
259- informerEventSource .onUpdate (deployment , deploymentWithResourceVersion (2 ));
253+
254+ CountDownLatch latch = sendForEventFilteringUpdate (2 );
255+ informerEventSource .onUpdate (
256+ deploymentWithResourceVersion (1 ), deploymentWithResourceVersion (2 ));
260257 latch .countDown ();
261258
262- await ()
263- .pollDelay (Duration .ofMillis (100 ))
264- .untilAsserted (
265- () -> {
266- verify (informerEventSource , never ())
267- .handleEvent (any (), any (), any (), any (), anyBoolean ());
268- });
259+ Thread .sleep (100 );
260+ verify (informerEventSource , never ()).handleEvent (any (), any (), any (), any (), anyBoolean ());
269261 }
270262
271263 private void expectHandleEvent (int newResourceVersion , int oldResourceVersion ) {
@@ -297,34 +289,8 @@ private CountDownLatch sendForEventFilteringUpdate(int resourceVersion) {
297289 }
298290
299291 private CountDownLatch sendForEventFilteringUpdate (Deployment deployment , int resourceVersion ) {
300- return sendForEventFilteringUpdate (
301- deployment , r -> withResourceVersion (deployment , resourceVersion ));
302- }
303-
304- private CountDownLatch sendForEventFilteringUpdate (
305- Deployment resource , UnaryOperator <Deployment > updateMethod ) {
306- try {
307- CountDownLatch latch = new CountDownLatch (1 );
308- CountDownLatch sendOnGoingLatch = new CountDownLatch (1 );
309- executorService .submit (
310- () ->
311- informerEventSource .eventFilteringUpdateAndCacheResource (
312- resource ,
313- r -> {
314- try {
315- sendOnGoingLatch .countDown ();
316- latch .await ();
317- var resp = updateMethod .apply (r );
318- return resp ;
319- } catch (InterruptedException e ) {
320- throw new RuntimeException (e );
321- }
322- }));
323- sendOnGoingLatch .await ();
324- return latch ;
325- } catch (InterruptedException e ) {
326- throw new RuntimeException (e );
327- }
292+ return EventFilterTestUtils .sendForEventFilteringUpdate (
293+ informerEventSource , deployment , r -> withResourceVersion (deployment , resourceVersion ));
328294 }
329295
330296 private void withRealTemporaryResourceCache () {
@@ -336,15 +302,6 @@ Deployment deploymentWithResourceVersion(int resourceVersion) {
336302 return withResourceVersion (testDeployment (), resourceVersion );
337303 }
338304
339- <R extends HasMetadata > R withResourceVersion (R resource , int resourceVersion ) {
340- var v = resource .getMetadata ().getResourceVersion ();
341- if (v == null ) {
342- throw new IllegalArgumentException ("Resource version is null" );
343- }
344- resource .getMetadata ().setResourceVersion ("" + resourceVersion );
345- return resource ;
346- }
347-
348305 @ Test
349306 void informerStoppedHandlerShouldBeCalledWhenInformerStops () {
350307 final var exception = new RuntimeException ("Informer stopped exceptionally!" );
0 commit comments