From 06fb9ff76117ca660eaee93e87f08cd3442b8ea8 Mon Sep 17 00:00:00 2001 From: Boaz Leskes Date: Sat, 23 Aug 2014 14:44:29 +0200 Subject: [PATCH] [Tests] verifyThreadNames should account for new threads of shared cluster The verifyThreadNames starts a node and checks that all new threads on the JVM are properly named. The current test uses the name of the new node which sometimes fails because our shared cluster spawns a new thread which is properly named but for not for the new name. The commits relaxes the requirement of the test and on verify the threads are properly named (but not necessarily of the new node) --- .../java/org/elasticsearch/test/InternalTestCluster.java | 9 +++++---- .../elasticsearch/threadpool/SimpleThreadPoolTests.java | 5 ++++- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/src/test/java/org/elasticsearch/test/InternalTestCluster.java b/src/test/java/org/elasticsearch/test/InternalTestCluster.java index 2f7cb1e4249..fdd345d1ab1 100644 --- a/src/test/java/org/elasticsearch/test/InternalTestCluster.java +++ b/src/test/java/org/elasticsearch/test/InternalTestCluster.java @@ -97,9 +97,7 @@ import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicInteger; -import static com.carrotsearch.randomizedtesting.RandomizedTest.frequently; -import static com.carrotsearch.randomizedtesting.RandomizedTest.isNightly; -import static com.carrotsearch.randomizedtesting.RandomizedTest.systemPropertyAsBoolean; +import static com.carrotsearch.randomizedtesting.RandomizedTest.*; import static junit.framework.Assert.fail; import static org.apache.lucene.util.LuceneTestCase.rarely; import static org.apache.lucene.util.LuceneTestCase.usually; @@ -139,7 +137,10 @@ public final class InternalTestCluster extends TestCluster { */ public static final String SETTING_CLUSTER_NODE_SEED = "test.cluster.node.seed"; - private static final String NODE_PREFIX = "node_"; + /** + * All nodes started by the cluster will have their name set to NODE_PREFIX followed by a positive number + */ + public static final String NODE_PREFIX = "node_"; private static final boolean ENABLE_MOCK_MODULES = systemPropertyAsBoolean(TESTS_ENABLE_MOCK_MODULES, true); diff --git a/src/test/java/org/elasticsearch/threadpool/SimpleThreadPoolTests.java b/src/test/java/org/elasticsearch/threadpool/SimpleThreadPoolTests.java index 58e257be7ba..ce70102315c 100644 --- a/src/test/java/org/elasticsearch/threadpool/SimpleThreadPoolTests.java +++ b/src/test/java/org/elasticsearch/threadpool/SimpleThreadPoolTests.java @@ -36,6 +36,7 @@ import org.elasticsearch.test.ElasticsearchIntegrationTest; import org.elasticsearch.test.ElasticsearchIntegrationTest.ClusterScope; import org.elasticsearch.test.ElasticsearchSingleNodeTest; import org.elasticsearch.test.InternalTestCluster; +import org.elasticsearch.test.hamcrest.RegexMatcher; import org.elasticsearch.threadpool.ThreadPool.Names; import org.junit.Test; @@ -46,6 +47,7 @@ import java.lang.management.ThreadMXBean; import java.util.Map; import java.util.Set; import java.util.concurrent.*; +import java.util.regex.Pattern; import static org.elasticsearch.common.settings.ImmutableSettings.settingsBuilder; import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder; @@ -113,7 +115,8 @@ public class SimpleThreadPoolTests extends ElasticsearchIntegrationTest { || threadName.contains("Keep-Alive-Timer")) { continue; } - assertThat(threadName, anyOf(containsString("[" + node + "]"), containsString("[" + InternalTestCluster.TRANSPORT_CLIENT_PREFIX + node + "]"))); + String nodePrefix = "(" + Pattern.quote(InternalTestCluster.TRANSPORT_CLIENT_PREFIX) + ")?" + Pattern.quote(InternalTestCluster.NODE_PREFIX); + assertThat(threadName, RegexMatcher.matches("\\[" + nodePrefix + "\\d+\\]")); } }