Author: veithen
Date: Fri Oct 19 20:49:46 2012
New Revision: 1400282

URL: http://svn.apache.org/viewvc?rev=1400282&view=rev
Log:
Cleaned up the code in MultithreadTestCase and extended it to cover the 
scenario described in AXIS-2498.

Added:
    
axis/axis1/java/trunk/integration/src/test/java/test/wsdl/multithread/Invoker.java
   (with props)
    
axis/axis1/java/trunk/integration/src/test/java/test/wsdl/multithread/Report.java
   (with props)
    
axis/axis1/java/trunk/integration/src/test/java/test/wsdl/multithread/StubSupplier.java
   (with props)
Modified:
    
axis/axis1/java/trunk/integration/src/test/java/test/wsdl/multithread/MultithreadTestCase.java

Added: 
axis/axis1/java/trunk/integration/src/test/java/test/wsdl/multithread/Invoker.java
URL: 
http://svn.apache.org/viewvc/axis/axis1/java/trunk/integration/src/test/java/test/wsdl/multithread/Invoker.java?rev=1400282&view=auto
==============================================================================
--- 
axis/axis1/java/trunk/integration/src/test/java/test/wsdl/multithread/Invoker.java
 (added)
+++ 
axis/axis1/java/trunk/integration/src/test/java/test/wsdl/multithread/Invoker.java
 Fri Oct 19 20:49:46 2012
@@ -0,0 +1,67 @@
+/*
+ * 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 test.wsdl.multithread;
+
+import org.apache.axis.components.logger.LogFactory;
+import org.apache.commons.logging.Log;
+
+import samples.addr.Address;
+import samples.addr.AddressBook;
+import samples.addr.Phone;
+import samples.addr.StateType;
+
+class Invoker implements Runnable {
+    private static Log log = LogFactory.getLog(Invoker.class.getName());
+    
+    private final AddressBook binding;
+    private final Report report;
+    
+    Invoker(AddressBook binding, Report report) {
+        this.binding = binding;
+        this.report = report;
+    }
+
+    public void run() {
+        try {
+            for (int i = 0; i < 4; ++i) {
+                Address address = new Address();
+                Phone phone = new Phone();
+                address.setStreetNum(i);
+                address.setStreetName("2");
+                address.setCity("3");
+                address.setState(StateType.TX);
+                address.setZip(i);
+                phone.setAreaCode(11);
+                phone.setExchange("22");
+                phone.setNumber("33");
+                address.setPhoneNumber(phone);
+                
+                binding.addEntry("hi", address); 
+                Address addressRet = binding.getAddressFromName("hi");
+                // succeeded, count it.
+                report.addSuccess();
+            }
+        } catch (Throwable t) {
+            // Log a stack trace as we may not be so lucky next time!
+            log.fatal("Throwable caught: ", t);
+
+            report.setError(t);
+        }
+    } // run
+}
\ No newline at end of file

Propchange: 
axis/axis1/java/trunk/integration/src/test/java/test/wsdl/multithread/Invoker.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: 
axis/axis1/java/trunk/integration/src/test/java/test/wsdl/multithread/MultithreadTestCase.java
URL: 
http://svn.apache.org/viewvc/axis/axis1/java/trunk/integration/src/test/java/test/wsdl/multithread/MultithreadTestCase.java?rev=1400282&r1=1400281&r2=1400282&view=diff
==============================================================================
--- 
axis/axis1/java/trunk/integration/src/test/java/test/wsdl/multithread/MultithreadTestCase.java
 (original)
+++ 
axis/axis1/java/trunk/integration/src/test/java/test/wsdl/multithread/MultithreadTestCase.java
 Fri Oct 19 20:49:46 2012
@@ -1,126 +1,52 @@
 package test.wsdl.multithread;
 
-import junit.framework.AssertionFailedError;
 import junit.framework.TestCase;
-import org.apache.axis.AxisFault;
-import org.apache.axis.components.logger.LogFactory;
-import org.apache.commons.logging.Log;
-import samples.addr.Address;
 import samples.addr.AddressBook;
 import samples.addr.AddressBookSOAPBindingStub;
 import samples.addr.AddressBookServiceLocator;
-import samples.addr.Phone;
-import samples.addr.StateType;
 import test.HttpTestUtil;
 
-import javax.xml.rpc.ServiceException;
-import java.net.ConnectException;
-
-/**
-* This test calls the stub multiple times from multiple threads.  Before the
-* stub was made threadsafe, there was a good chance this test would fail with 
an
-* IllegalStateException or "javax.xml.rpc.ServiceException: Number of 
parameters
-* passed in (2) doesn't match the number of IN/INOUT parameters (4) from the
-* addParameter() calls" or something else just as cryptic.
-*/
-
 public class MultithreadTestCase extends TestCase {
-    private static Log log =
-            LogFactory.getLog(MultithreadTestCase.class.getName());
-
-    private AddressBook binding;
-    private static int successCount = 0;
-
-    static synchronized void addSuccess()
-    {
-        successCount++;
-    }
-
-    public MultithreadTestCase(String name) {
-        super(name);
+    /**
+     * This test calls a single stub multiple times from multiple threads.  
Before the
+     * stub was made threadsafe, there was a good chance this test would fail 
with an
+     * IllegalStateException or "javax.xml.rpc.ServiceException: Number of 
parameters
+     * passed in (2) doesn't match the number of IN/INOUT parameters (4) from 
the
+     * addParameter() calls" or something else just as cryptic.
+     */
+    public void testSingleStub() throws Throwable {
+        AddressBookServiceLocator loc = new AddressBookServiceLocator();
+        final AddressBook binding = 
loc.getAddressBook(HttpTestUtil.getTestEndpoint(loc.getAddressBookAddress()));
+        ((AddressBookSOAPBindingStub) binding).setMaintainSession(true);
+        testMultithreading(new StubSupplier() {
+            public AddressBook getStub() throws Exception {
+                return binding;
+            }
+        });
     }
 
-    private String printAddress (Address ad) {
-        String out;
-        if (ad == null)
-            out = "\t[ADDRESS NOT FOUND!]";
-        else
-            out ="\t" + ad.getStreetNum () + " " + ad.getStreetName () + 
"\n\t" + ad.getCity () + ", " + ad.getState () + " " + ad.getZip () + "\n\t" + 
printPhone (ad.getPhoneNumber ());
-        return out;
-    } // printAddress
-
-    private String printPhone (Phone ph)
-    {
-        String out;
-        if (ph == null)
-            out = "[PHONE NUMBER NOT FOUND!]";
-        else
-            out ="Phone: (" + ph.getAreaCode () + ") " + ph.getExchange () + 
"-" + ph.getNumber ();
-        return out;
-    } // printPhone
-
-    private AssertionFailedError error = null;
-
-    private synchronized void setError(AssertionFailedError error) {
-        if (this.error == null) {
-            this.error = error;
-        }
-    } // setError
-
-    private static int var = 0;
-
-    public class Run implements Runnable {
-        public void run() {
-            try {
-                for (int i = 0; i < 4; ++i) {
-                    Address address = new Address();
-                    Phone phone = new Phone();
-                    address.setStreetNum(var++);
-                    address.setStreetName("2");
-                    address.setCity("3");
-                    address.setState(StateType.TX);
-                    address.setZip(var++);
-                    phone.setAreaCode(11);
-                    phone.setExchange("22");
-                    phone.setNumber("33");
-                    address.setPhoneNumber(phone);
-                    
-                    binding.addEntry("hi", address); 
-                    Address addressRet = binding.getAddressFromName("hi");
-                    // succeeded, count it.
-                    addSuccess();
-                }
-            } catch (Throwable t) {
-                // There are bound to be connection refused exceptions when the
-                // server socket is busy) in a multithreaded environment.  I
-                // don't want to deal with those.  Only grab exceptions that 
are
-                // likely to have something to do with bad AXIS runtime.
-                if (!(t instanceof AxisFault &&
-                        ((AxisFault) t).detail instanceof ConnectException)) {
-
-                    // Log a stack trace as we may not be so lucky next time!
-                    log.fatal("Throwable caught: ", t);
-
-                    setError(new AssertionFailedError("Throwable caught: " + 
t));
-                }
+    /**
+     * Tests concurrent invocations of different stubs (one per thread) 
created from a single
+     * locator. This tests the scenario described in <a
+     * href="https://issues.apache.org/jira/browse/AXIS-2498";>AXIS-2498</a>.
+     */
+    public void testSingleLocator() throws Throwable {
+        final AddressBookServiceLocator loc = new AddressBookServiceLocator();
+        testMultithreading(new StubSupplier() {
+            public AddressBook getStub() throws Exception {
+                AddressBook binding = 
loc.getAddressBook(HttpTestUtil.getTestEndpoint(loc.getAddressBookAddress()));
+                ((AddressBookSOAPBindingStub) 
binding).setMaintainSession(true);
+                return binding;
             }
-        } // run
-    } // class Run
-
-    public void testMultithreading() throws Exception {
-        try {
-            AddressBookServiceLocator loc = new AddressBookServiceLocator();
-            binding = 
loc.getAddressBook(HttpTestUtil.getTestEndpoint(loc.getAddressBookAddress()));
-        }
-        catch (ServiceException jre) {
-            throw new AssertionFailedError("ServiceException caught: " + jre);
-        }
-        assertTrue("binding is null", binding != null);
-        ((AddressBookSOAPBindingStub) binding).setMaintainSession(true);
+        });
+    }
+    
+    private void testMultithreading(StubSupplier stubSupplier) throws 
Throwable {
+        Report report = new Report();
         int NUM_THREADS = 50;
         Thread[] threads = new Thread[NUM_THREADS];
         for (int i = 0; i < NUM_THREADS; ++i) {
-            threads[i] = new Thread(new Run());
+            threads[i] = new Thread(new Invoker(stubSupplier.getStub(), 
report));
             threads[i].start();
         }
         for (int i = 0; i < NUM_THREADS; ++i) {
@@ -130,12 +56,11 @@ public class MultithreadTestCase extends
             catch (InterruptedException ie) {
             }
         }
-        System.out.println("Had " + successCount +
-                           " successes (of a possible " +
-                           (NUM_THREADS * 4) + ")");
+        Throwable error = report.getError();
         if (error != null) {
             throw error;
         }
+        assertEquals("number of successes", NUM_THREADS * 4, 
report.getSuccessCount());
     } // testMultithreading
 } // class MultithreadTestCase
 

Added: 
axis/axis1/java/trunk/integration/src/test/java/test/wsdl/multithread/Report.java
URL: 
http://svn.apache.org/viewvc/axis/axis1/java/trunk/integration/src/test/java/test/wsdl/multithread/Report.java?rev=1400282&view=auto
==============================================================================
--- 
axis/axis1/java/trunk/integration/src/test/java/test/wsdl/multithread/Report.java
 (added)
+++ 
axis/axis1/java/trunk/integration/src/test/java/test/wsdl/multithread/Report.java
 Fri Oct 19 20:49:46 2012
@@ -0,0 +1,45 @@
+/*
+ * 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 test.wsdl.multithread;
+
+/**
+ * Collects the results from a set of {@link Invoker} instances.
+ */
+class Report {
+    private int successCount = 0;
+    private Throwable error;
+
+    synchronized void addSuccess() {
+        successCount++;
+    }
+
+    synchronized int getSuccessCount() {
+        return successCount;
+    }
+
+    synchronized void setError(Throwable error) {
+        if (this.error == null) {
+            this.error = error;
+        }
+    }
+
+    synchronized Throwable getError() {
+        return error;
+    }
+}

Propchange: 
axis/axis1/java/trunk/integration/src/test/java/test/wsdl/multithread/Report.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: 
axis/axis1/java/trunk/integration/src/test/java/test/wsdl/multithread/StubSupplier.java
URL: 
http://svn.apache.org/viewvc/axis/axis1/java/trunk/integration/src/test/java/test/wsdl/multithread/StubSupplier.java?rev=1400282&view=auto
==============================================================================
--- 
axis/axis1/java/trunk/integration/src/test/java/test/wsdl/multithread/StubSupplier.java
 (added)
+++ 
axis/axis1/java/trunk/integration/src/test/java/test/wsdl/multithread/StubSupplier.java
 Fri Oct 19 20:49:46 2012
@@ -0,0 +1,29 @@
+/*
+ * 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 test.wsdl.multithread;
+
+import samples.addr.AddressBook;
+
+/**
+ * Supplies a stub instance to a given {@link Invoker}. Depending on the 
implementation this may be
+ * a singleton or a new stub for each {@link Invoker}.
+ */
+interface StubSupplier {
+    AddressBook getStub() throws Exception;
+}

Propchange: 
axis/axis1/java/trunk/integration/src/test/java/test/wsdl/multithread/StubSupplier.java
------------------------------------------------------------------------------
    svn:eol-style = native


Reply via email to