1717import org .elasticsearch .cluster .ClusterName ;
1818import org .elasticsearch .cluster .ClusterState ;
1919import org .elasticsearch .cluster .NodeConnectionsService ;
20- import org .elasticsearch .cluster .coordination .FailedToCommitClusterStateException ;
2120import org .elasticsearch .cluster .metadata .Metadata ;
2221import org .elasticsearch .cluster .metadata .ReservedStateMetadata ;
2322import org .elasticsearch .cluster .node .DiscoveryNode ;
6968
7069import static java .nio .file .StandardCopyOption .ATOMIC_MOVE ;
7170import static java .nio .file .StandardCopyOption .REPLACE_EXISTING ;
71+ import static org .elasticsearch .health .HealthStatus .GREEN ;
72+ import static org .elasticsearch .health .HealthStatus .YELLOW ;
7273import static org .elasticsearch .node .Node .NODE_NAME_SETTING ;
7374import static org .hamcrest .Matchers .anEmptyMap ;
7475import static org .hamcrest .Matchers .hasEntry ;
8283import static org .mockito .Mockito .spy ;
8384import static org .mockito .Mockito .times ;
8485import static org .mockito .Mockito .verify ;
85- import static org .mockito .Mockito .verifyNoInteractions ;
86- import static org .mockito .Mockito .verifyNoMoreInteractions ;
8786
8887public class FileSettingsServiceTests extends ESTestCase {
8988 private static final Logger logger = LogManager .getLogger (FileSettingsServiceTests .class );
@@ -138,7 +137,7 @@ public void setUp() throws Exception {
138137 List .of (new ReservedClusterSettingsAction (clusterSettings ))
139138 )
140139 );
141- healthIndicatorService = mock ( FileSettingsHealthIndicatorService . class );
140+ healthIndicatorService = spy ( new FileSettingsHealthIndicatorService () );
142141 fileSettingsService = spy (new FileSettingsService (clusterService , controller , env , healthIndicatorService ));
143142 }
144143
@@ -170,7 +169,8 @@ public void testStartStop() {
170169 assertTrue (fileSettingsService .watching ());
171170 fileSettingsService .stop ();
172171 assertFalse (fileSettingsService .watching ());
173- verifyNoInteractions (healthIndicatorService );
172+ verify (healthIndicatorService , times (1 )).startOccurred ();
173+ verify (healthIndicatorService , times (1 )).stopOccurred ();
174174 }
175175
176176 public void testOperatorDirName () {
@@ -218,9 +218,9 @@ public void testInitialFileError() throws Exception {
218218 // assert we never notified any listeners of successful application of file based settings
219219 assertFalse (settingsChanged .get ());
220220
221+ assertEquals (YELLOW , healthIndicatorService .calculate (false , null ).status ());
221222 verify (healthIndicatorService , times (1 )).changeOccurred ();
222223 verify (healthIndicatorService , times (1 )).failureOccurred (argThat (s -> s .startsWith (IllegalStateException .class .getName ())));
223- verifyNoMoreInteractions (healthIndicatorService );
224224 }
225225
226226 @ SuppressWarnings ("unchecked" )
@@ -246,9 +246,9 @@ public void testInitialFileWorks() throws Exception {
246246 verify (fileSettingsService , times (1 )).processFileOnServiceStart ();
247247 verify (controller , times (1 )).process (any (), any (XContentParser .class ), eq (ReservedStateVersionCheck .HIGHER_OR_SAME_VERSION ), any ());
248248
249+ assertEquals (GREEN , healthIndicatorService .calculate (false , null ).status ());
249250 verify (healthIndicatorService , times (1 )).changeOccurred ();
250251 verify (healthIndicatorService , times (1 )).successOccurred ();
251- verifyNoMoreInteractions (healthIndicatorService );
252252 }
253253
254254 @ SuppressWarnings ("unchecked" )
@@ -285,9 +285,9 @@ public void testProcessFileChanges() throws Exception {
285285 verify (fileSettingsService , times (1 )).processFileChanges ();
286286 verify (controller , times (1 )).process (any (), any (XContentParser .class ), eq (ReservedStateVersionCheck .HIGHER_VERSION_ONLY ), any ());
287287
288+ assertEquals (GREEN , healthIndicatorService .calculate (false , null ).status ());
288289 verify (healthIndicatorService , times (2 )).changeOccurred ();
289290 verify (healthIndicatorService , times (2 )).successOccurred ();
290- verifyNoMoreInteractions (healthIndicatorService );
291291 }
292292
293293 public void testInvalidJSON () throws Exception {
@@ -323,6 +323,7 @@ public void testInvalidJSON() throws Exception {
323323 // referring to fileSettingsService.start(). Rather, it is referring to the initialization
324324 // of the watcher thread itself, which occurs asynchronously when clusterChanged is first called.
325325
326+ assertEquals (YELLOW , healthIndicatorService .calculate (false , null ).status ());
326327 verify (healthIndicatorService ).failureOccurred (contains (XContentParseException .class .getName ()));
327328 }
328329
@@ -388,14 +389,13 @@ public void testStopWorksInMiddleOfProcessing() throws Exception {
388389 fileSettingsService .stop ();
389390 assertFalse (fileSettingsService .watching ());
390391 fileSettingsService .close ();
392+
393+ // When the service is stopped, the health indicator should be green
394+ assertEquals (GREEN , healthIndicatorService .calculate (false , null ).status ());
395+ verify (healthIndicatorService ).stopOccurred ();
396+
391397 // let the deadlocked thread end, so we can cleanly exit the test
392398 deadThreadLatch .countDown ();
393-
394- verify (healthIndicatorService , times (1 )).changeOccurred ();
395- verify (healthIndicatorService , times (1 )).failureOccurred (
396- argThat (s -> s .startsWith (FailedToCommitClusterStateException .class .getName ()))
397- );
398- verifyNoMoreInteractions (healthIndicatorService );
399399 }
400400
401401 public void testHandleSnapshotRestoreClearsMetadata () throws Exception {
0 commit comments