james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From btell...@apache.org
Subject svn commit: r1688130 - in /james/mailbox/trunk/elasticsearch/src/test/java/org/apache/james/mailbox/elasticsearch: ElasticSearchIndexerTest.java EmbeddedElasticSearch.java
Date Mon, 29 Jun 2015 08:34:41 GMT
Author: btellier
Date: Mon Jun 29 08:34:40 2015
New Revision: 1688130

URL: http://svn.apache.org/r1688130
Log:
MAILBOX-155 Factorize test utils and increase Time out for tests

Modified:
    james/mailbox/trunk/elasticsearch/src/test/java/org/apache/james/mailbox/elasticsearch/ElasticSearchIndexerTest.java
    james/mailbox/trunk/elasticsearch/src/test/java/org/apache/james/mailbox/elasticsearch/EmbeddedElasticSearch.java

Modified: james/mailbox/trunk/elasticsearch/src/test/java/org/apache/james/mailbox/elasticsearch/ElasticSearchIndexerTest.java
URL: http://svn.apache.org/viewvc/james/mailbox/trunk/elasticsearch/src/test/java/org/apache/james/mailbox/elasticsearch/ElasticSearchIndexerTest.java?rev=1688130&r1=1688129&r2=1688130&view=diff
==============================================================================
--- james/mailbox/trunk/elasticsearch/src/test/java/org/apache/james/mailbox/elasticsearch/ElasticSearchIndexerTest.java
(original)
+++ james/mailbox/trunk/elasticsearch/src/test/java/org/apache/james/mailbox/elasticsearch/ElasticSearchIndexerTest.java
Mon Jun 29 08:34:40 2015
@@ -16,16 +16,15 @@
  * specific language governing permissions and limitations      *
  * under the License.                                           *
  ****************************************************************/
+
 package org.apache.james.mailbox.elasticsearch;
 
-import static com.jayway.awaitility.Awaitility.await;
 import static org.assertj.core.api.Assertions.assertThat;
-import static org.elasticsearch.node.NodeBuilder.nodeBuilder;
 
-import org.elasticsearch.action.admin.indices.flush.FlushRequestBuilder;
+import java.io.IOException;
+
 import org.elasticsearch.action.search.SearchResponse;
 import org.elasticsearch.client.Client;
-import org.elasticsearch.common.settings.ImmutableSettings;
 import org.elasticsearch.index.query.QueryBuilders;
 import org.elasticsearch.node.Node;
 import org.junit.After;
@@ -34,9 +33,6 @@ import org.junit.Rule;
 import org.junit.Test;
 import org.junit.rules.TemporaryFolder;
 
-import com.jayway.awaitility.Duration;
-
-
 public class ElasticSearchIndexerTest {
 
     @Rule public TemporaryFolder temporaryFolder = new TemporaryFolder();
@@ -45,22 +41,14 @@ public class ElasticSearchIndexerTest {
     private ElasticSearchIndexer testee;
 
     @Before
-    public void setup() throws Exception {
-        node = nodeBuilder().local(true)
-                .settings(ImmutableSettings.builder()
-                        .put("path.data", temporaryFolder.newFolder().getAbsolutePath())
-                        .put("script.disable_dynamic",false)
-                        .build())
-                .node();
-        node.start();
-        awaitForElasticSearch();
-        
+    public void setup() throws IOException {
+        node = EmbeddedElasticSearch.provideNode(temporaryFolder);
         testee = new ElasticSearchIndexer(node);
     }
     
     @After
     public void tearDown() {
-        node.close();
+        EmbeddedElasticSearch.shutDown(node);
     }
     
     @Test
@@ -69,7 +57,7 @@ public class ElasticSearchIndexerTest {
         String content = "{\"message\": \"trying out Elasticsearch\"}";
         
         testee.indexMessage(messageId, content);
-        awaitForElasticSearch();
+        EmbeddedElasticSearch.awaitForElasticSearch(node);
         
         try (Client client = node.client()) {
             SearchResponse searchResponse = client.prepareSearch(ElasticSearchIndexer.MAILBOX_INDEX)
@@ -91,7 +79,7 @@ public class ElasticSearchIndexerTest {
         String content = "{\"message\": \"trying out Elasticsearch\",\"field\":\"Should be
unchanged\"}";
 
         testee.indexMessage(messageId, content);
-        awaitForElasticSearch();
+        EmbeddedElasticSearch.awaitForElasticSearch(node);
 
         testee.updateMessage(messageId, "{\"message\": \"mastering out Elasticsearch\"}");
         EmbeddedElasticSearch.awaitForElasticSearch(node);
@@ -115,6 +103,12 @@ public class ElasticSearchIndexerTest {
     
     @Test(expected=IllegalArgumentException.class)
     public void updateMessageShouldThrowWhenJsonIsNull() throws InterruptedException {
+        String messageId = "1:2";
+        String content = "{\"message\": \"trying out Elasticsearch\"}";
+
+        testee.indexMessage(messageId, content);
+        EmbeddedElasticSearch.awaitForElasticSearch(node);
+
         testee.updateMessage("1", null);
     }
     
@@ -124,10 +118,10 @@ public class ElasticSearchIndexerTest {
         String content = "{\"message\": \"trying out Elasticsearch\"}";
 
         testee.indexMessage(messageId, content);
-        awaitForElasticSearch();
+        EmbeddedElasticSearch.awaitForElasticSearch(node);
         
         testee.deleteAllWithIdStarting("1:");
-        awaitForElasticSearch();
+        EmbeddedElasticSearch.awaitForElasticSearch(node);
         
         try (Client client = node.client()) {
             SearchResponse searchResponse = client.prepareSearch(ElasticSearchIndexer.MAILBOX_INDEX)
@@ -144,22 +138,22 @@ public class ElasticSearchIndexerTest {
         String content = "{\"message\": \"trying out Elasticsearch\"}";
         
         testee.indexMessage(messageId, content);
-        awaitForElasticSearch();
+        EmbeddedElasticSearch.awaitForElasticSearch(node);
         
         String messageId2 = "1:2";
         String content2 = "{\"message\": \"trying out Elasticsearch 2\"}";
         
         testee.indexMessage(messageId2, content2);
-        awaitForElasticSearch();
+        EmbeddedElasticSearch.awaitForElasticSearch(node);
         
         String messageId3 = "2:3";
         String content3 = "{\"message\": \"trying out Elasticsearch 3\"}";
         
         testee.indexMessage(messageId3, content3);
-        awaitForElasticSearch();
+        EmbeddedElasticSearch.awaitForElasticSearch(node);
         
         testee.deleteAllWithIdStarting("1:");
-        awaitForElasticSearch();
+        EmbeddedElasticSearch.awaitForElasticSearch(node);
         
         try (Client client = node.client()) {
             SearchResponse searchResponse = client.prepareSearch(ElasticSearchIndexer.MAILBOX_INDEX)
@@ -172,14 +166,14 @@ public class ElasticSearchIndexerTest {
     
     @Test
     public void deleteMessage() throws Exception {
-        String messageId = "1";
+        String messageId = "1:2";
         String content = "{\"message\": \"trying out Elasticsearch\"}";
-        
+
         testee.indexMessage(messageId, content);
-        awaitForElasticSearch();
-        
+        EmbeddedElasticSearch.awaitForElasticSearch(node);
+
         testee.deleteMessage(messageId);
-        awaitForElasticSearch();
+        EmbeddedElasticSearch.awaitForElasticSearch(node);
         
         try (Client client = node.client()) {
             SearchResponse searchResponse = client.prepareSearch(ElasticSearchIndexer.MAILBOX_INDEX)
@@ -189,21 +183,5 @@ public class ElasticSearchIndexerTest {
             assertThat(searchResponse.getHits().getTotalHits()).isEqualTo(0);
         }
     }
-    
-    /**
-     * Sometimes, tests are too fast.
-     * This method ensure that ElasticSearch service is up and indices are updated
-     */
-    private void awaitForElasticSearch() {
-        await().atMost(Duration.ONE_SECOND).until(() -> flush());
-    }
-    
-    private boolean flush() {
-        try (Client client = node.client()) {
-            new FlushRequestBuilder(client.admin().indices()).setForce(true).get();
-            return true;
-        } catch (Exception e) {
-            return false;
-        }
-    }
+
 }

Modified: james/mailbox/trunk/elasticsearch/src/test/java/org/apache/james/mailbox/elasticsearch/EmbeddedElasticSearch.java
URL: http://svn.apache.org/viewvc/james/mailbox/trunk/elasticsearch/src/test/java/org/apache/james/mailbox/elasticsearch/EmbeddedElasticSearch.java?rev=1688130&r1=1688129&r2=1688130&view=diff
==============================================================================
--- james/mailbox/trunk/elasticsearch/src/test/java/org/apache/james/mailbox/elasticsearch/EmbeddedElasticSearch.java
(original)
+++ james/mailbox/trunk/elasticsearch/src/test/java/org/apache/james/mailbox/elasticsearch/EmbeddedElasticSearch.java
Mon Jun 29 08:34:40 2015
@@ -21,10 +21,11 @@ package org.apache.james.mailbox.elastic
 
 
 
+import static com.jayway.awaitility.Awaitility.await;
+import static org.elasticsearch.node.NodeBuilder.nodeBuilder;
+
 import java.io.IOException;
 
-import com.jayway.awaitility.Duration;
-import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
 import org.elasticsearch.action.admin.indices.flush.FlushRequestBuilder;
 import org.elasticsearch.client.Client;
 import org.elasticsearch.common.settings.ImmutableSettings;
@@ -34,8 +35,7 @@ import org.junit.rules.TemporaryFolder;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import static com.jayway.awaitility.Awaitility.await;
-import static org.elasticsearch.node.NodeBuilder.nodeBuilder;
+import com.jayway.awaitility.Duration;
 
 public class EmbeddedElasticSearch {
 
@@ -45,7 +45,6 @@ public class EmbeddedElasticSearch {
         Node node = nodeBuilder().local(true)
             .settings(ImmutableSettings.builder()
                 .put("path.data", temporaryFolder.newFolder().getAbsolutePath())
-                .put("script.disable_dynamic",true)
                 .build())
             .node();
         node.start();
@@ -56,11 +55,9 @@ public class EmbeddedElasticSearch {
     public static void shutDown(Node node) {
         EmbeddedElasticSearch.awaitForElasticSearch(node);
         try (Client client = node.client()) {
-            node.client()
-                .admin()
-                .indices()
-                .delete(new DeleteIndexRequest(ElasticSearchIndexer.MAILBOX_INDEX))
-                .actionGet();
+            client.prepareDeleteByQuery(ElasticSearchIndexer.MAILBOX_INDEX)
+                .setQuery(QueryBuilders.matchAllQuery())
+                .get();
         } catch (Exception e) {
             LOGGER.warn("Error while closing ES connection", e);
         }



---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org


Mime
View raw message