Author: reto
Date: Thu Nov 11 21:07:46 2010
New Revision: 1034127

URL: http://svn.apache.org/viewvc?rev=1034127&view=rev
Log:
added multithreaded request tests

Added:
    
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.platform.testing/src/test/java/org/apache/clerezza/platform/testing/blackbox/osgi/RDFListRootResource.java
    
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.platform.testing/src/test/java/org/apache/clerezza/platform/testing/blackbox/osgi/SimpleRootResource.java
Modified:
    
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.platform.testing/
   (props changed)
    
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.platform.testing/src/test/java/org/apache/clerezza/platform/testing/blackbox/osgi/FelixClerezzaPlatformTest.java

Propchange: 
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.platform.testing/
------------------------------------------------------------------------------
--- svn:ignore (original)
+++ svn:ignore Thu Nov 11 21:07:46 2010
@@ -1,3 +1,5 @@
 target
 .project
 .log.log.swp
+.classpath
+.settings

Modified: 
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.platform.testing/src/test/java/org/apache/clerezza/platform/testing/blackbox/osgi/FelixClerezzaPlatformTest.java
URL: 
http://svn.apache.org/viewvc/incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.platform.testing/src/test/java/org/apache/clerezza/platform/testing/blackbox/osgi/FelixClerezzaPlatformTest.java?rev=1034127&r1=1034126&r2=1034127&view=diff
==============================================================================
--- 
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.platform.testing/src/test/java/org/apache/clerezza/platform/testing/blackbox/osgi/FelixClerezzaPlatformTest.java
 (original)
+++ 
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.platform.testing/src/test/java/org/apache/clerezza/platform/testing/blackbox/osgi/FelixClerezzaPlatformTest.java
 Thu Nov 11 21:07:46 2010
@@ -18,37 +18,51 @@
  */
 package org.apache.clerezza.platform.testing.blackbox.osgi;
 
-import org.junit.runner.RunWith;
-import org.ops4j.pax.exam.Option;
-import org.ops4j.pax.exam.junit.Configuration;
-import org.ops4j.pax.exam.junit.JUnit4TestRunner;
-
-import static org.ops4j.pax.exam.CoreOptions.*;
-import static org.ops4j.pax.exam.container.def.PaxRunnerOptions.*;
-import static org.ops4j.pax.exam.junit.JUnitOptions.*;
+import static org.ops4j.pax.exam.CoreOptions.felix;
+import static org.ops4j.pax.exam.CoreOptions.frameworks;
+import static org.ops4j.pax.exam.CoreOptions.mavenBundle;
+import static org.ops4j.pax.exam.CoreOptions.mavenConfiguration;
+import static org.ops4j.pax.exam.CoreOptions.options;
+import static org.ops4j.pax.exam.CoreOptions.systemProperty;
+import static org.ops4j.pax.exam.container.def.PaxRunnerOptions.configProfile;
+import static org.ops4j.pax.exam.container.def.PaxRunnerOptions.vmOption;
+import static org.ops4j.pax.exam.container.def.PaxRunnerOptions.webProfile;
+import static org.ops4j.pax.exam.junit.JUnitOptions.junitBundles;
 
 import java.io.FileNotFoundException;
 import java.io.IOException;
+import java.io.InputStream;
 import java.net.MalformedURLException;
 import java.net.URL;
+import java.net.URLConnection;
+import java.util.Dictionary;
+import java.util.Hashtable;
 import java.util.logging.Level;
 import java.util.logging.Logger;
+
 import junit.framework.Assert;
+
 import org.apache.clerezza.rdf.core.access.TcManager;
 import org.junit.Before;
 import org.junit.Test;
+import org.junit.runner.RunWith;
 import org.ops4j.pax.exam.Inject;
+import org.ops4j.pax.exam.Option;
+import org.ops4j.pax.exam.junit.Configuration;
+import org.ops4j.pax.exam.junit.JUnit4TestRunner;
 import org.osgi.framework.BundleContext;
-
 import org.osgi.util.tracker.ServiceTracker;
 
 /**
- *
+ * 
  * @author mir, reto
  */
 @RunWith(JUnit4TestRunner.class)
 public class FelixClerezzaPlatformTest {
 
+       private static final int REQUESTS_PER_THREAD = 50;
+       private static final int THREADS_COUNT = 10;
+
        @Configuration
        public static Option[] configuration() {
                return options(
@@ -59,8 +73,6 @@ public class FelixClerezzaPlatformTest {
                                "org.osgi.compendium").versionAsInProject(),
                                
mavenBundle().groupId("org.apache.clerezza.ext").artifactId(
                                "com.hp.hpl.jena").versionAsInProject(),
-                               
/*mavenBundle().groupId("org.apache.felix").artifactId(
-                               "org.apache.felix.log").versionAsInProject(),*/
                                
mavenBundle().groupId("org.ops4j.pax.logging").artifactId(
                                "pax-logging-api").versionAsInProject(),
                                
mavenBundle().groupId("org.ops4j.pax.logging").artifactId(
@@ -175,13 +187,12 @@ public class FelixClerezzaPlatformTest {
                                
"org.apache.clerezza.platform.language.core").versionAsInProject(),
                                
mavenBundle().groupId("org.apache.httpcomponents").artifactId(
                                "httpcore-osgi").versionAsInProject(),
-                               //dsProfile(),
+                               // dsProfile(),
                                configProfile(),
                                webProfile(),
                                junitBundles(),
                                vmOption("-XX:MaxPermSize=200m"),
-                               frameworks(
-                               felix()),
+                               frameworks(felix()),
                                
systemProperty("org.osgi.service.http.port").value(
                                Integer.toString(testHttpPort)));
        }
@@ -191,22 +202,43 @@ public class FelixClerezzaPlatformTest {
        private boolean webServerExist;
 
        @Before
-       public void registerServices()
-                       throws Exception {
+       public void registerServices() throws Exception {
                webServerExist = waitForWebserver();
-               Assert.assertTrue("webserver running on port "+testHttpPort, 
webServerExist);
+               Assert.assertTrue("webserver running on port " + testHttpPort,
+                               webServerExist);
                Thread.sleep(10000);
        }
 
+       /**
+        * multiple tests that run within the lifetime of the same instance
+        * 
+        * @throws Exception
+        */
        @Test
-       public void checkTcManagerService()
-                       throws Exception {
-               Object service = waitFor(TcManager.class, 20000);
-               Assert.assertTrue(service != null);
+       public void multi() throws Exception {
+               checkTcManagerService();
+               testJaxRsRegistration();
+       }
 
+       private void testJaxRsRegistration() throws InterruptedException, 
IOException {
+               final Dictionary<String, Object> jaxRsResourceProperty = new 
Hashtable<String, Object>();
+               {
+                       jaxRsResourceProperty.put("javax.ws.rs", Boolean.TRUE);
+                       jaxRsResourceProperty.put("service.pid", 
SimpleRootResource.class.getName());
+               }
+               bundleContext.registerService(Object.class.getName(),
+                               new SimpleRootResource(), 
jaxRsResourceProperty);
+               URL url = new URL("http://localhost:"; + testHttpPort + "/foo");
+               Thread.sleep(4000);
+               requestUrl(url);//"/admin/users/list-users"));
+               runRequestThreads(url);
        }
 
+       private void checkTcManagerService() throws Exception {
+               Object service = waitFor(TcManager.class, 20000);
+               Assert.assertTrue(service != null);
 
+       }
 
        private Object waitFor(Class<?> aClass, long timeout)
                        throws InterruptedException {
@@ -217,8 +249,7 @@ public class FelixClerezzaPlatformTest {
                return service;
        }
 
-       private boolean waitForWebserver()
-                       throws InterruptedException {
+       private boolean waitForWebserver() throws InterruptedException {
                int j = 0;
                try {
                        URL serverURL = new URL("http://localhost:"; + 
testHttpPort + "/");
@@ -237,8 +268,59 @@ public class FelixClerezzaPlatformTest {
                                break;
                        }
                } catch (MalformedURLException ex) {
-                       
Logger.getLogger(FelixClerezzaPlatformTest.class.getName()).log(Level.SEVERE, 
null, ex);
+                       
Logger.getLogger(FelixClerezzaPlatformTest.class.getName()).log(
+                                       Level.SEVERE, null, ex);
                }
                return j < 100;
        }
+
+       private void runRequestThreads(final URL url) throws 
MalformedURLException,
+                       InterruptedException {
+               RequestThread[] requestThread = new 
RequestThread[THREADS_COUNT];
+               for (int i = 0; i < THREADS_COUNT; i++) {
+                       requestThread[i] = new RequestThread(url);
+               }
+               for (int i = 0; i < THREADS_COUNT; i++) {
+                       requestThread[i].start();
+               }
+               int successfulRequests = 0;
+               for (int i = 0; i < THREADS_COUNT; i++) {
+                       requestThread[i].join();
+                       successfulRequests += 
requestThread[i].successfulRequests;
+               }
+               Assert.assertEquals(REQUESTS_PER_THREAD * THREADS_COUNT,
+                               successfulRequests);
+       }
+
+       static class RequestThread extends Thread {
+
+               private URL url;
+               int successfulRequests = 0;
+
+               public RequestThread(URL url) {
+                       this.url = url;
+               }
+
+               @Override
+               public void run() {
+                       try {
+                               for (int i = 0; i < REQUESTS_PER_THREAD; i++) {
+                                       requestUrl(url);
+                                       successfulRequests++;
+                               }
+                       } catch (IOException e) {
+                               // TODO Auto-generated catch block
+                               e.printStackTrace();
+                       }
+
+               }
+       }
+
+       private static void requestUrl(URL url) throws IOException {
+               URLConnection urlConnection = url.openConnection();
+               InputStream in = urlConnection.getInputStream();
+               for (int ch = in.read(); ch != -1; ch = in.read()) {
+                       System.out.print((char) ch);
+               }
+       }
 }

Added: 
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.platform.testing/src/test/java/org/apache/clerezza/platform/testing/blackbox/osgi/RDFListRootResource.java
URL: 
http://svn.apache.org/viewvc/incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.platform.testing/src/test/java/org/apache/clerezza/platform/testing/blackbox/osgi/RDFListRootResource.java?rev=1034127&view=auto
==============================================================================
--- 
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.platform.testing/src/test/java/org/apache/clerezza/platform/testing/blackbox/osgi/RDFListRootResource.java
 (added)
+++ 
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.platform.testing/src/test/java/org/apache/clerezza/platform/testing/blackbox/osgi/RDFListRootResource.java
 Thu Nov 11 21:07:46 2010
@@ -0,0 +1,52 @@
+/*
+ * 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.clerezza.platform.testing.blackbox.osgi;
+
+import javax.ws.rs.GET;
+import javax.ws.rs.Path;
+import javax.ws.rs.Produces;
+
+import org.apache.clerezza.rdf.core.BNode;
+import org.apache.clerezza.rdf.core.TripleCollection;
+import org.apache.clerezza.rdf.core.impl.PlainLiteralImpl;
+import org.apache.clerezza.rdf.core.impl.SimpleMGraph;
+import org.apache.clerezza.rdf.utils.GraphNode;
+import org.apache.clerezza.rdf.utils.RdfList;
+
+/**
+ *
+ * @author reto
+ */
+...@path("/list")
+public class RDFListRootResource {
+
+       static final public String greeting = "hello";
+       
+       @GET
+       @Produces("text/plain")
+       public GraphNode sayHello() {
+               TripleCollection resultGraph = new SimpleMGraph();
+               GraphNode result = new GraphNode(new BNode(), resultGraph);
+               RdfList list = new RdfList(result);
+               for (int i = 0; i < 10 ; i++) {
+                       list.add(new PlainLiteralImpl("number "+i));
+               }
+               return result;
+       }
+}

Added: 
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.platform.testing/src/test/java/org/apache/clerezza/platform/testing/blackbox/osgi/SimpleRootResource.java
URL: 
http://svn.apache.org/viewvc/incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.platform.testing/src/test/java/org/apache/clerezza/platform/testing/blackbox/osgi/SimpleRootResource.java?rev=1034127&view=auto
==============================================================================
--- 
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.platform.testing/src/test/java/org/apache/clerezza/platform/testing/blackbox/osgi/SimpleRootResource.java
 (added)
+++ 
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.platform.testing/src/test/java/org/apache/clerezza/platform/testing/blackbox/osgi/SimpleRootResource.java
 Thu Nov 11 21:07:46 2010
@@ -0,0 +1,39 @@
+/*
+ * 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.clerezza.platform.testing.blackbox.osgi;
+
+import javax.ws.rs.GET;
+import javax.ws.rs.Path;
+import javax.ws.rs.Produces;
+
+/**
+ *
+ * @author reto
+ */
+...@path("/foo")
+public class SimpleRootResource {
+
+       static final public String greeting = "hello";
+       
+       @GET
+       @Produces("text/plain")
+       public String sayHello() {
+               return greeting;
+       }
+}


Reply via email to