Repository: incubator-eagle
Updated Branches:
  refs/heads/master cfd5c7f82 -> e6c77f762


[EAGLE-559] Add AlertServiceTestBase for safely starting embedded zookeeper, 
coordinator and alert service for common test purpose

https://issues.apache.org/jira/browse/EAGLE-559

Add AlertServiceTestBase for safely starting embedded zookeeper, coordinator 
and alert service for common test purpose

Author: Hao Chen <h...@apache.org>

Closes #446 from haoch/EAGLE-559.


Project: http://git-wip-us.apache.org/repos/asf/incubator-eagle/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-eagle/commit/e6c77f76
Tree: http://git-wip-us.apache.org/repos/asf/incubator-eagle/tree/e6c77f76
Diff: http://git-wip-us.apache.org/repos/asf/incubator-eagle/diff/e6c77f76

Branch: refs/heads/master
Commit: e6c77f762088f36d4e948785cc16dc9581d7a620
Parents: cfd5c7f
Author: Hao Chen <h...@apache.org>
Authored: Thu Sep 22 21:04:23 2016 +0800
Committer: Hao Chen <h...@apache.org>
Committed: Thu Sep 22 21:04:23 2016 +0800

----------------------------------------------------------------------
 .../eagle-alert/alert-service/pom.xml           |  10 ++
 .../eagle/service/app/AlertServiceTestBase.java | 107 ++++++++++++++++
 .../eagle/service/app/TestServiceAppWithZk.java | 123 -------------------
 .../service/app/TestServiceTestAppWithZk.java   |  65 ++++++++++
 .../src/test/resources/configuration.yml        |  22 ++++
 eagle-server/pom.xml                            |   6 +
 pom.xml                                         |  23 ++--
 7 files changed, 217 insertions(+), 139 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-eagle/blob/e6c77f76/eagle-core/eagle-alert-parent/eagle-alert/alert-service/pom.xml
----------------------------------------------------------------------
diff --git a/eagle-core/eagle-alert-parent/eagle-alert/alert-service/pom.xml 
b/eagle-core/eagle-alert-parent/eagle-alert/alert-service/pom.xml
index 7764ccf..e3d3c4c 100644
--- a/eagle-core/eagle-alert-parent/eagle-alert/alert-service/pom.xml
+++ b/eagle-core/eagle-alert-parent/eagle-alert/alert-service/pom.xml
@@ -81,6 +81,16 @@
             </exclusions>
         </dependency>
         <dependency>
+            <groupId>io.dropwizard</groupId>
+            <artifactId>dropwizard-testing</artifactId>
+            <exclusions>
+                <exclusion>
+                    <groupId>org.slf4j</groupId>
+                    <artifactId>slf4j-log4j12</artifactId>
+                </exclusion>
+            </exclusions>
+        </dependency>
+        <dependency>
             <groupId>io.swagger</groupId>
             <artifactId>swagger-jersey-jaxrs</artifactId>
             <scope>compile</scope>

http://git-wip-us.apache.org/repos/asf/incubator-eagle/blob/e6c77f76/eagle-core/eagle-alert-parent/eagle-alert/alert-service/src/test/java/com/apache/eagle/service/app/AlertServiceTestBase.java
----------------------------------------------------------------------
diff --git 
a/eagle-core/eagle-alert-parent/eagle-alert/alert-service/src/test/java/com/apache/eagle/service/app/AlertServiceTestBase.java
 
b/eagle-core/eagle-alert-parent/eagle-alert/alert-service/src/test/java/com/apache/eagle/service/app/AlertServiceTestBase.java
new file mode 100644
index 0000000..3f6ae0b
--- /dev/null
+++ 
b/eagle-core/eagle-alert-parent/eagle-alert/alert-service/src/test/java/com/apache/eagle/service/app/AlertServiceTestBase.java
@@ -0,0 +1,107 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.apache.eagle.service.app;
+
+import org.apache.eagle.alert.utils.ZookeeperEmbedded;
+import org.apache.eagle.service.app.ServiceApp;
+import org.junit.AfterClass;
+import org.junit.BeforeClass;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.concurrent.atomic.AtomicInteger;
+
+/**
+ * Start embedded zookeeper, coordinator and alert service.
+ */
+public class AlertServiceTestBase {
+    private static final Logger LOG = 
LoggerFactory.getLogger(AlertServiceTestBase.class);
+
+    private static ZookeeperEmbedded zkEmbed;
+    private static int zkPort;
+    private static int serverPort;
+    private static Thread serverThread;
+
+    @BeforeClass
+    public static void startZookeeperAndServiceServer() throws Throwable {
+        zkEmbed = new ZookeeperEmbedded(2181);
+        zkPort = zkEmbed.start();
+        System.setProperty("coordinator.zkConfig.zkQuorum", "localhost:" + 
zkPort);
+
+        System.setProperty("dw.server.applicationConnectors[0].port", "7777");
+        System.setProperty("dw.server.adminConnectors[0].port", "7771");
+        serverPort = tryStartServerApp(8080);
+
+        LOG.info("Started Zookeeper on port {} and Service on port {}", 
zkPort, serverPort);
+    }
+
+    /**
+     * @param initialPort initial server port
+     * @return bind server port
+     * @throws Exception throws if failed more then 3 times.
+     */
+    private static int tryStartServerApp(int initialPort) throws Throwable {
+        final AtomicInteger port = new AtomicInteger(initialPort);
+        final AtomicBoolean success = new AtomicBoolean(false);
+        serverThread = new Thread(() -> {
+            String userDir = System.getProperty("user.dir");
+            String serverConf = userDir + 
"/src/test/resources/configuration.yml";
+            Throwable lastException = null;
+            int tried = 0;
+            while (!success.get() && tried < 3) {
+                LOG.info("Starting server in port {}", port);
+                System.setProperty("dw.server.applicationConnectors[0].port", 
String.valueOf(port.intValue()));
+                System.setProperty("dw.server.adminConnectors[0].port", 
String.valueOf(port.intValue() + 1));
+                try {
+                    new ServiceApp().run(new String[] {"server", serverConf});
+                    success.set(true);
+                } catch (Throwable e) {
+                    LOG.warn(e.getMessage(), e);
+                    tried++;
+                    port.incrementAndGet();
+                    lastException = e;
+                }
+            }
+            if (!success.get()) {
+                LOG.error("Failed to start server after tried 3 times");
+                throw new IllegalStateException(lastException);
+            }
+        });
+        serverThread.start();
+        serverThread.join();
+        return port.get();
+    }
+
+    public static int getBindServerPort() {
+        return serverPort;
+    }
+
+    public static int getBindZkPort() {
+        return zkPort;
+    }
+
+    @AfterClass
+    public static void shutdownZookeeperAndServiceServer() throws Exception {
+        if (zkEmbed != null) {
+            zkEmbed.shutdown();
+        }
+        if (serverThread != null && serverThread.isAlive()) {
+            serverThread.interrupt();
+        }
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-eagle/blob/e6c77f76/eagle-core/eagle-alert-parent/eagle-alert/alert-service/src/test/java/com/apache/eagle/service/app/TestServiceAppWithZk.java
----------------------------------------------------------------------
diff --git 
a/eagle-core/eagle-alert-parent/eagle-alert/alert-service/src/test/java/com/apache/eagle/service/app/TestServiceAppWithZk.java
 
b/eagle-core/eagle-alert-parent/eagle-alert/alert-service/src/test/java/com/apache/eagle/service/app/TestServiceAppWithZk.java
deleted file mode 100644
index f7f6c57..0000000
--- 
a/eagle-core/eagle-alert-parent/eagle-alert/alert-service/src/test/java/com/apache/eagle/service/app/TestServiceAppWithZk.java
+++ /dev/null
@@ -1,123 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements.  See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License.  You may obtain a copy of the License at
- *
- *    http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package com.apache.eagle.service.app;
-
-import java.io.BufferedReader;
-import java.io.ByteArrayOutputStream;
-import java.io.IOException;
-import java.io.PrintStream;
-import java.io.StringReader;
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.List;
-
-import org.apache.eagle.alert.engine.coordinator.PolicyDefinition;
-import org.apache.eagle.alert.engine.coordinator.PolicyDefinition.Definition;
-import org.apache.eagle.alert.service.IMetadataServiceClient;
-import org.apache.eagle.alert.service.MetadataServiceClientImpl;
-import org.apache.eagle.alert.utils.ZookeeperEmbedded;
-import org.apache.eagle.service.app.ServiceApp;
-import org.junit.*;
-
-import com.google.common.base.Joiner;
-import com.typesafe.config.Config;
-import com.typesafe.config.ConfigFactory;
-
-public class TestServiceAppWithZk {
-
-    ZookeeperEmbedded zkEmbed;
-
-    PrintStream oldStream;
-    PrintStream newStream;
-    ByteArrayOutputStream newStreamOutput;
-
-    @Before
-    public void setUp() throws Exception {
-        // Create a stream to hold the output
-        newStreamOutput = new ByteArrayOutputStream();
-        newStream = new PrintStream(newStreamOutput);
-        // IMPORTANT: Save the old System.out!
-        oldStream = System.out;
-        // Tell Java to use your special stream
-        System.setOut(newStream);
-
-        zkEmbed = new ZookeeperEmbedded(2181);
-        zkEmbed.start();
-
-        Thread.sleep(2000);
-
-        new ServiceApp().run(new String[] {"server"});
-    }
-
-    @After
-    public void tearDown() throws Exception {
-        zkEmbed.shutdown();
-    }
-
-    @Test
-    public void testMain() throws Exception {
-        try {
-            Thread.sleep(15000);
-        } catch (InterruptedException e1) {
-        }
-
-        // Put things back
-        System.out.flush();
-        System.setOut(oldStream);
-
-        BufferedReader br = new BufferedReader(new 
StringReader(newStreamOutput.toString()));
-        List<String> logs = new ArrayList<String>();
-        String line = null;
-        while ((line = br.readLine()) != null) {
-            logs.add(line);
-        }
-
-        System.out.println(Joiner.on("\n").join(logs));
-
-        Assert.assertTrue(logs.stream().anyMatch((log) -> log.contains("this 
is leader node right now..")));
-        Assert.assertTrue(logs.stream().anyMatch((log) -> log.contains("start 
coordinator background tasks..")));
-
-        Config config = ConfigFactory.load().getConfig("coordinator");
-        // build dynamic policy loader
-        String host = config.getString("metadataService.host");
-        int port = config.getInt("metadataService.port");
-        String context = config.getString("metadataService.context");
-        IMetadataServiceClient client = new MetadataServiceClientImpl(host, 
port, context);
-
-        List<PolicyDefinition> policies = client.listPolicies();
-
-        Assert.assertEquals(0, policies.size());
-
-        PolicyDefinition def = new PolicyDefinition();
-        def.setName("test-policy-1");
-        def.setInputStreams(Arrays.asList("testStreamDef"));
-        def.setOutputStreams(Arrays.asList("test-datasource-1"));
-        def.setParallelismHint(5);
-        def.setDefinition(new Definition());
-        client.addPolicy(def);
-
-        policies = client.listPolicies();
-
-        Assert.assertEquals(1, policies.size());
-
-        try {
-            client.close();
-        } catch (IOException e) {
-        }
-    }
-
-}

http://git-wip-us.apache.org/repos/asf/incubator-eagle/blob/e6c77f76/eagle-core/eagle-alert-parent/eagle-alert/alert-service/src/test/java/com/apache/eagle/service/app/TestServiceTestAppWithZk.java
----------------------------------------------------------------------
diff --git 
a/eagle-core/eagle-alert-parent/eagle-alert/alert-service/src/test/java/com/apache/eagle/service/app/TestServiceTestAppWithZk.java
 
b/eagle-core/eagle-alert-parent/eagle-alert/alert-service/src/test/java/com/apache/eagle/service/app/TestServiceTestAppWithZk.java
new file mode 100644
index 0000000..f194588
--- /dev/null
+++ 
b/eagle-core/eagle-alert-parent/eagle-alert/alert-service/src/test/java/com/apache/eagle/service/app/TestServiceTestAppWithZk.java
@@ -0,0 +1,65 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.apache.eagle.service.app;
+
+import com.typesafe.config.Config;
+import com.typesafe.config.ConfigFactory;
+import org.apache.eagle.alert.engine.coordinator.PolicyDefinition;
+import org.apache.eagle.alert.engine.coordinator.PolicyDefinition.Definition;
+import org.apache.eagle.alert.service.IMetadataServiceClient;
+import org.apache.eagle.alert.service.MetadataServiceClientImpl;
+import org.junit.Assert;
+import org.junit.Test;
+
+import java.io.IOException;
+import java.util.Collections;
+import java.util.List;
+
+public class TestServiceTestAppWithZk extends AlertServiceTestBase {
+    @Test
+    public void testMain() throws Exception {
+        System.setProperty("coordinator.zkConfig.zkQuorum", "localhost:" + 
getBindZkPort());
+        Config config = ConfigFactory.load().getConfig("coordinator");
+        // build dynamic policy loader
+        String host = config.getString("metadataService.host");
+        String context = config.getString("metadataService.context");
+        IMetadataServiceClient client = new MetadataServiceClientImpl(host, 
getBindServerPort(), context);
+
+        List<PolicyDefinition> policies = client.listPolicies();
+
+        Assert.assertEquals(0, policies.size());
+
+        PolicyDefinition def = new PolicyDefinition();
+        def.setName("test-policy-1");
+        def.setInputStreams(Collections.singletonList("testStreamDef"));
+        def.setOutputStreams(Collections.singletonList("test-datasource-1"));
+        def.setParallelismHint(5);
+        def.setDefinition(new Definition());
+        client.addPolicy(def);
+
+        policies = client.listPolicies();
+
+        Assert.assertEquals(1, policies.size());
+
+        try {
+            client.close();
+        } catch (IOException e) {
+            // ignore
+        }
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/incubator-eagle/blob/e6c77f76/eagle-core/eagle-alert-parent/eagle-alert/alert-service/src/test/resources/configuration.yml
----------------------------------------------------------------------
diff --git 
a/eagle-core/eagle-alert-parent/eagle-alert/alert-service/src/test/resources/configuration.yml
 
b/eagle-core/eagle-alert-parent/eagle-alert/alert-service/src/test/resources/configuration.yml
new file mode 100644
index 0000000..6dc864d
--- /dev/null
+++ 
b/eagle-core/eagle-alert-parent/eagle-alert/alert-service/src/test/resources/configuration.yml
@@ -0,0 +1,22 @@
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License.  You may obtain a copy of the License at
+#
+#    http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+server:
+  applicationConnectors:
+    - type: http
+      port: 9090
+  adminConnectors:
+    - type: http
+      port: 9091
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/incubator-eagle/blob/e6c77f76/eagle-server/pom.xml
----------------------------------------------------------------------
diff --git a/eagle-server/pom.xml b/eagle-server/pom.xml
index 0413b34..31b219d 100644
--- a/eagle-server/pom.xml
+++ b/eagle-server/pom.xml
@@ -124,6 +124,12 @@
             <groupId>org.apache.eagle</groupId>
             <artifactId>eagle-common</artifactId>
             <version>${project.version}</version>
+            <exclusions>
+                <exclusion>
+                    <groupId>org.wso2.orbit.com.lmax</groupId>
+                    <artifactId>disruptor</artifactId>
+                </exclusion>
+            </exclusions>
         </dependency>
         <dependency>
             <groupId>org.apache.eagle</groupId>

http://git-wip-us.apache.org/repos/asf/incubator-eagle/blob/e6c77f76/pom.xml
----------------------------------------------------------------------
diff --git a/pom.xml b/pom.xml
index 0885375..fd8cb52 100755
--- a/pom.xml
+++ b/pom.xml
@@ -22,7 +22,7 @@
         <groupId>org.apache</groupId>
         <artifactId>apache</artifactId>
         <version>14</version>
-    </parent> 
+    </parent>
     <groupId>org.apache.eagle</groupId>
     <artifactId>eagle-parent</artifactId>
     <version>0.5.0-incubating-SNAPSHOT</version>
@@ -303,7 +303,7 @@
         
<metrics-elasticsearch-reporter.version>2.2.0</metrics-elasticsearch-reporter.version>
 
         <common.cli.version>1.3.1</common.cli.version>
-           <ullink.slack.version>0.6.0</ullink.slack.version>
+        <ullink.slack.version>0.6.0</ullink.slack.version>
 
         <!-- dropwizard -->
         <dropwizard.version>0.7.1</dropwizard.version>
@@ -522,16 +522,6 @@
                 <artifactId>json</artifactId>
                 <version>${org.json.version}</version>
             </dependency>
-            <!--<dependency>-->
-                <!--<groupId>org.codehaus.jackson</groupId>-->
-                <!--<artifactId>jackson-jaxrs</artifactId>-->
-                <!--<version>${codehaus-jackson.version}</version>-->
-            <!--</dependency>-->
-            <!--<dependency>-->
-                <!--<groupId>org.codehaus.jackson</groupId>-->
-                <!--<artifactId>jackson-xc</artifactId>-->
-                <!--<version>${codehaus-jackson.version}</version>-->
-            <!--</dependency>-->
             <dependency>
                 <groupId>com.google.code.gson</groupId>
                 <artifactId>gson</artifactId>
@@ -846,6 +836,11 @@
                 <version>${dropwizard.version}</version>
             </dependency>
             <dependency>
+                <groupId>io.dropwizard</groupId>
+                <artifactId>dropwizard-testing</artifactId>
+                <version>${dropwizard.version}</version>
+            </dependency>
+            <dependency>
                 <groupId>com.hubspot.dropwizard</groupId>
                 <artifactId>dropwizard-guice</artifactId>
                 <version>${dropwizard-guice.version}</version>
@@ -970,9 +965,6 @@
 
         <profile>
             <id>mapr</id>
-            <!--<activation>-->
-            <!--<activeByDefault>true</activeByDefault>-->
-            <!--</activation>-->
             <repositories>
                 <repository>
                     <id>mapr-releases</id>
@@ -1230,7 +1222,6 @@
                                 <!-- External dependency script -->
                                 
<exclude>**/resource/serf/etc/ambari.json</exclude>
 
-                                <!-- TODO: fix it -->
                                 <exclude>**/webapp/**</exclude>
                                 <exclude>**/*.pid</exclude>
                             </excludes>

Reply via email to