ace-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From j...@apache.org
Subject svn commit: r1529142 - in /ace/trunk: org.apache.ace.repository.itest/src/org/apache/ace/it/repository/ org.apache.ace.test/src/org/apache/ace/it/
Date Fri, 04 Oct 2013 12:26:17 GMT
Author: jawi
Date: Fri Oct  4 12:26:17 2013
New Revision: 1529142

URL: http://svn.apache.org/r1529142
Log:
Yet some more fixes to improve itest stability.


Modified:
    ace/trunk/org.apache.ace.repository.itest/src/org/apache/ace/it/repository/RepositoryTest.java
    ace/trunk/org.apache.ace.repository.itest/src/org/apache/ace/it/repository/Utils.java
    ace/trunk/org.apache.ace.test/src/org/apache/ace/it/IntegrationTestBase.java

Modified: ace/trunk/org.apache.ace.repository.itest/src/org/apache/ace/it/repository/RepositoryTest.java
URL: http://svn.apache.org/viewvc/ace/trunk/org.apache.ace.repository.itest/src/org/apache/ace/it/repository/RepositoryTest.java?rev=1529142&r1=1529141&r2=1529142&view=diff
==============================================================================
--- ace/trunk/org.apache.ace.repository.itest/src/org/apache/ace/it/repository/RepositoryTest.java
(original)
+++ ace/trunk/org.apache.ace.repository.itest/src/org/apache/ace/it/repository/RepositoryTest.java
Fri Oct  4 12:26:17 2013
@@ -35,22 +35,23 @@ import java.util.concurrent.TimeUnit;
 import org.apache.ace.http.listener.constants.HttpConstants;
 import org.apache.ace.it.IntegrationTestBase;
 import org.apache.ace.repository.Repository;
-import org.apache.ace.repository.impl.constants.RepositoryConstants;
 import org.apache.ace.test.constants.TestConstants;
-import org.apache.felix.dm.Component;
 import org.osgi.framework.Constants;
 import org.osgi.framework.InvalidSyntaxException;
 import org.osgi.framework.ServiceReference;
 import org.osgi.service.cm.Configuration;
-import org.osgi.service.cm.ConfigurationAdmin;
 import org.osgi.util.tracker.ServiceTracker;
 
 /**
  * Integration test for our repositories, and the replication thereof.
  */
 public class RepositoryTest extends IntegrationTestBase {
-
-    private volatile ConfigurationAdmin m_configAdmin;
+    private static final String REPOSITORY_NAME = "name";
+    private static final String REPOSITORY_CUSTOMER = "customer";
+    private static final String REPOSITORY_MASTER = "master";
+    private static final String REPOSITORY_INITIAL_CONTENT = "initial";
+    private static final String REPOSITORY_BASE_DIR = "basedir";
+    private static final String REPOSITORY_FILE_EXTENSION = "fileextension";
 
     private URL m_host;
 
@@ -145,7 +146,7 @@ public class RepositoryTest extends Inte
     }
 
     public void testGetAndPutWithCustomBasedirAndFileExtenions() throws Exception {
-        
+
         File tmpFile = File.createTempFile("repo", "");
         tmpFile.delete();
         tmpFile.mkdir();
@@ -209,15 +210,6 @@ public class RepositoryTest extends Inte
     }
 
     @Override
-    protected Component[] getDependencies() {
-        return new Component[] {
-            createComponent()
-                .setImplementation(this)
-                .add(createServiceDependency().setService(ConfigurationAdmin.class).setRequired(true))
-        };
-    }
-
-    @Override
     protected void doTearDown() throws Exception {
         // remove all repositories, in case a test case does not reach it's cleanup section
due to an exception
         removeAllRepositories();
@@ -229,32 +221,35 @@ public class RepositoryTest extends Inte
 
     /* Configure a new repository instance */
     private void addRepository(String instanceName, String customer, String name, String
basedir, String fileextension, String initial, boolean isMaster) throws IOException, InterruptedException,
InvalidSyntaxException {
+        ServiceTracker tracker = new ServiceTracker(m_bundleContext, m_bundleContext.createFilter("(factory.instance.pid="
+ instanceName + ")"), null);
+        tracker.open();
+
         // Publish configuration for a repository instance
         Properties props = new Properties();
-        props.put(RepositoryConstants.REPOSITORY_CUSTOMER, customer);
-        props.put(RepositoryConstants.REPOSITORY_NAME, name);
-        props.put(RepositoryConstants.REPOSITORY_BASE_DIR, basedir == null ? "" : basedir);
-        props.put(RepositoryConstants.REPOSITORY_FILE_EXTENSION, fileextension == null ?
"" : fileextension);
-        props.put(RepositoryConstants.REPOSITORY_MASTER, String.valueOf(isMaster));
+        props.put(REPOSITORY_CUSTOMER, customer);
+        props.put(REPOSITORY_NAME, name);
+        props.put(REPOSITORY_BASE_DIR, basedir == null ? "" : basedir);
+        props.put(REPOSITORY_FILE_EXTENSION, fileextension == null ? "" : fileextension);
+        props.put(REPOSITORY_MASTER, String.valueOf(isMaster));
         if (initial != null) {
-            props.put(RepositoryConstants.REPOSITORY_INITIAL_CONTENT, initial);
+            props.put(REPOSITORY_INITIAL_CONTENT, initial);
         }
         props.put("factory.instance.pid", instanceName);
-        Configuration config = m_configAdmin.createFactoryConfiguration("org.apache.ace.server.repository.factory",
null);
-
-        ServiceTracker tracker = new ServiceTracker(m_bundleContext, m_bundleContext.createFilter("(factory.instance.pid="
+ instanceName + ")"), null);
-        tracker.open();
-
+        Configuration config = createFactoryConfiguration("org.apache.ace.server.repository.factory");
         config.update(props);
 
-        if (tracker.waitForService(1000) == null) {
-            throw new IOException("Did not get notified about new repository becoming available
in time.");
+        try {
+            if (tracker.waitForService(1000) == null) {
+                throw new IOException("Did not get notified about new repository becoming
available in time.");
+            }
+        }
+        finally {
+            tracker.close();
         }
-        tracker.close();
     }
 
     private void removeAllRepositories() throws IOException, InvalidSyntaxException, InterruptedException
{
-        final Configuration[] configs = m_configAdmin.listConfigurations("(factory.instance.pid=*)");
+        final Configuration[] configs = listConfigurations("(factory.instance.pid=*)");
         if ((configs != null) && (configs.length > 0)) {
             final Semaphore sem = new Semaphore(0);
 
@@ -270,25 +265,29 @@ public class RepositoryTest extends Inte
             };
             tracker.open();
 
-            for (int i = 0; i < configs.length; i++) {
-                configs[i].delete();
+            try {
+                for (int i = 0; i < configs.length; i++) {
+                    configs[i].delete();
+                }
+                if (!sem.tryAcquire(1, TimeUnit.SECONDS)) {
+                    throw new IOException("Not all instances were removed in time.");
+                }
             }
-
-            if (!sem.tryAcquire(1, TimeUnit.SECONDS)) {
-                throw new IOException("Not all instances were removed in time.");
+            finally {
+                tracker.close();
             }
-            tracker.close();
         }
     }
 
     private void removeRepository(String instanceName) throws IOException, InterruptedException,
InvalidSyntaxException {
         Configuration[] configs = null;
         try {
-            configs = m_configAdmin.listConfigurations("(factory.instance.pid=" + instanceName
+ ")");
+            configs = listConfigurations("(factory.instance.pid=" + instanceName + ")");
         }
         catch (InvalidSyntaxException e) {
             // should not happen
         }
+
         if ((configs != null) && (configs.length > 0)) {
             final Semaphore sem = new Semaphore(0);
             ServiceTracker tracker = new ServiceTracker(m_bundleContext, m_bundleContext.createFilter("(factory.instance.pid="
+ instanceName + ")"), null) {
@@ -300,10 +299,15 @@ public class RepositoryTest extends Inte
             };
             tracker.open();
 
-            configs[0].delete();
+            try {
+                configs[0].delete();
 
-            if (!sem.tryAcquire(1, TimeUnit.SECONDS)) {
-                throw new IOException("Instance did not get removed in time.");
+                if (!sem.tryAcquire(1, TimeUnit.SECONDS)) {
+                    throw new IOException("Instance did not get removed in time.");
+                }
+            }
+            finally {
+                tracker.close();
             }
         }
     }

Modified: ace/trunk/org.apache.ace.repository.itest/src/org/apache/ace/it/repository/Utils.java
URL: http://svn.apache.org/viewvc/ace/trunk/org.apache.ace.repository.itest/src/org/apache/ace/it/repository/Utils.java?rev=1529142&r1=1529141&r2=1529142&view=diff
==============================================================================
--- ace/trunk/org.apache.ace.repository.itest/src/org/apache/ace/it/repository/Utils.java
(original)
+++ ace/trunk/org.apache.ace.repository.itest/src/org/apache/ace/it/repository/Utils.java
Fri Oct  4 12:26:17 2013
@@ -19,6 +19,7 @@
 
 package org.apache.ace.it.repository;
 
+import java.io.Closeable;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
@@ -27,29 +28,21 @@ import java.net.HttpURLConnection;
 import java.net.URL;
 
 final class Utils {
-	
+
     private static final int COPY_BUFFER_SIZE = 4096;
-    private static final String MIME_APPLICATION_OCTET_STREAM = "application/octet-stream";
          
+    private static final String MIME_APPLICATION_OCTET_STREAM = "application/octet-stream";
 
-    static void waitForWebserver(URL host) throws IOException {
-        int retries = 1;
-        IOException ioe = null;
-        while (retries++ < 10) {
-	        try {
-	        	((HttpURLConnection) host.openConnection()).getResponseCode();
-	        	return;
-	        }
-	        catch (ConnectException e) {
-	        	ioe = e;
-	        	try {
-	        		Thread.sleep(retries * 50);
-	        	}
-	        	catch (InterruptedException ie) {}
-	        }
+    static void closeSilently(Closeable resource) {
+        if (resource != null) {
+            try {
+                resource.close();
+            }
+            catch (IOException exception) {
+                // Ignore...
+            }
         }
-        throw ioe;
     }
-    
+
     /* copy in to out */
     static void copy(InputStream in, OutputStream out) throws IOException {
         byte[] buffer = new byte[COPY_BUFFER_SIZE];
@@ -59,17 +52,27 @@ final class Utils {
             bytes = in.read(buffer);
         }
     }
-    
+
     static int get(URL host, String endpoint, String customer, String name, String version,
OutputStream out) throws IOException {
         URL url = new URL(host, endpoint + "?customer=" + customer + "&name=" + name
+ "&version=" + version);
         HttpURLConnection connection = (HttpURLConnection) url.openConnection();
-        int responseCode = connection.getResponseCode();
-        if (responseCode == HttpURLConnection.HTTP_OK) {
-            InputStream input = connection.getInputStream();
-            copy(input, out);
-            out.flush();
+        try {
+            int responseCode = connection.getResponseCode();
+            if (responseCode == HttpURLConnection.HTTP_OK) {
+                InputStream input = connection.getInputStream();
+                try {
+                    copy(input, out);
+                    out.flush();
+                }
+                finally {
+                    closeSilently(input);
+                }
+            }
+            return responseCode;
+        }
+        finally {
+            connection.disconnect();
         }
-        return responseCode;
     }
 
     static int put(URL host, String endpoint, String customer, String name, String version,
InputStream in) throws IOException {
@@ -82,12 +85,26 @@ final class Utils {
         connection.setChunkedStreamingMode(8192);
         connection.setRequestProperty("Content-Type", MIME_APPLICATION_OCTET_STREAM);
         OutputStream out = connection.getOutputStream();
-        copy(in, out);
-        out.flush();
+        try {
+            copy(in, out);
+            out.flush();
+        }
+        finally {
+            closeSilently(in);
+            closeSilently(out);
+        }
+
         int responseCode = connection.getResponseCode();
         if (responseCode == HttpURLConnection.HTTP_OK) {
             InputStream is = (InputStream) connection.getContent();
-            is.close();
+            try {
+                while (is.read() > 0) {
+                    // ignore...
+                }
+            }
+            finally {
+                closeSilently(is);
+            }
         }
         return responseCode;
     }
@@ -101,9 +118,49 @@ final class Utils {
         int responseCode = connection.getResponseCode();
         if (responseCode == HttpURLConnection.HTTP_OK) {
             InputStream input = connection.getInputStream();
-            copy(input, out);
-            out.flush();
+            try {
+                copy(input, out);
+                out.flush();
+            }
+            finally {
+                closeSilently(input);
+            }
         }
         return responseCode;
     }
+
+    static void waitForWebserver(URL host) throws IOException {
+        int retries = 1;
+        IOException ioe = null;
+        HttpURLConnection conn = null;
+        while (retries++ < 10) {
+            try {
+                conn = (HttpURLConnection) host.openConnection();
+
+                int rc = conn.getResponseCode();
+                if (rc >= 0) {
+                    return;
+                }
+            }
+            catch (ConnectException e) {
+                ioe = e;
+                try {
+                    Thread.sleep(retries * 50);
+                }
+                catch (InterruptedException ie) {
+                    // We're asked to stop...
+                    return;
+                }
+            }
+            finally {
+                if (conn != null) {
+                    conn.disconnect();
+                }
+                conn = null;
+            }
+        }
+        if (ioe != null) {
+            throw ioe;
+        }
+    }
 }

Modified: ace/trunk/org.apache.ace.test/src/org/apache/ace/it/IntegrationTestBase.java
URL: http://svn.apache.org/viewvc/ace/trunk/org.apache.ace.test/src/org/apache/ace/it/IntegrationTestBase.java?rev=1529142&r1=1529141&r2=1529142&view=diff
==============================================================================
--- ace/trunk/org.apache.ace.test/src/org/apache/ace/it/IntegrationTestBase.java (original)
+++ ace/trunk/org.apache.ace.test/src/org/apache/ace/it/IntegrationTestBase.java Fri Oct 
4 12:26:17 2013
@@ -146,6 +146,15 @@ public class IntegrationTestBase extends
     }
 
     /**
+     * @param filter
+     * @return an array of configurations, can be <code>null</code>.
+     */
+    protected Configuration[] listConfigurations(String filter) throws IOException, InvalidSyntaxException
{
+        ConfigurationAdmin admin = getService(ConfigurationAdmin.class);
+        return admin.listConfigurations(filter);
+    }
+
+    /**
      * Sets whether or not any of the tracked configurations should be automatically be deleted
when ending a test.
      * 
      * @param aClean



Mime
View raw message