@@ -19,16 +19,12 @@ public class TestClusterCleanupOnShutdown implements Runnable {
19
19
20
20
private Set <ElasticsearchCluster > clustersToWatch = new HashSet <>();
21
21
22
- public void watch (Collection <ElasticsearchCluster > cluster ) {
23
- synchronized (clustersToWatch ) {
24
- clustersToWatch .addAll (clustersToWatch );
25
- }
22
+ public synchronized void watch (Collection <ElasticsearchCluster > clusters ) {
23
+ clustersToWatch .addAll (clusters );
26
24
}
27
25
28
- public void unWatch (Collection <ElasticsearchCluster > cluster ) {
29
- synchronized (clustersToWatch ) {
30
- clustersToWatch .removeAll (clustersToWatch );
31
- }
26
+ public synchronized void unWatch (Collection <ElasticsearchCluster > clusters ) {
27
+ clustersToWatch .removeAll (clusters );
32
28
}
33
29
34
30
@ Override
@@ -38,21 +34,23 @@ public void run() {
38
34
Thread .sleep (Long .MAX_VALUE );
39
35
}
40
36
} catch (InterruptedException interrupted ) {
41
- synchronized (clustersToWatch ) {
42
- if (clustersToWatch .isEmpty ()) {
43
- return ;
44
- }
45
- logger .info ("Cleanup thread was interrupted, shutting down all clusters" );
46
- Iterator <ElasticsearchCluster > iterator = clustersToWatch .iterator ();
47
- while (iterator .hasNext ()) {
48
- ElasticsearchCluster cluster = iterator .next ();
49
- iterator .remove ();
50
- try {
51
- cluster .stop (false );
52
- } catch (Exception e ) {
53
- logger .warn ("Could not shut down {}" , cluster , e );
54
- }
55
- }
37
+ shutdownClusters ();
38
+ }
39
+ }
40
+
41
+ public synchronized void shutdownClusters () {
42
+ if (clustersToWatch .isEmpty ()) {
43
+ return ;
44
+ }
45
+ logger .info ("Cleanup thread was interrupted, shutting down all clusters" );
46
+ Iterator <ElasticsearchCluster > iterator = clustersToWatch .iterator ();
47
+ while (iterator .hasNext ()) {
48
+ ElasticsearchCluster cluster = iterator .next ();
49
+ iterator .remove ();
50
+ try {
51
+ cluster .stop (false );
52
+ } catch (Exception e ) {
53
+ logger .warn ("Could not shut down {}" , cluster , e );
56
54
}
57
55
}
58
56
}
0 commit comments