You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@stanbol.apache.org by bd...@apache.org on 2011/02/11 13:52:06 UTC

svn commit: r1069787 - in /incubator/stanbol/trunk: commons/testing/stanbol/src/main/java/org/apache/stanbol/commons/testing/stanbol/ enhancer/integration-tests/src/test/java/org/apache/stanbol/enhancer/it/ enhancer/jersey/src/main/java/org/apache/stan...

Author: bdelacretaz
Date: Fri Feb 11 12:52:05 2011
New Revision: 1069787

URL: http://svn.apache.org/viewvc?rev=1069787&view=rev
Log:
STANBOL-75 - wait for all engines to be ready before running tests

Added:
    incubator/stanbol/trunk/commons/testing/stanbol/src/main/java/org/apache/stanbol/commons/testing/stanbol/RetryLoop.java   (with props)
    incubator/stanbol/trunk/enhancer/integration-tests/src/test/java/org/apache/stanbol/enhancer/it/EnhancerTestBase.java   (with props)
Modified:
    incubator/stanbol/trunk/enhancer/integration-tests/src/test/java/org/apache/stanbol/enhancer/it/HomepageTest.java
    incubator/stanbol/trunk/enhancer/integration-tests/src/test/java/org/apache/stanbol/enhancer/it/StatelessEngineTest.java
    incubator/stanbol/trunk/enhancer/jersey/src/main/java/org/apache/stanbol/enhancer/jersey/resource/EnginesRootResource.java

Added: incubator/stanbol/trunk/commons/testing/stanbol/src/main/java/org/apache/stanbol/commons/testing/stanbol/RetryLoop.java
URL: http://svn.apache.org/viewvc/incubator/stanbol/trunk/commons/testing/stanbol/src/main/java/org/apache/stanbol/commons/testing/stanbol/RetryLoop.java?rev=1069787&view=auto
==============================================================================
--- incubator/stanbol/trunk/commons/testing/stanbol/src/main/java/org/apache/stanbol/commons/testing/stanbol/RetryLoop.java (added)
+++ incubator/stanbol/trunk/commons/testing/stanbol/src/main/java/org/apache/stanbol/commons/testing/stanbol/RetryLoop.java Fri Feb 11 12:52:05 2011
@@ -0,0 +1,80 @@
+/*
+ * 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.stanbol.commons.testing.stanbol;
+
+import static org.junit.Assert.fail;
+
+/** Convenience class for retrying tests
+ *  until timeout or success.
+ */
+public class RetryLoop {
+    
+    private final long timeout;
+    
+    /** Interface for conditions to check, isTrue will be called
+     *  repeatedly until success or timeout */
+    static public interface Condition {
+        /** Used in failure messages to describe what was expected */
+        String getDescription();
+        
+        /** If true we stop retrying. The RetryLoop retries on AssertionError, 
+         *  so if tests fail in this method they are not reported as 
+         *  failures but retried.
+         */
+        boolean isTrue() throws Exception;
+    }
+    
+    /** Retry Condition c until it returns true or timeout. See {@link Condition}
+     *  for isTrue semantics.
+     */
+    public RetryLoop(Condition c, int timeoutSeconds, int intervalBetweenTriesMsec) {
+        timeout = System.currentTimeMillis() + timeoutSeconds * 1000L;
+        while(System.currentTimeMillis() < timeout) {
+            try {
+                if(c.isTrue()) {
+                    return;
+                }
+            } catch(AssertionError ae) {
+                // Retry JUnit tests failing in the condition as well
+                reportException(ae);
+            } catch(Exception e) {
+                reportException(e);
+            }
+            
+            try {
+                Thread.sleep(intervalBetweenTriesMsec);
+            } catch(InterruptedException ignore) {
+            }
+        }
+    
+        onTimeout();
+        fail("RetryLoop failed, condition is false after " + timeoutSeconds + " seconds: " 
+                + c.getDescription());
+    }
+
+    /** Can be overridden to report Exceptions that happen in the retry loop */
+    protected void reportException(Throwable t) {
+    }
+    
+    /** Called if the loop times out without success, just before failing */
+    protected void onTimeout() {
+    }
+    
+    protected long getRemainingTimeSeconds() {
+        return Math.max(0L, (timeout - System.currentTimeMillis()) / 1000L);
+    }
+}

Propchange: incubator/stanbol/trunk/commons/testing/stanbol/src/main/java/org/apache/stanbol/commons/testing/stanbol/RetryLoop.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/stanbol/trunk/commons/testing/stanbol/src/main/java/org/apache/stanbol/commons/testing/stanbol/RetryLoop.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision Rev URL

Added: incubator/stanbol/trunk/enhancer/integration-tests/src/test/java/org/apache/stanbol/enhancer/it/EnhancerTestBase.java
URL: http://svn.apache.org/viewvc/incubator/stanbol/trunk/enhancer/integration-tests/src/test/java/org/apache/stanbol/enhancer/it/EnhancerTestBase.java?rev=1069787&view=auto
==============================================================================
--- incubator/stanbol/trunk/enhancer/integration-tests/src/test/java/org/apache/stanbol/enhancer/it/EnhancerTestBase.java (added)
+++ incubator/stanbol/trunk/enhancer/integration-tests/src/test/java/org/apache/stanbol/enhancer/it/EnhancerTestBase.java Fri Feb 11 12:52:05 2011
@@ -0,0 +1,101 @@
+/*
+ * 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.stanbol.enhancer.it;
+
+import static org.junit.Assert.fail;
+
+import org.apache.stanbol.commons.testing.stanbol.RetryLoop;
+import org.apache.stanbol.commons.testing.stanbol.StanbolTestBase;
+import org.junit.Before;
+
+/** Inherit from this to wait for all default enhancement 
+ *  engines to be up before running tests.
+ */
+public class EnhancerTestBase extends StanbolTestBase {
+
+    // TODO configurable via system properties??
+    public static final int ENGINES_TIMEOUT_SECONDS = 60;
+    public static final int WAIT_BETWEEN_TRIES_MSEC = 1000;
+    
+    static boolean enginesReady;
+    static boolean timedOut;
+    
+    @Before
+    public void checkEnginesReady() throws Exception {
+    
+        // Check only once per test run
+        if(enginesReady) {
+            return;
+        }
+        
+        // If we timed out previously, don't waste time checking again
+        if(timedOut) {
+            fail("Timeout in previous check of enhancement engines, cannot run tests");
+        }
+        
+        // We'll retry the check for all engines to be ready
+        // for up to ENGINES_TIMEOUT_SECONDS 
+        final RetryLoop.Condition c = new RetryLoop.Condition() {
+            
+            @Override
+            public boolean isTrue() throws Exception {
+                /*  List of expected engines could be made configurable via system
+                 *  properties, but we don't expect it to change often. 
+                 */
+                executor.execute(
+                        builder.buildGetRequest("/engines")
+                        .withHeader("Accept", "text/plain")
+                )
+                .assertStatus(200)
+                .assertContentType("text/plain")
+                .assertContentRegexp(
+                    "org.apache.stanbol.*MetaxaEngine",
+                    "org.apache.stanbol.*LangIdEnhancementEngine",
+                    "org.apache.stanbol.*NamedEntityExtractionEnhancementEngine",
+                    "org.apache.stanbol.*OpenCalaisEngine",
+                    "org.apache.stanbol.*EntityMentionEnhancementEngine",
+                    "org.apache.stanbol.*LocationEnhancementEngine",
+                    "org.apache.stanbol.*RelatedTopicEnhancementEngine",
+                    "org.apache.stanbol.*CachingDereferencerEngine"
+                );
+                // TODO use a log
+                System.out.println("Enhancement engines checked, all present");
+                return true;
+            }
+            
+            @Override
+            public String getDescription() {
+                return "Checking that all enhancement engines are ready";
+            }
+        };
+        
+        new RetryLoop(c, ENGINES_TIMEOUT_SECONDS, WAIT_BETWEEN_TRIES_MSEC) {
+            @Override
+            protected void reportException(Throwable t) {
+                // TODO use a log??
+                System.out.println("Exception in RetryLoop, will retry for up to " 
+                        + getRemainingTimeSeconds() + " seconds: " + t);
+            }
+            
+            protected void onTimeout() {
+                timedOut = true;
+            }
+        };
+        
+        enginesReady = true;
+    }
+}

Propchange: incubator/stanbol/trunk/enhancer/integration-tests/src/test/java/org/apache/stanbol/enhancer/it/EnhancerTestBase.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/stanbol/trunk/enhancer/integration-tests/src/test/java/org/apache/stanbol/enhancer/it/EnhancerTestBase.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision Rev URL

Modified: incubator/stanbol/trunk/enhancer/integration-tests/src/test/java/org/apache/stanbol/enhancer/it/HomepageTest.java
URL: http://svn.apache.org/viewvc/incubator/stanbol/trunk/enhancer/integration-tests/src/test/java/org/apache/stanbol/enhancer/it/HomepageTest.java?rev=1069787&r1=1069786&r2=1069787&view=diff
==============================================================================
--- incubator/stanbol/trunk/enhancer/integration-tests/src/test/java/org/apache/stanbol/enhancer/it/HomepageTest.java (original)
+++ incubator/stanbol/trunk/enhancer/integration-tests/src/test/java/org/apache/stanbol/enhancer/it/HomepageTest.java Fri Feb 11 12:52:05 2011
@@ -19,7 +19,10 @@ package org.apache.stanbol.enhancer.it;
 import org.apache.stanbol.commons.testing.stanbol.StanbolTestBase;
 import org.junit.Test;
 
-/** Test the enhancer homepage and demonstrate the test classes */
+/** Test the enhancer homepage and demonstrate the test classes.
+ *  Does not inherit from EnhancerTestBase as we don't care
+ *  at this stage if engines are ready or not.
+ */
 public class HomepageTest extends StanbolTestBase {
     
     @Test

Modified: incubator/stanbol/trunk/enhancer/integration-tests/src/test/java/org/apache/stanbol/enhancer/it/StatelessEngineTest.java
URL: http://svn.apache.org/viewvc/incubator/stanbol/trunk/enhancer/integration-tests/src/test/java/org/apache/stanbol/enhancer/it/StatelessEngineTest.java?rev=1069787&r1=1069786&r2=1069787&view=diff
==============================================================================
--- incubator/stanbol/trunk/enhancer/integration-tests/src/test/java/org/apache/stanbol/enhancer/it/StatelessEngineTest.java (original)
+++ incubator/stanbol/trunk/enhancer/integration-tests/src/test/java/org/apache/stanbol/enhancer/it/StatelessEngineTest.java Fri Feb 11 12:52:05 2011
@@ -17,11 +17,10 @@
 package org.apache.stanbol.enhancer.it;
 
 import org.apache.stanbol.commons.testing.http.RequestDocumentor;
-import org.apache.stanbol.commons.testing.stanbol.StanbolTestBase;
 import org.junit.Test;
 
 /** Test the stateless text enhancement engines */
-public class StatelessEngineTest extends StanbolTestBase {
+public class StatelessEngineTest extends EnhancerTestBase {
     
     private final RequestDocumentor documentor = new RequestDocumentor(getClass().getName());
     

Modified: incubator/stanbol/trunk/enhancer/jersey/src/main/java/org/apache/stanbol/enhancer/jersey/resource/EnginesRootResource.java
URL: http://svn.apache.org/viewvc/incubator/stanbol/trunk/enhancer/jersey/src/main/java/org/apache/stanbol/enhancer/jersey/resource/EnginesRootResource.java?rev=1069787&r1=1069786&r2=1069787&view=diff
==============================================================================
--- incubator/stanbol/trunk/enhancer/jersey/src/main/java/org/apache/stanbol/enhancer/jersey/resource/EnginesRootResource.java (original)
+++ incubator/stanbol/trunk/enhancer/jersey/src/main/java/org/apache/stanbol/enhancer/jersey/resource/EnginesRootResource.java Fri Feb 11 12:52:05 2011
@@ -1,7 +1,17 @@
 package org.apache.stanbol.enhancer.jersey.resource;
 
+import static javax.ws.rs.core.MediaType.APPLICATION_FORM_URLENCODED;
+import static javax.ws.rs.core.MediaType.APPLICATION_JSON;
+import static javax.ws.rs.core.MediaType.APPLICATION_JSON_TYPE;
+import static javax.ws.rs.core.MediaType.TEXT_HTML;
+import static javax.ws.rs.core.MediaType.TEXT_PLAIN;
+import static javax.ws.rs.core.MediaType.WILDCARD;
+import static org.apache.clerezza.rdf.core.serializedform.SupportedFormat.RDF_JSON;
+import static org.apache.clerezza.rdf.core.serializedform.SupportedFormat.RDF_XML;
+
 import java.io.IOException;
 import java.net.URI;
+import java.util.ArrayList;
 import java.util.Collections;
 import java.util.List;
 
@@ -35,11 +45,6 @@ import org.slf4j.LoggerFactory;
 import com.sun.jersey.api.view.ImplicitProduces;
 import com.sun.jersey.api.view.Viewable;
 
-
-import static javax.ws.rs.core.MediaType.*;
-import static org.apache.clerezza.rdf.core.serializedform.SupportedFormat.RDF_JSON;
-import static org.apache.clerezza.rdf.core.serializedform.SupportedFormat.RDF_XML;
-
 /**
  * RESTful interface to browse the list of available engines and allow to call
  * them in a stateless, synchronous way.
@@ -81,20 +86,36 @@ public class EnginesRootResource extends
         }
     }
 
+    private List<EnhancementEngine> getEngines() {
+        if (jobManager != null) {
+            return jobManager.getActiveEngines();
+        }
+        return new ArrayList<EnhancementEngine>();
+    }
+    
     @GET
     @Produces(APPLICATION_JSON)
     public JSONArray getEnginesAsJsonArray() {
         JSONArray uriArray = new JSONArray();
-        if (jobManager != null) {
-            for (EnhancementEngine engine : jobManager.getActiveEngines()) {
-                UriBuilder ub = uriInfo.getAbsolutePathBuilder();
-                URI userUri = ub.path(makeEngineId(engine)).build();
-                uriArray.put(userUri.toASCIIString());
-            }
+        for (EnhancementEngine engine : getEngines()) {
+            UriBuilder ub = uriInfo.getAbsolutePathBuilder();
+            URI userUri = ub.path(makeEngineId(engine)).build();
+            uriArray.put(userUri.toASCIIString());
         }
         return uriArray;
     }
 
+    @GET
+    @Produces(TEXT_PLAIN)
+    public String getEnginesAsString() {
+        final StringBuilder sb = new StringBuilder();
+        for (EnhancementEngine engine : getEngines()) {
+            sb.append(engine.getClass().getName());
+            sb.append("\n");
+        }
+        return sb.toString();
+    }
+
     public static String makeEngineId(EnhancementEngine engine) {
         // TODO: add a property on engines to provided custom local ids and make
         // this static method a method of the interface EnhancementEngine