11
11
12
12
import java .time .LocalDate ;
13
13
import java .time .LocalDateTime ;
14
+ import java .util .Arrays ;
14
15
15
16
import static org .junit .jupiter .api .Assertions .*;
16
17
import static org .mockito .Mockito .*;
17
18
18
19
class CustomResourceEventSourceTest {
19
20
21
+ public static final String FINALIZER = "finalizer" ;
20
22
CustomResourceCache customResourceCache = new CustomResourceCache ();
21
23
MixedOperation mixedOperation = mock (MixedOperation .class );
22
24
EventHandler eventHandler = mock (EventHandler .class );
23
25
24
26
private CustomResourceEventSource customResourceEventSource =
25
- CustomResourceEventSource .customResourceEventSourceForAllNamespaces (customResourceCache , mixedOperation , true );
27
+ CustomResourceEventSource .customResourceEventSourceForAllNamespaces (customResourceCache , mixedOperation , true , FINALIZER );
26
28
27
29
@ BeforeEach
28
30
public void setup () {
@@ -32,25 +34,26 @@ public void setup() {
32
34
@ Test
33
35
public void skipsEventHandlingIfGenerationNotIncreased () {
34
36
TestCustomResource customResource1 = TestUtils .testCustomResource ();
37
+ customResource1 .getMetadata ().setFinalizers (Arrays .asList (FINALIZER ));
35
38
36
39
customResourceEventSource .eventReceived (Watcher .Action .MODIFIED , customResource1 );
37
- verify (eventHandler ,times (1 )).handleEvent (any ());
40
+ verify (eventHandler , times (1 )).handleEvent (any ());
38
41
39
42
customResourceEventSource .eventReceived (Watcher .Action .MODIFIED , customResource1 );
40
- verify (eventHandler ,times (1 )).handleEvent (any ());
43
+ verify (eventHandler , times (1 )).handleEvent (any ());
41
44
}
42
45
43
46
@ Test
44
47
public void dontSkipEventHandlingIfMarkedForDeletion () {
45
48
TestCustomResource customResource1 = TestUtils .testCustomResource ();
46
49
47
50
customResourceEventSource .eventReceived (Watcher .Action .MODIFIED , customResource1 );
48
- verify (eventHandler ,times (1 )).handleEvent (any ());
51
+ verify (eventHandler , times (1 )).handleEvent (any ());
49
52
50
53
// mark for deletion
51
54
customResource1 .getMetadata ().setDeletionTimestamp (LocalDateTime .now ().toString ());
52
55
customResourceEventSource .eventReceived (Watcher .Action .MODIFIED , customResource1 );
53
- verify (eventHandler ,times (2 )).handleEvent (any ());
56
+ verify (eventHandler , times (2 )).handleEvent (any ());
54
57
}
55
58
56
59
@@ -59,26 +62,38 @@ public void normalExecutionIfGenerationChanges() {
59
62
TestCustomResource customResource1 = TestUtils .testCustomResource ();
60
63
61
64
customResourceEventSource .eventReceived (Watcher .Action .MODIFIED , customResource1 );
62
- verify (eventHandler ,times (1 )).handleEvent (any ());
65
+ verify (eventHandler , times (1 )).handleEvent (any ());
63
66
64
67
customResource1 .getMetadata ().setGeneration (2L );
65
68
customResourceEventSource .eventReceived (Watcher .Action .MODIFIED , customResource1 );
66
- verify (eventHandler ,times (2 )).handleEvent (any ());
69
+ verify (eventHandler , times (2 )).handleEvent (any ());
67
70
}
68
71
69
72
@ Test
70
73
public void handlesAllEventIfNotGenerationAware () {
71
74
customResourceEventSource =
72
- CustomResourceEventSource .customResourceEventSourceForAllNamespaces (customResourceCache , mixedOperation , false );
75
+ CustomResourceEventSource .customResourceEventSourceForAllNamespaces (customResourceCache ,
76
+ mixedOperation , false , FINALIZER );
73
77
setup ();
74
78
75
79
TestCustomResource customResource1 = TestUtils .testCustomResource ();
76
80
77
81
customResourceEventSource .eventReceived (Watcher .Action .MODIFIED , customResource1 );
78
- verify (eventHandler ,times (1 )).handleEvent (any ());
82
+ verify (eventHandler , times (1 )).handleEvent (any ());
79
83
80
84
customResourceEventSource .eventReceived (Watcher .Action .MODIFIED , customResource1 );
81
- verify (eventHandler ,times (2 )).handleEvent (any ());
85
+ verify (eventHandler , times (2 )).handleEvent (any ());
86
+ }
87
+
88
+ @ Test
89
+ public void eventNotMarkedForLastGenerationIfNoFinalizer () {
90
+ TestCustomResource customResource1 = TestUtils .testCustomResource ();
91
+
92
+ customResourceEventSource .eventReceived (Watcher .Action .MODIFIED , customResource1 );
93
+ verify (eventHandler , times (1 )).handleEvent (any ());
94
+
95
+ customResourceEventSource .eventReceived (Watcher .Action .MODIFIED , customResource1 );
96
+ verify (eventHandler , times (2 )).handleEvent (any ());
82
97
}
83
98
84
99
}
0 commit comments