1
+ package io .javaoperatorsdk .operator .processing .event .internal ;
2
+
3
+ import io .fabric8 .kubernetes .client .CustomResource ;
4
+ import io .javaoperatorsdk .operator .TestUtils ;
5
+ import io .javaoperatorsdk .operator .processing .ProcessingUtils ;
6
+ import io .javaoperatorsdk .operator .processing .event .EventHandler ;
7
+ import io .javaoperatorsdk .operator .processing .event .EventSourceManager ;
8
+ import org .junit .jupiter .api .BeforeEach ;
9
+ import org .junit .jupiter .api .Test ;
10
+ import org .mockito .ArgumentCaptor ;
11
+
12
+ import java .util .List ;
13
+
14
+ import static org .assertj .core .api .Assertions .assertThat ;
15
+ import static org .mockito .Mockito .*;
16
+
17
+ class TimerEventSourceTest {
18
+
19
+ public static final int INITIAL_DELAY = 50 ;
20
+ public static final int PERIOD = 50 ;
21
+ public static final int TESTING_TIME_SLACK = 20 ;
22
+
23
+ private TimerEventSource timerEventSource ;
24
+ private EventHandler eventHandlerMock = mock (EventHandler .class );
25
+ private EventSourceManager eventSourceManagerMock = mock (EventSourceManager .class );
26
+
27
+ @ BeforeEach
28
+ public void setup () {
29
+ timerEventSource = new TimerEventSource ();
30
+ timerEventSource .setEventHandler (eventHandlerMock );
31
+ timerEventSource .setEventSourceManager (eventSourceManagerMock );
32
+ }
33
+
34
+ @ Test
35
+ public void producesOneTimeEvent () {
36
+ CustomResource customResource = TestUtils .testCustomResource ();
37
+
38
+ timerEventSource .scheduleOnce (customResource , INITIAL_DELAY );
39
+
40
+ ArgumentCaptor <TimerEvent > argumentCaptor = ArgumentCaptor .forClass (TimerEvent .class );
41
+ verify (eventHandlerMock , timeout (100 ).times (1 )).handleEvent (argumentCaptor .capture ());
42
+ TimerEvent event = argumentCaptor .getValue ();
43
+ assertThat (event .getRelatedCustomResourceUid ()).isEqualTo (ProcessingUtils .getUID (customResource ));
44
+ assertThat (event .getEventSource ()).isEqualTo (timerEventSource );
45
+ }
46
+
47
+ @ Test
48
+ public void producesEventsPeriodically () {
49
+ CustomResource customResource = TestUtils .testCustomResource ();
50
+
51
+ timerEventSource .schedule (customResource , INITIAL_DELAY , PERIOD );
52
+
53
+ ArgumentCaptor <TimerEvent > argumentCaptor = ArgumentCaptor .forClass (TimerEvent .class );
54
+ verify (eventHandlerMock , timeout (INITIAL_DELAY + PERIOD + TESTING_TIME_SLACK ).times (2 ))
55
+ .handleEvent (argumentCaptor .capture ());
56
+ List <TimerEvent > events = argumentCaptor .getAllValues ();
57
+ assertThat (events ).allMatch (e -> e .getRelatedCustomResourceUid ().equals (ProcessingUtils .getUID (customResource )));
58
+ assertThat (events ).allMatch (e -> e .getEventSource ().equals (timerEventSource ));
59
+ }
60
+
61
+ @ Test
62
+ public void deRegistersPeriodicalEventSources () throws InterruptedException {
63
+ CustomResource customResource = TestUtils .testCustomResource ();
64
+
65
+ timerEventSource .schedule (customResource , INITIAL_DELAY , PERIOD );
66
+ Thread .sleep (INITIAL_DELAY + PERIOD + TESTING_TIME_SLACK );
67
+ timerEventSource .eventSourceDeRegisteredForResource (ProcessingUtils .getUID (customResource ));
68
+ Thread .sleep (PERIOD + TESTING_TIME_SLACK );
69
+
70
+ verify (eventHandlerMock , times (2 ))
71
+ .handleEvent (any ());
72
+ }
73
+
74
+ @ Test
75
+ public void deRegistersOneTimeEventSource () throws InterruptedException {
76
+ CustomResource customResource = TestUtils .testCustomResource ();
77
+
78
+ timerEventSource .scheduleOnce (customResource , INITIAL_DELAY );
79
+ Thread .sleep (TESTING_TIME_SLACK );
80
+ timerEventSource .eventSourceDeRegisteredForResource (ProcessingUtils .getUID (customResource ));
81
+ Thread .sleep (PERIOD + TESTING_TIME_SLACK );
82
+
83
+ verify (eventHandlerMock , times (0 ))
84
+ .handleEvent (any ());
85
+ }
86
+
87
+ }
0 commit comments