2121
2222import org .elasticsearch .Build ;
2323import org .elasticsearch .common .CheckedConsumer ;
24+ import org .elasticsearch .common .unit .TimeValue ;
2425import org .elasticsearch .test .ESTestCase ;
2526import org .elasticsearch .test .hamcrest .OptionalMatchers ;
27+ import org .elasticsearch .threadpool .Scheduler ;
28+ import org .elasticsearch .threadpool .ThreadPool ;
2629
2730import java .io .IOException ;
2831import java .util .Optional ;
2932import java .util .concurrent .atomic .AtomicBoolean ;
3033import java .util .concurrent .atomic .AtomicInteger ;
3134import java .util .concurrent .atomic .AtomicReference ;
32- import java .util .function .Consumer ;
35+ import java .util .function .BiConsumer ;
3336
3437import static org .hamcrest .Matchers .containsString ;
3538import static org .hamcrest .Matchers .equalTo ;
3639import static org .hamcrest .Matchers .hasToString ;
3740import 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 ;
3846
3947public class SystemdPluginTests extends ESTestCase {
4048
4149 private final Build .Type randomPackageBuildType = randomFrom (Build .Type .DEB , Build .Type .RPM );
4250 private final Build .Type randomNonPackageBuildType =
4351 randomValueOtherThanMany (t -> t == Build .Type .DEB || t == Build .Type .RPM , () -> randomFrom (Build .Type .values ()));
4452
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+
4562 public void testIsEnabled () {
4663 final SystemdPlugin plugin = new SystemdPlugin (false , randomPackageBuildType , Boolean .TRUE .toString ());
64+ plugin .createComponents (null , null , threadPool , null , null , null , null , null , null );
4765 assertTrue (plugin .isEnabled ());
66+ assertNotNull (plugin .extender );
4867 }
4968
5069 public void testIsNotPackageDistribution () {
5170 final SystemdPlugin plugin = new SystemdPlugin (false , randomNonPackageBuildType , Boolean .TRUE .toString ());
71+ plugin .createComponents (null , null , threadPool , null , null , null , null , null , null );
5272 assertFalse (plugin .isEnabled ());
73+ assertNull (plugin .extender );
5374 }
5475
5576 public void testIsImplicitlyNotEnabled () {
5677 final SystemdPlugin plugin = new SystemdPlugin (false , randomPackageBuildType , null );
78+ plugin .createComponents (null , null , threadPool , null , null , null , null , null , null );
5779 assertFalse (plugin .isEnabled ());
80+ assertNull (plugin .extender );
5881 }
5982
6083 public void testIsExplicitlyNotEnabled () {
6184 final SystemdPlugin plugin = new SystemdPlugin (false , randomPackageBuildType , Boolean .FALSE .toString ());
85+ plugin .createComponents (null , null , threadPool , null , null , null , null , null , null );
6286 assertFalse (plugin .isEnabled ());
87+ assertNull (plugin .extender );
6388 }
6489
6590 public void testInvalid () {
@@ -75,15 +100,18 @@ public void testOnNodeStartedSuccess() {
75100 runTestOnNodeStarted (
76101 Boolean .TRUE .toString (),
77102 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+ });
79107 }
80108
81109 public void testOnNodeStartedFailure () {
82110 final int rc = randomIntBetween (Integer .MIN_VALUE , -1 );
83111 runTestOnNodeStarted (
84112 Boolean .TRUE .toString (),
85113 rc ,
86- maybe -> {
114+ ( maybe , plugin ) -> {
87115 assertThat (maybe , OptionalMatchers .isPresent ());
88116 // noinspection OptionalGetWithoutIsPresent
89117 assertThat (maybe .get (), instanceOf (RuntimeException .class ));
@@ -95,48 +123,48 @@ public void testOnNodeStartedNotEnabled() {
95123 runTestOnNodeStarted (
96124 Boolean .FALSE .toString (),
97125 randomInt (),
98- maybe -> assertThat (maybe , OptionalMatchers .isEmpty ()));
126+ ( maybe , plugin ) -> assertThat (maybe , OptionalMatchers .isEmpty ()));
99127 }
100128
101129 private void runTestOnNodeStarted (
102130 final String esSDNotify ,
103131 final int rc ,
104- final Consumer <Optional <Exception >> assertions ) {
132+ final BiConsumer <Optional <Exception >, SystemdPlugin > assertions ) {
105133 runTest (esSDNotify , rc , assertions , SystemdPlugin ::onNodeStarted , "READY=1" );
106134 }
107135
108136 public void testCloseSuccess () {
109137 runTestClose (
110138 Boolean .TRUE .toString (),
111139 randomIntBetween (1 , Integer .MAX_VALUE ),
112- maybe -> assertThat (maybe , OptionalMatchers .isEmpty ()));
140+ ( maybe , plugin ) -> assertThat (maybe , OptionalMatchers .isEmpty ()));
113141 }
114142
115143 public void testCloseFailure () {
116144 runTestClose (
117145 Boolean .TRUE .toString (),
118146 randomIntBetween (Integer .MIN_VALUE , -1 ),
119- maybe -> assertThat (maybe , OptionalMatchers .isEmpty ()));
147+ ( maybe , plugin ) -> assertThat (maybe , OptionalMatchers .isEmpty ()));
120148 }
121149
122150 public void testCloseNotEnabled () {
123151 runTestClose (
124152 Boolean .FALSE .toString (),
125153 randomInt (),
126- maybe -> assertThat (maybe , OptionalMatchers .isEmpty ()));
154+ ( maybe , plugin ) -> assertThat (maybe , OptionalMatchers .isEmpty ()));
127155 }
128156
129157 private void runTestClose (
130158 final String esSDNotify ,
131159 final int rc ,
132- final Consumer <Optional <Exception >> assertions ) {
160+ final BiConsumer <Optional <Exception >, SystemdPlugin > assertions ) {
133161 runTest (esSDNotify , rc , assertions , SystemdPlugin ::close , "STOPPING=1" );
134162 }
135163
136164 private void runTest (
137165 final String esSDNotify ,
138166 final int rc ,
139- final Consumer <Optional <Exception >> assertions ,
167+ final BiConsumer <Optional <Exception >, SystemdPlugin > assertions ,
140168 final CheckedConsumer <SystemdPlugin , IOException > invocation ,
141169 final String expectedState ) {
142170 final AtomicBoolean invoked = new AtomicBoolean ();
@@ -153,16 +181,22 @@ int sd_notify(final int unset_environment, final String state) {
153181 }
154182
155183 };
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+ }
156190
157191 boolean success = false ;
158192 try {
159193 invocation .accept (plugin );
160194 success = true ;
161195 } catch (final Exception e ) {
162- assertions .accept (Optional .of (e ));
196+ assertions .accept (Optional .of (e ), plugin );
163197 }
164198 if (success ) {
165- assertions .accept (Optional .empty ());
199+ assertions .accept (Optional .empty (), plugin );
166200 }
167201 if (Boolean .TRUE .toString ().equals (esSDNotify )) {
168202 assertTrue (invoked .get ());
0 commit comments