]> source.dussan.org Git - sonarqube.git/commitdiff
Use the shared Elasticsearch instance in EsTester
authorSimon Brandhof <simon.brandhof@sonarsource.com>
Sun, 30 Nov 2014 18:25:30 +0000 (19:25 +0100)
committerSimon Brandhof <simon.brandhof@sonarsource.com>
Sun, 30 Nov 2014 18:25:30 +0000 (19:25 +0100)
server/sonar-server/src/test/java/org/sonar/server/es/EsServerHolder.java
server/sonar-server/src/test/java/org/sonar/server/es/EsTester.java

index 28810012ff5ffb64746a31e3a41b122f2b1e7f72..7ca2f18031ae208c4e0cb5efe40bbbf177f036c6 100644 (file)
@@ -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-");
index ad08d619827cc6ea80dacb32c70aa54b8d81ce3f..9c95a9312e6a2dd0f1a241ac589b3d34ea28cf50 100644 (file)
@@ -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<IndexDefinition> 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;
   }