21
21
22
22
import org .elasticsearch .Build ;
23
23
import org .elasticsearch .common .CheckedConsumer ;
24
+ import org .elasticsearch .common .unit .TimeValue ;
24
25
import org .elasticsearch .test .ESTestCase ;
25
26
import org .elasticsearch .test .hamcrest .OptionalMatchers ;
27
+ import org .elasticsearch .threadpool .Scheduler ;
28
+ import org .elasticsearch .threadpool .ThreadPool ;
26
29
27
30
import java .io .IOException ;
28
31
import java .util .Optional ;
29
32
import java .util .concurrent .atomic .AtomicBoolean ;
30
33
import java .util .concurrent .atomic .AtomicInteger ;
31
34
import java .util .concurrent .atomic .AtomicReference ;
32
- import java .util .function .Consumer ;
35
+ import java .util .function .BiConsumer ;
33
36
34
37
import static org .hamcrest .Matchers .containsString ;
35
38
import static org .hamcrest .Matchers .equalTo ;
36
39
import static org .hamcrest .Matchers .hasToString ;
37
40
import static org .hamcrest .Matchers .instanceOf ;
41
+ import static org .mockito .Matchers .any ;
42
+ import static org .mockito .Matchers .eq ;
43
+ import static org .mockito .Mockito .mock ;
44
+ import static org .mockito .Mockito .verify ;
45
+ import static org .mockito .Mockito .when ;
38
46
39
47
public class SystemdPluginTests extends ESTestCase {
40
48
41
49
private final Build .Type randomPackageBuildType = randomFrom (Build .Type .DEB , Build .Type .RPM );
42
50
private final Build .Type randomNonPackageBuildType =
43
51
randomValueOtherThanMany (t -> t == Build .Type .DEB || t == Build .Type .RPM , () -> randomFrom (Build .Type .values ()));
44
52
53
+ final Scheduler .Cancellable extender = mock (Scheduler .Cancellable .class );
54
+ final ThreadPool threadPool = mock (ThreadPool .class );
55
+
56
+ {
57
+ when (extender .cancel ()).thenReturn (true );
58
+ when (threadPool .scheduleWithFixedDelay (any (Runnable .class ), eq (TimeValue .timeValueSeconds (15 )), eq (ThreadPool .Names .SAME )))
59
+ .thenReturn (extender );
60
+ }
61
+
45
62
public void testIsEnabled () {
46
63
final SystemdPlugin plugin = new SystemdPlugin (false , randomPackageBuildType , Boolean .TRUE .toString ());
64
+ plugin .createComponents (null , null , threadPool , null , null , null , null , null , null );
47
65
assertTrue (plugin .isEnabled ());
66
+ assertNotNull (plugin .extender );
48
67
}
49
68
50
69
public void testIsNotPackageDistribution () {
51
70
final SystemdPlugin plugin = new SystemdPlugin (false , randomNonPackageBuildType , Boolean .TRUE .toString ());
71
+ plugin .createComponents (null , null , threadPool , null , null , null , null , null , null );
52
72
assertFalse (plugin .isEnabled ());
73
+ assertNull (plugin .extender );
53
74
}
54
75
55
76
public void testIsImplicitlyNotEnabled () {
56
77
final SystemdPlugin plugin = new SystemdPlugin (false , randomPackageBuildType , null );
78
+ plugin .createComponents (null , null , threadPool , null , null , null , null , null , null );
57
79
assertFalse (plugin .isEnabled ());
80
+ assertNull (plugin .extender );
58
81
}
59
82
60
83
public void testIsExplicitlyNotEnabled () {
61
84
final SystemdPlugin plugin = new SystemdPlugin (false , randomPackageBuildType , Boolean .FALSE .toString ());
85
+ plugin .createComponents (null , null , threadPool , null , null , null , null , null , null );
62
86
assertFalse (plugin .isEnabled ());
87
+ assertNull (plugin .extender );
63
88
}
64
89
65
90
public void testInvalid () {
@@ -75,15 +100,18 @@ public void testOnNodeStartedSuccess() {
75
100
runTestOnNodeStarted (
76
101
Boolean .TRUE .toString (),
77
102
randomIntBetween (0 , Integer .MAX_VALUE ),
78
- maybe -> assertThat (maybe , OptionalMatchers .isEmpty ()));
103
+ (maybe , plugin ) -> {
104
+ assertThat (maybe , OptionalMatchers .isEmpty ());
105
+ verify (plugin .extender ).cancel ();
106
+ });
79
107
}
80
108
81
109
public void testOnNodeStartedFailure () {
82
110
final int rc = randomIntBetween (Integer .MIN_VALUE , -1 );
83
111
runTestOnNodeStarted (
84
112
Boolean .TRUE .toString (),
85
113
rc ,
86
- maybe -> {
114
+ ( maybe , plugin ) -> {
87
115
assertThat (maybe , OptionalMatchers .isPresent ());
88
116
// noinspection OptionalGetWithoutIsPresent
89
117
assertThat (maybe .get (), instanceOf (RuntimeException .class ));
@@ -95,48 +123,48 @@ public void testOnNodeStartedNotEnabled() {
95
123
runTestOnNodeStarted (
96
124
Boolean .FALSE .toString (),
97
125
randomInt (),
98
- maybe -> assertThat (maybe , OptionalMatchers .isEmpty ()));
126
+ ( maybe , plugin ) -> assertThat (maybe , OptionalMatchers .isEmpty ()));
99
127
}
100
128
101
129
private void runTestOnNodeStarted (
102
130
final String esSDNotify ,
103
131
final int rc ,
104
- final Consumer <Optional <Exception >> assertions ) {
132
+ final BiConsumer <Optional <Exception >, SystemdPlugin > assertions ) {
105
133
runTest (esSDNotify , rc , assertions , SystemdPlugin ::onNodeStarted , "READY=1" );
106
134
}
107
135
108
136
public void testCloseSuccess () {
109
137
runTestClose (
110
138
Boolean .TRUE .toString (),
111
139
randomIntBetween (1 , Integer .MAX_VALUE ),
112
- maybe -> assertThat (maybe , OptionalMatchers .isEmpty ()));
140
+ ( maybe , plugin ) -> assertThat (maybe , OptionalMatchers .isEmpty ()));
113
141
}
114
142
115
143
public void testCloseFailure () {
116
144
runTestClose (
117
145
Boolean .TRUE .toString (),
118
146
randomIntBetween (Integer .MIN_VALUE , -1 ),
119
- maybe -> assertThat (maybe , OptionalMatchers .isEmpty ()));
147
+ ( maybe , plugin ) -> assertThat (maybe , OptionalMatchers .isEmpty ()));
120
148
}
121
149
122
150
public void testCloseNotEnabled () {
123
151
runTestClose (
124
152
Boolean .FALSE .toString (),
125
153
randomInt (),
126
- maybe -> assertThat (maybe , OptionalMatchers .isEmpty ()));
154
+ ( maybe , plugin ) -> assertThat (maybe , OptionalMatchers .isEmpty ()));
127
155
}
128
156
129
157
private void runTestClose (
130
158
final String esSDNotify ,
131
159
final int rc ,
132
- final Consumer <Optional <Exception >> assertions ) {
160
+ final BiConsumer <Optional <Exception >, SystemdPlugin > assertions ) {
133
161
runTest (esSDNotify , rc , assertions , SystemdPlugin ::close , "STOPPING=1" );
134
162
}
135
163
136
164
private void runTest (
137
165
final String esSDNotify ,
138
166
final int rc ,
139
- final Consumer <Optional <Exception >> assertions ,
167
+ final BiConsumer <Optional <Exception >, SystemdPlugin > assertions ,
140
168
final CheckedConsumer <SystemdPlugin , IOException > invocation ,
141
169
final String expectedState ) {
142
170
final AtomicBoolean invoked = new AtomicBoolean ();
@@ -153,16 +181,22 @@ int sd_notify(final int unset_environment, final String state) {
153
181
}
154
182
155
183
};
184
+ plugin .createComponents (null , null , threadPool , null , null , null , null , null , null );
185
+ if (Boolean .TRUE .toString ().equals (esSDNotify )) {
186
+ assertNotNull (plugin .extender );
187
+ } else {
188
+ assertNull (plugin .extender );
189
+ }
156
190
157
191
boolean success = false ;
158
192
try {
159
193
invocation .accept (plugin );
160
194
success = true ;
161
195
} catch (final Exception e ) {
162
- assertions .accept (Optional .of (e ));
196
+ assertions .accept (Optional .of (e ), plugin );
163
197
}
164
198
if (success ) {
165
- assertions .accept (Optional .empty ());
199
+ assertions .accept (Optional .empty (), plugin );
166
200
}
167
201
if (Boolean .TRUE .toString ().equals (esSDNotify )) {
168
202
assertTrue (invoked .get ());
0 commit comments