From: Simon Brandhof Date: Sun, 30 Nov 2014 18:25:30 +0000 (+0100) Subject: Use the shared Elasticsearch instance in EsTester X-Git-Tag: 5.0-RC1~146 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=6516646e2f0b558ef81f8be705ae0c1764c0eb17;p=sonarqube.git Use the shared Elasticsearch instance in EsTester --- diff --git a/server/sonar-server/src/test/java/org/sonar/server/es/EsServerHolder.java b/server/sonar-server/src/test/java/org/sonar/server/es/EsServerHolder.java index 28810012ff5..7ca2f18031a 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/es/EsServerHolder.java +++ b/server/sonar-server/src/test/java/org/sonar/server/es/EsServerHolder.java @@ -64,12 +64,7 @@ public class EsServerHolder { } private void reset() { - TransportClient client = new TransportClient(ImmutableSettings.settingsBuilder() - .put("node.name", nodeName) - .put("network.bind_host", "localhost") - .put("cluster.name", clusterName) - .build()); - client.addTransportAddress(new InetSocketTransportAddress(LoopbackAddress.get().getHostAddress(), port)); + TransportClient client = newClient(); // wait for node to be ready client.admin().cluster().prepareHealth() @@ -84,6 +79,16 @@ public class EsServerHolder { client.close(); } + public TransportClient newClient() { + TransportClient client = new TransportClient(ImmutableSettings.settingsBuilder() + .put("node.name", nodeName) + .put("network.bind_host", "localhost") + .put("cluster.name", clusterName) + .build()); + client.addTransportAddress(new InetSocketTransportAddress(LoopbackAddress.get().getHostAddress(), port)); + return client; + } + public static synchronized EsServerHolder get() { if (HOLDER == null) { File homeDir = TestUtils.newTempDir("tmp-es-"); diff --git a/server/sonar-server/src/test/java/org/sonar/server/es/EsTester.java b/server/sonar-server/src/test/java/org/sonar/server/es/EsTester.java index ad08d619827..9c95a9312e6 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/es/EsTester.java +++ b/server/sonar-server/src/test/java/org/sonar/server/es/EsTester.java @@ -25,21 +25,15 @@ import com.google.common.collect.Collections2; import com.google.common.collect.Iterables; import com.google.common.collect.Lists; import org.apache.commons.io.IOUtils; -import org.apache.commons.lang.math.RandomUtils; import org.apache.commons.lang.reflect.ConstructorUtils; -import org.elasticsearch.action.admin.indices.delete.DeleteIndexResponse; import org.elasticsearch.action.bulk.BulkRequestBuilder; import org.elasticsearch.action.index.IndexRequest; import org.elasticsearch.action.search.SearchRequestBuilder; import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.action.search.SearchType; -import org.elasticsearch.cluster.metadata.IndexMetaData; -import org.elasticsearch.cluster.node.DiscoveryNode; -import org.elasticsearch.common.settings.ImmutableSettings; +import org.elasticsearch.client.transport.TransportClient; import org.elasticsearch.common.unit.TimeValue; import org.elasticsearch.index.query.QueryBuilders; -import org.elasticsearch.node.Node; -import org.elasticsearch.node.NodeBuilder; import org.elasticsearch.search.SearchHit; import org.junit.rules.ExternalResource; import org.sonar.api.config.Settings; @@ -52,14 +46,11 @@ import java.io.FileInputStream; import java.util.Collections; import java.util.List; -import static org.fest.assertions.Assertions.assertThat; - public class EsTester extends ExternalResource { - private static final int INSTANCE_ID = RandomUtils.nextInt(); - private Node node; private EsClient client; private final List definitions = Lists.newArrayList(); + private TransportClient transportClient; public EsTester addDefinitions(IndexDefinition... defs) { Collections.addAll(definitions, defs); @@ -67,36 +58,9 @@ public class EsTester extends ExternalResource { } protected void before() throws Throwable { - String nodeName = "tmp-es-" + INSTANCE_ID; - node = NodeBuilder.nodeBuilder().local(true).data(true).settings(ImmutableSettings.builder() - .put("cluster.name", nodeName) - .put("node.name", nodeName) - // the two following properties are probably not used because they are - // declared on indices too - .put(IndexMetaData.SETTING_NUMBER_OF_SHARDS, 1) - .put(IndexMetaData.SETTING_NUMBER_OF_REPLICAS, 0) - // limit the number of threads created (see org.elasticsearch.common.util.concurrent.EsExecutors) - .put("processors", 1) - .put("http.enabled", false) - .put("index.store.type", "mmapfs") - .put("config.ignore_system_properties", true) - // reuse the same directory than other tests for faster initialization - .put("path.home", "target/" + nodeName) - .put("gateway.type", "none")) - .build(); - node.start(); - assertThat(DiscoveryNode.localNode(node.settings())).isTrue(); - - // wait for node to be ready - node.client().admin().cluster().prepareHealth() - .setWaitForGreenStatus() - .get(); - - // delete the indices created by previous tests - DeleteIndexResponse response = node.client().admin().indices().prepareDelete("_all").get(); - assertThat(response.isAcknowledged()).isTrue(); - - client = new EsClient(new Profiling(new Settings()), node.client()); + EsServerHolder holder = EsServerHolder.get(); + transportClient = holder.newClient(); + client = new EsClient(new Profiling(new Settings()), transportClient); client.start(); if (!definitions.isEmpty()) { @@ -111,12 +75,14 @@ public class EsTester extends ExternalResource { @Override protected void after() { + if (transportClient != null) { + // TODO to be removed as soon as EsClient.stop() is implemented correctly + transportClient.close(); + transportClient = null; + } if (client != null) { client.stop(); - } - if (node != null) { - node.stop(); - node.close(); + client = null; } } @@ -187,10 +153,6 @@ public class EsTester extends ExternalResource { return result; } - public Node node() { - return node; - } - public EsClient client() { return client; }