You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@usergrid.apache.org by sn...@apache.org on 2015/02/26 17:27:55 UTC

[31/50] [abbrv] incubator-usergrid git commit: Fixes startup order bug.

Fixes startup order bug.

Still broken.  Need to fix mock mail tests.


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

Branch: refs/heads/two-dot-o
Commit: ba7a11aee0c7cc2e66e90acfd6c5dc45f122ed09
Parents: e05800e
Author: Todd Nine <tn...@apigee.com>
Authored: Tue Feb 24 19:21:33 2015 -0700
Committer: Todd Nine <tn...@apigee.com>
Committed: Tue Feb 24 19:21:33 2015 -0700

----------------------------------------------------------------------
 .../apache/usergrid/rest/AbstractRestIT.java    |  14 +-
 .../java/org/apache/usergrid/rest/ITSetup.java  |  48 +----
 .../apache/usergrid/rest/TomcatResource.java    | 182 ------------------
 .../org/apache/usergrid/rest/TomcatRuntime.java | 192 +++++++++++++++++++
 .../rest/management/RegistrationIT.java         |   9 +-
 .../test/resource2point0/AbstractRestIT.java    |  12 +-
 6 files changed, 221 insertions(+), 236 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/ba7a11ae/stack/rest/src/test/java/org/apache/usergrid/rest/AbstractRestIT.java
----------------------------------------------------------------------
diff --git a/stack/rest/src/test/java/org/apache/usergrid/rest/AbstractRestIT.java b/stack/rest/src/test/java/org/apache/usergrid/rest/AbstractRestIT.java
index 15b2882..082e436 100644
--- a/stack/rest/src/test/java/org/apache/usergrid/rest/AbstractRestIT.java
+++ b/stack/rest/src/test/java/org/apache/usergrid/rest/AbstractRestIT.java
@@ -28,6 +28,9 @@ import com.sun.jersey.test.framework.JerseyTest;
 import com.sun.jersey.test.framework.WebAppDescriptor;
 import com.sun.jersey.test.framework.spi.container.TestContainerFactory;
 import org.apache.usergrid.java.client.Client;
+import org.apache.usergrid.management.ManagementService;
+import org.apache.usergrid.setup.ConcurrentProcessSingleton;
+
 import org.junit.AfterClass;
 import org.junit.Before;
 import org.junit.Rule;
@@ -64,6 +67,9 @@ public abstract class AbstractRestIT extends JerseyTest {
     private static final Logger LOG = LoggerFactory.getLogger( AbstractRestIT.class );
     private static boolean usersSetup = false;
 
+    protected static TomcatRuntime tomcatRuntime = TomcatRuntime.getInstance();
+
+    protected static final ITSetup setup = ITSetup.getInstance();
 
     private static ClientConfig clientConfig = new DefaultClientConfig();
 
@@ -75,10 +81,6 @@ public abstract class AbstractRestIT extends JerseyTest {
 
     protected static final AppDescriptor descriptor;
 
-    public static ITSetup setup = new ITSetup();
-
-    @Rule
-    public TomcatResource tomcatResource = new TomcatResource();
 
     //private static final URI baseURI = setup.getBaseURI();
 
@@ -186,7 +188,7 @@ public abstract class AbstractRestIT extends JerseyTest {
         setUserPassword( "ed@anuff.com", "sesame" );
 
         client = new Client( "test-organization", "test-app" ).withApiUrl(
-                UriBuilder.fromUri( "http://localhost/" ).port( setup.getTomcatPort() ).build().toString() );
+                UriBuilder.fromUri( "http://localhost/" ).port(tomcatRuntime.getPort() ).build().toString() );
 
         org.apache.usergrid.java.client.response.ApiResponse response =
                 client.authorizeAppUser( "ed@anuff.com", "sesame" );
@@ -206,7 +208,7 @@ public abstract class AbstractRestIT extends JerseyTest {
     @Override
     protected URI getBaseURI() {
         try {
-            return new URI("http://localhost:" + tomcatResource.getPort());
+            return new URI("http://localhost:" + tomcatRuntime.getPort());
         } catch (URISyntaxException e) {
             throw new RuntimeException("Error determining baseURI", e);
         }

http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/ba7a11ae/stack/rest/src/test/java/org/apache/usergrid/rest/ITSetup.java
----------------------------------------------------------------------
diff --git a/stack/rest/src/test/java/org/apache/usergrid/rest/ITSetup.java b/stack/rest/src/test/java/org/apache/usergrid/rest/ITSetup.java
index 7888061..510c992 100644
--- a/stack/rest/src/test/java/org/apache/usergrid/rest/ITSetup.java
+++ b/stack/rest/src/test/java/org/apache/usergrid/rest/ITSetup.java
@@ -35,45 +35,37 @@ import org.apache.usergrid.setup.ConcurrentProcessSingleton;
 /** A {@link org.junit.rules.TestRule} that sets up services. */
 public class ITSetup  {
 
-    private ServiceManagerFactory smf;
+    private static ITSetup instance;
+
     private EntityManagerFactory emf;
-    private ApplicationCreator applicationCreator;
-    private TokenService tokenService;
-    private SignInProviderFactory providerFactory;
     private Properties properties;
     private ManagementService managementService;
-    private TomcatResource tomcatResource;
 
-    private URI uri;
 
     private SpringResource springResource;
 
 
-    public ITSetup( ) {
+    private ITSetup( ) {
 
         this.springResource = ConcurrentProcessSingleton.getInstance().getSpringResource();
 
         emf =                springResource.getBean( EntityManagerFactory.class );
-        smf =                springResource.getBean( ServiceManagerFactory.class );
-        tokenService =       springResource.getBean( TokenService.class );
-        providerFactory =    springResource.getBean( SignInProviderFactory.class );
-        applicationCreator = springResource.getBean( ApplicationCreator.class );
         managementService =  springResource.getBean( ManagementService.class );
+        properties = springResource.getBean( "properties", Properties.class );
 
-        tomcatResource = TomcatResource.instance;
-        tomcatResource.setWebAppsPath( "src/main/webapp" );
 
-        // Initialize Jersey Client
-        //TODO, make this port a resource that's filtered by maven build for the port number
-        uri = UriBuilder.fromUri("http://localhost/").port( 8080 ).build();
     }
 
+    public static synchronized ITSetup getInstance(){
+        if(instance == null){
+            instance = new ITSetup();
+        }
 
-    public int getTomcatPort() {
-        return 8080;
+        return instance;
     }
 
 
+
     public ManagementService getMgmtSvc() {
         return managementService;
     }
@@ -83,28 +75,8 @@ public class ITSetup  {
         return emf;
     }
 
-
-    public ServiceManagerFactory getSmf() {
-        return smf;
-    }
-
-
-    public ApplicationCreator getAppCreator() {
-        return applicationCreator;
-    }
-
-
-    public TokenService getTokenSvc() {
-        return tokenService;
-    }
-
-
     public Properties getProps() {
         return properties;
     }
 
-
-    public SignInProviderFactory getProviderFactory() {
-        return providerFactory;
-    }
 }

http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/ba7a11ae/stack/rest/src/test/java/org/apache/usergrid/rest/TomcatResource.java
----------------------------------------------------------------------
diff --git a/stack/rest/src/test/java/org/apache/usergrid/rest/TomcatResource.java b/stack/rest/src/test/java/org/apache/usergrid/rest/TomcatResource.java
deleted file mode 100644
index 5f00f7b..0000000
--- a/stack/rest/src/test/java/org/apache/usergrid/rest/TomcatResource.java
+++ /dev/null
@@ -1,182 +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 org.apache.usergrid.rest;
-
-import com.google.common.io.Files;
-import com.sun.jersey.api.client.Client;
-import com.sun.jersey.api.client.WebResource;
-import org.apache.catalina.LifecycleException;
-import org.apache.catalina.startup.Tomcat;
-import org.apache.commons.lang.math.RandomUtils;
-import org.apache.usergrid.cassandra.AvailablePortFinder;
-import org.junit.rules.ExternalResource;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-import org.springframework.context.ConfigurableApplicationContext;
-import org.springframework.context.support.ClassPathXmlApplicationContext;
-
-import javax.servlet.ServletException;
-import java.io.File;
-import java.util.concurrent.atomic.AtomicInteger;
-
-
-/**
- * Start and stop embedded Tomcat.
- */
-public class TomcatResource extends ExternalResource {
-    private static final Logger log = LoggerFactory.getLogger(TomcatResource.class);
-
-    public static final TomcatResource instance = new TomcatResource();
-
-    private static final Object mutex = new Object();
-    private String webAppsPath = "src/main/webapp";
-    private int port;
-    private boolean started = false;
-
-    private static AtomicInteger clientCount = new AtomicInteger(0);
-
-    Tomcat tomcat = null;
-    Process process = null;
-
-
-    public TomcatResource() {
-        try {
-            String[] locations = { "usergrid-properties-context.xml" };
-            ConfigurableApplicationContext appContext =
-                new ClassPathXmlApplicationContext( locations );
-
-        } catch (Exception ex) {
-            throw new RuntimeException("Error getting properties", ex);
-        }
-    }
-
-
-    @Override
-    protected void after() {
-        log.info("Entering after");
-
-        synchronized (mutex) {
-
-            if ( clientCount.decrementAndGet() < 1 ) {
-
-                log.info("----------------------------------------------------------------------");
-                log.info("Destroying Tomcat running on port " + port);
-                log.info("----------------------------------------------------------------------");
-
-                if ( process != null ) {
-                    process.destroy();
-
-                } else if ( tomcat != null ) {
-                    try {
-                        tomcat.stop();
-                        tomcat.destroy();
-                    } catch (LifecycleException ex) {
-                        log.error("Error stopping Tomcat", ex);
-                    }
-                }
-                started = false;
-
-            } else {
-                log.info("NOT stopping Tomcat because it is still in use by {}", clientCount.get());
-            }
-        }
-
-        log.info("Leaving after");
-    }
-
-
-    @Override
-    protected void before() throws Throwable {
-        log.info("Entering before");
-
-        synchronized (mutex) {
-
-            clientCount.incrementAndGet();
-
-            if (started) {
-                log.info("NOT starting Tomcat because it is already started");
-                log.info("Leaving before: {} users of Tomcat", clientCount.get());
-                return;
-            }
-
-            startTomcatEmbedded();
-
-            log.info("Leaving before: Started Tomcat, now {} users", clientCount.get());
-        }
-
-    }
-
-
-    private void waitForTomcat() throws RuntimeException {
-        String url = "http://localhost:" + port + "/status";
-        int count = 0;
-        while (count++ < 30) {
-            try {
-                Thread.sleep(1000);
-                Client c = Client.create();
-                WebResource wr = c.resource( url );
-                wr.get(String.class);
-                log.info("Tomcat is started.");
-                started = true;
-                break;
-
-            } catch (Exception e) {
-                log.info("Waiting for Tomcat on url {}", url);
-            }
-        }
-        if ( !started ) {
-            throw new RuntimeException("Tomcat process never started.");
-        }
-    }
-
-
-    private void startTomcatEmbedded() throws ServletException, LifecycleException {
-
-        File dataDir = Files.createTempDir();
-        dataDir.deleteOnExit();
-
-        port = AvailablePortFinder.getNextAvailable( 9998 + RandomUtils.nextInt(10)  );
-
-        tomcat = new Tomcat();
-        tomcat.setBaseDir( dataDir.getAbsolutePath() );
-        tomcat.setPort( port );
-        tomcat.getConnector().setAttribute("maxThreads", "2000");
-        tomcat.addWebapp( "/", new File( getWebAppsPath() ).getAbsolutePath() );
-
-        log.info("-----------------------------------------------------------------");
-        log.info("Starting Tomcat embedded port {} dir {}", port, dataDir.getAbsolutePath());
-        log.info("-----------------------------------------------------------------");
-        tomcat.start();
-
-        waitForTomcat();
-
-        mutex.notifyAll();
-    }
-
-    public int getPort() {
-        return port;
-    }
-
-    public String getWebAppsPath() {
-        return webAppsPath;
-    }
-
-    public void setWebAppsPath(String webAppsPath) {
-        this.webAppsPath = webAppsPath;
-    }
-
-}

http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/ba7a11ae/stack/rest/src/test/java/org/apache/usergrid/rest/TomcatRuntime.java
----------------------------------------------------------------------
diff --git a/stack/rest/src/test/java/org/apache/usergrid/rest/TomcatRuntime.java b/stack/rest/src/test/java/org/apache/usergrid/rest/TomcatRuntime.java
new file mode 100644
index 0000000..ab22450
--- /dev/null
+++ b/stack/rest/src/test/java/org/apache/usergrid/rest/TomcatRuntime.java
@@ -0,0 +1,192 @@
+/*
+ * 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 org.apache.usergrid.rest;
+
+
+import java.io.File;
+
+import org.junit.rules.ExternalResource;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import org.apache.catalina.LifecycleException;
+import org.apache.catalina.startup.Tomcat;
+import org.apache.commons.lang.math.RandomUtils;
+
+import org.apache.usergrid.cassandra.AvailablePortFinder;
+import org.apache.usergrid.setup.ConcurrentProcessSingleton;
+
+import com.google.common.io.Files;
+import com.sun.jersey.api.client.Client;
+import com.sun.jersey.api.client.WebResource;
+
+
+/**
+ * Start and stop embedded Tomcat.
+ */
+public class TomcatRuntime extends ExternalResource {
+    private static final Logger log = LoggerFactory.getLogger( TomcatRuntime.class );
+
+
+    private static final String WEBAPPS_PATH = "src/main/webapp";
+
+    private static TomcatRuntime instance;
+
+    public final TomcatInstance tomcat;
+
+
+    private TomcatRuntime() {
+
+        //before we run tomcat, we need to cleanup our data
+        ConcurrentProcessSingleton.getInstance();
+
+        tomcat = new TomcatInstance( WEBAPPS_PATH );
+        tomcat.startTomcat();
+
+        //stop on JVM shutdown
+        Runtime.getRuntime().addShutdownHook( new Thread() {
+            @Override
+            public void run() {
+                tomcat.stopTomcat();
+            }
+        } );
+    }
+
+
+    /**
+     * Get the instance of the tomcat runtime and starts the tomcat singleton.  Starts tomcat once per JVM
+     * @return
+     */
+    public static synchronized TomcatRuntime getInstance() {
+        if ( instance == null ) {
+
+
+            instance = new TomcatRuntime();
+        }
+
+        return instance;
+    }
+
+
+    /**
+     * Get the port tomcat is running on
+     */
+    public int getPort() {
+        return tomcat.getPort();
+    }
+
+
+    /**
+     * Inner class of tomcat runtime
+     */
+    private static class TomcatInstance {
+
+        public static final int THREADS_PERPROC = 25;
+
+        private final String webAppsPath;
+
+        private Tomcat tomcat = null;
+        private int port;
+
+        private boolean started = false;
+
+
+        private TomcatInstance( final String webAppsPath ) {this.webAppsPath = webAppsPath;}
+
+
+        /**
+         * Start the tomcat instance
+         */
+        public void startTomcat() {
+            try {
+
+                //we don't want to use all our threads, we'll kill the box
+                final int availableProcessors = Runtime.getRuntime().availableProcessors();
+                final int usedProcs = Math.min( 2, availableProcessors );
+                final int threads = usedProcs * THREADS_PERPROC;
+
+
+                File dataDir = Files.createTempDir();
+                dataDir.deleteOnExit();
+
+                port = AvailablePortFinder.getNextAvailable( 9998 + RandomUtils.nextInt( 10 ) );
+
+                tomcat = new Tomcat();
+                tomcat.setBaseDir( dataDir.getAbsolutePath() );
+                tomcat.setPort( port );
+
+
+                tomcat.getConnector().setAttribute( "maxThreads", "" + threads );
+
+                tomcat.addWebapp( "/", new File( webAppsPath ).getAbsolutePath() );
+
+
+                log.info( "-----------------------------------------------------------------" );
+                log.info( "Starting Tomcat embedded port {} dir {}", port, dataDir.getAbsolutePath() );
+                log.info( "-----------------------------------------------------------------" );
+                tomcat.start();
+
+                waitForTomcat();
+
+            }
+            catch ( Exception e ) {
+                throw new RuntimeException( "Couldn't start tomcat", e );
+            }
+        }
+
+
+        /**
+         * Stop the embedded tomcat process
+         */
+        public void stopTomcat() {
+            try {
+                tomcat.stop();
+            }
+            catch ( LifecycleException e ) {
+                throw new RuntimeException( "Unable to stop tomcat", e );
+            }
+        }
+
+
+        public int getPort() {
+            return port;
+        }
+
+
+        private void waitForTomcat() throws RuntimeException {
+            String url = "http://localhost:" + port + "/status";
+            int count = 0;
+            while ( count++ < 30 ) {
+                try {
+                    Thread.sleep( 1000 );
+                    Client c = Client.create();
+                    WebResource wr = c.resource( url );
+                    wr.get( String.class );
+                    log.info( "Tomcat is started." );
+                    started = true;
+                    break;
+                }
+                catch ( Exception e ) {
+                    log.info( "Waiting for Tomcat on url {}", url );
+                }
+            }
+            if ( !started ) {
+                throw new RuntimeException( "Tomcat process never started." );
+            }
+        }
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/ba7a11ae/stack/rest/src/test/java/org/apache/usergrid/rest/management/RegistrationIT.java
----------------------------------------------------------------------
diff --git a/stack/rest/src/test/java/org/apache/usergrid/rest/management/RegistrationIT.java b/stack/rest/src/test/java/org/apache/usergrid/rest/management/RegistrationIT.java
index aca33ba..f949da3 100644
--- a/stack/rest/src/test/java/org/apache/usergrid/rest/management/RegistrationIT.java
+++ b/stack/rest/src/test/java/org/apache/usergrid/rest/management/RegistrationIT.java
@@ -43,6 +43,7 @@ import org.slf4j.LoggerFactory;
 import org.apache.usergrid.management.UserInfo;
 import org.apache.usergrid.persistence.model.util.UUIDGenerator;
 import org.apache.usergrid.rest.AbstractRestIT;
+import org.apache.usergrid.rest.ITSetup;
 import org.apache.usergrid.rest.TestContextSetup;
 import org.apache.usergrid.rest.test.security.TestAppUser;
 import org.apache.usergrid.rest.test.security.TestUser;
@@ -71,6 +72,8 @@ public class RegistrationIT extends AbstractRestIT {
 
     private static final Logger logger = LoggerFactory.getLogger( RegistrationIT.class );
 
+    private static final ITSetup setup = ITSetup.getInstance();
+
     @Rule
     public TestContextSetup context = new TestContextSetup( this );
 
@@ -100,7 +103,7 @@ public class RegistrationIT extends AbstractRestIT {
             context.createAppForOrg();
 
             final UUID owner_uuid = context.getActiveUser().getUuid();
-            
+
 //            refreshIndex("test-organization", "test-app");
 //
 //            UUID owner_uuid =
@@ -309,7 +312,7 @@ public class RegistrationIT extends AbstractRestIT {
             String adminUserName = "AdminUserFromOtherOrg";
             String adminUserEmail = "AdminUserFromOtherOrg@otherorg.com";
 
-            UserInfo adminUser = setup.getMgmtSvc().createAdminUser( 
+            UserInfo adminUser = setup.getMgmtSvc().createAdminUser(
                     adminUserEmail, adminUserEmail, adminUserEmail, "password1", true, false );
 
             refreshIndex("test-organization", "test-app");
@@ -323,7 +326,7 @@ public class RegistrationIT extends AbstractRestIT {
             // this should NOT send resetpwd link in email to newly added org admin user(that
             // already exists in usergrid) only "User Invited To Organization" email
             String adminToken = adminToken();
-            JsonNode node = postAddAdminToOrg( "test-organization", 
+            JsonNode node = postAddAdminToOrg( "test-organization",
                     adminUserEmail, "password1", adminToken );
             String uuid = node.get( "data" ).get( "user" ).get( "uuid" ).textValue();
             UUID userId = UUID.fromString( uuid );

http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/ba7a11ae/stack/rest/src/test/java/org/apache/usergrid/rest/test/resource2point0/AbstractRestIT.java
----------------------------------------------------------------------
diff --git a/stack/rest/src/test/java/org/apache/usergrid/rest/test/resource2point0/AbstractRestIT.java b/stack/rest/src/test/java/org/apache/usergrid/rest/test/resource2point0/AbstractRestIT.java
index 43d0ba4..90d3d55 100644
--- a/stack/rest/src/test/java/org/apache/usergrid/rest/test/resource2point0/AbstractRestIT.java
+++ b/stack/rest/src/test/java/org/apache/usergrid/rest/test/resource2point0/AbstractRestIT.java
@@ -28,7 +28,7 @@ import com.sun.jersey.test.framework.JerseyTest;
 import com.sun.jersey.test.framework.WebAppDescriptor;
 import com.sun.jersey.test.framework.spi.container.TestContainerFactory;
 import org.apache.usergrid.rest.ITSetup;
-import org.apache.usergrid.rest.TomcatResource;
+import org.apache.usergrid.rest.TomcatRuntime;
 import org.apache.usergrid.rest.test.resource2point0.endpoints.ApplicationsResource;
 import org.apache.usergrid.rest.test.resource2point0.endpoints.OrganizationResource;
 import org.apache.usergrid.rest.test.resource2point0.endpoints.mgmt.ManagementResource;
@@ -53,12 +53,10 @@ public class AbstractRestIT extends JerseyTest {
 
     private static ClientConfig clientConfig = new DefaultClientConfig();
 
-    @Rule
-    public TomcatResource tomcatResource = new TomcatResource();
+    public static TomcatRuntime tomcatRuntime = TomcatRuntime.getInstance();
+
+
 
-    public static ITSetup setup = new ITSetup(  );
-//
-//    TODO: Allow the client to be setup seperately
     @Rule
     public ClientSetup clientSetup = new ClientSetup( this.getBaseURI().toString() );
 
@@ -112,7 +110,7 @@ public class AbstractRestIT extends JerseyTest {
     @Override
     protected URI getBaseURI() {
         try {
-            return new URI("http://localhost:" + tomcatResource.getPort());
+            return new URI("http://localhost:" + tomcatRuntime.getPort());
         } catch (URISyntaxException e) {
             throw new RuntimeException("Error determining baseURI", e);
         }