Browse Source

Test: move LongGCDisruption from Thread.getAllStackTraces() to Thread.enumerate()

To allow it to run without the "modifyThreadGroup" permission.
Boaz Leskes 10 years ago
parent
commit
39f86a1abe

+ 0 - 6
core/src/test/java/org/elasticsearch/discovery/DiscoveryWithServiceDisruptionsIT.java

@@ -71,7 +71,6 @@ import org.elasticsearch.transport.TransportRequest;
 import org.elasticsearch.transport.TransportRequestOptions;
 import org.elasticsearch.transport.TransportService;
 import org.junit.Before;
-import org.junit.BeforeClass;
 import org.junit.Test;
 
 import java.io.IOException;
@@ -101,11 +100,6 @@ public class DiscoveryWithServiceDisruptionsIT extends ESIntegTestCase {
         return discoveryConfig.nodeSettings(nodeOrdinal);
     }
 
-    @BeforeClass
-    public static void beforeClassDisruption() {
-        assumeTrue("test cannot run with security manager: does evil stuff with threads", System.getSecurityManager() == null);
-    }
-
     @Before
     public void clearConfig() {
         discoveryConfig = null;

+ 12 - 4
core/src/test/java/org/elasticsearch/test/disruption/LongGCDisruption.java

@@ -28,8 +28,6 @@ import java.util.Random;
 import java.util.Set;
 import java.util.regex.Pattern;
 
-import static org.junit.Assert.assertFalse;
-
 /**
  * Suspends all threads on the specified node in order to simulate a long gc.
  */
@@ -79,10 +77,20 @@ public class LongGCDisruption extends SingleNodeDisruption {
 
     @SuppressForbidden(reason = "stops/resumes threads intentionally")
     protected boolean stopNodeThreads(String node, Set<Thread> nodeThreads) {
-        Set<Thread> allThreadsSet = Thread.getAllStackTraces().keySet();
+        Thread[] allThreads = null;
+        while (allThreads == null) {
+            allThreads = new Thread[Thread.activeCount()];
+            if (Thread.enumerate(allThreads) > allThreads.length) {
+                // we didn't make enough space, retry
+                allThreads = null;
+            }
+        }
         boolean stopped = false;
         final String nodeThreadNamePart = "[" + node + "]";
-        for (Thread thread : allThreadsSet) {
+        for (Thread thread : allThreads) {
+            if (thread == null) {
+                continue;
+            }
             String name = thread.getName();
             if (name.contains(nodeThreadNamePart)) {
                 if (thread.isAlive() && nodeThreads.add(thread)) {