KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > wsdl > multithread > MultithreadTestCase


1 package test.wsdl.multithread;
2
3 import junit.framework.AssertionFailedError;
4 import junit.framework.TestCase;
5 import org.apache.axis.AxisFault;
6 import org.apache.axis.components.logger.LogFactory;
7 import org.apache.commons.logging.Log;
8 import samples.addr.Address;
9 import samples.addr.AddressBook;
10 import samples.addr.AddressBookSOAPBindingStub;
11 import samples.addr.AddressBookServiceLocator;
12 import samples.addr.Phone;
13 import samples.addr.StateType;
14
15 import javax.xml.rpc.ServiceException JavaDoc;
16 import java.net.ConnectException JavaDoc;
17
18 /**
19 * This test calls the stub multiple times from multiple threads. Before the
20 * stub was made threadsafe, there was a good chance this test would fail with an
21 * IllegalStateException or "javax.xml.rpc.ServiceException: Number of parameters
22 * passed in (2) doesn't match the number of IN/INOUT parameters (4) from the
23 * addParameter() calls" or something else just as cryptic.
24 */

25
26 public class MultithreadTestCase extends TestCase {
27     private static Log log =
28             LogFactory.getLog(MultithreadTestCase.class.getName());
29
30     private AddressBook binding;
31     private static int successCount = 0;
32
33     static synchronized void addSuccess()
34     {
35         successCount++;
36     }
37
38     public MultithreadTestCase(String JavaDoc name) {
39         super(name);
40     }
41
42     private String JavaDoc printAddress (Address ad) {
43         String JavaDoc out;
44         if (ad == null)
45             out = "\t[ADDRESS NOT FOUND!]";
46         else
47             out ="\t" + ad.getStreetNum () + " " + ad.getStreetName () + "\n\t" + ad.getCity () + ", " + ad.getState () + " " + ad.getZip () + "\n\t" + printPhone (ad.getPhoneNumber ());
48         return out;
49     } // printAddress
50

51     private String JavaDoc printPhone (Phone ph)
52     {
53         String JavaDoc out;
54         if (ph == null)
55             out = "[PHONE NUMBER NOT FOUND!]";
56         else
57             out ="Phone: (" + ph.getAreaCode () + ") " + ph.getExchange () + "-" + ph.getNumber ();
58         return out;
59     } // printPhone
60

61     private AssertionFailedError error = null;
62
63     private synchronized void setError(AssertionFailedError error) {
64         if (this.error == null) {
65             this.error = error;
66         }
67     } // setError
68

69     private static int var = 0;
70
71     public class Run implements Runnable JavaDoc {
72         public void run() {
73             try {
74                 for (int i = 0; i < 4; ++i) {
75                     Address address = new Address();
76                     Phone phone = new Phone();
77                     address.setStreetNum(var++);
78                     address.setStreetName("2");
79                     address.setCity("3");
80                     address.setState(StateType.TX);
81                     address.setZip(var++);
82                     phone.setAreaCode(11);
83                     phone.setExchange("22");
84                     phone.setNumber("33");
85                     address.setPhoneNumber(phone);
86                     
87                     binding.addEntry("hi", address);
88                     Address addressRet = binding.getAddressFromName("hi");
89                     // succeeded, count it.
90
addSuccess();
91                 }
92             } catch (Throwable JavaDoc t) {
93                 // There are bound to be connection refused exceptions when the
94
// server socket is busy) in a multithreaded environment. I
95
// don't want to deal with those. Only grab exceptions that are
96
// likely to have something to do with bad AXIS runtime.
97
if (!(t instanceof AxisFault &&
98                         ((AxisFault) t).detail instanceof ConnectException JavaDoc)) {
99
100                     // Log a stack trace as we may not be so lucky next time!
101
log.fatal("Throwable caught: ", t);
102
103                     setError(new AssertionFailedError("Throwable caught: " + t));
104                 }
105             }
106         } // run
107
} // class Run
108

109     public void testMultithreading() {
110         try {
111             binding = new AddressBookServiceLocator().getAddressBook();
112         }
113         catch (ServiceException JavaDoc jre) {
114             throw new AssertionFailedError("ServiceException caught: " + jre);
115         }
116         assertTrue("binding is null", binding != null);
117         ((AddressBookSOAPBindingStub) binding).setMaintainSession(true);
118         int NUM_THREADS = 50;
119         Thread JavaDoc[] threads = new Thread JavaDoc[NUM_THREADS];
120         for (int i = 0; i < NUM_THREADS; ++i) {
121             threads[i] = new Thread JavaDoc(new Run());
122             threads[i].start();
123         }
124         for (int i = 0; i < NUM_THREADS; ++i) {
125             try {
126                 threads[i].join();
127             }
128             catch (InterruptedException JavaDoc ie) {
129             }
130         }
131         System.out.println("Had " + successCount +
132                            " successes (of a possible " +
133                            (NUM_THREADS * 4) + ")");
134         if (error != null) {
135             throw error;
136         }
137     } // testMultithreading
138

139     public static void main(String JavaDoc[] args) {
140         MultithreadTestCase testCase = new MultithreadTestCase("MultithreadTestCase");
141         testCase.testMultithreading();
142     }
143 } // class MultithreadTestCase
144

145
Popular Tags