FileDocCategorySizeDatePackage
MultithreadTestCase.javaAPI DocApache Axis 1.45121Sat Apr 22 18:57:26 BST 2006test.wsdl.multithread

MultithreadTestCase

public class MultithreadTestCase extends TestCase
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.

Fields Summary
private static Log
log
private samples.addr.AddressBook
binding
private static int
successCount
private AssertionFailedError
error
private static int
var
Constructors Summary
public MultithreadTestCase(String name)

        super(name);
    
Methods Summary
static synchronized voidaddSuccess()


       
    
        successCount++;
    
public static voidmain(java.lang.String[] args)

        MultithreadTestCase testCase = new MultithreadTestCase("MultithreadTestCase");
        testCase.testMultithreading();
    
private java.lang.StringprintAddress(samples.addr.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;
    
private java.lang.StringprintPhone(samples.addr.Phone ph)

        String out;
        if (ph == null)
            out = "[PHONE NUMBER NOT FOUND!]";
        else
            out ="Phone: (" + ph.getAreaCode () + ") " + ph.getExchange () + "-" + ph.getNumber ();
        return out;
    
private synchronized voidsetError(junit.framework.AssertionFailedError error)


         
        if (this.error == null) {
            this.error = error;
        }
    
public voidtestMultithreading()

        try {
            binding = new AddressBookServiceLocator().getAddressBook();
        }
        catch (ServiceException jre) {
            throw new AssertionFailedError("ServiceException caught: " + jre);
        }
        assertTrue("binding is null", binding != null);
        ((AddressBookSOAPBindingStub) binding).setMaintainSession(true);
        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].start();
        }
        for (int i = 0; i < NUM_THREADS; ++i) {
            try {
                threads[i].join();
            }
            catch (InterruptedException ie) {
            }
        }
        System.out.println("Had " + successCount +
                           " successes (of a possible " +
                           (NUM_THREADS * 4) + ")");
        if (error != null) {
            throw error;
        }