KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > celtix > systest > stress > concurrency > ConcurrentInvokerTest


1 package org.objectweb.celtix.systest.stress.concurrency;
2
3 import java.net.URL JavaDoc;
4
5 import javax.xml.namespace.QName JavaDoc;
6
7 import junit.framework.Test;
8 import junit.framework.TestSuite;
9
10 import org.objectweb.celtix.systest.common.ClientServerSetupBase;
11 import org.objectweb.celtix.systest.common.ClientServerTestBase;
12 import org.objectweb.hello_world_soap_http.BadRecordLitFault;
13 import org.objectweb.hello_world_soap_http.Greeter;
14 import org.objectweb.hello_world_soap_http.NoSuchCodeLitFault;
15 import org.objectweb.hello_world_soap_http.SOAPService;
16
17 public class ConcurrentInvokerTest extends ClientServerTestBase {
18
19     static final int INVOKER_COUNT = 5;
20     static final int INVOCATION_REPS = 50;
21     static final int EXPECTED_CALLS = INVOKER_COUNT * INVOCATION_REPS;
22
23     Greeter greeter;
24     private final QName JavaDoc serviceName = new QName JavaDoc("http://objectweb.org/hello_world_soap_http",
25                                                 "SOAPServiceConcurrencyTest");
26     private final QName JavaDoc portName = new QName JavaDoc("http://objectweb.org/hello_world_soap_http", "SoapPort");
27
28     public static void main(String JavaDoc[] args) {
29         junit.textui.TestRunner.run(ConcurrentInvokerTest.class);
30     }
31     public static Test suite() throws Exception JavaDoc {
32         TestSuite suite = new TestSuite(ConcurrentInvokerTest.class);
33         return new ClientServerSetupBase(suite) {
34             public void startServers() throws Exception JavaDoc {
35                 assertTrue("server did not launch correctly", launchServer(Server.class));
36             }
37         };
38     }
39
40     public void setUp() throws Exception JavaDoc {
41         super.setUp();
42         URL JavaDoc wsdl = getClass().getResource("/wsdl/hello_world.wsdl");
43         SOAPService service = new SOAPService(wsdl, serviceName);
44         greeter = service.getPort(portName, Greeter.class);
45     }
46
47     public void testConcurrentInvocation() throws Exception JavaDoc {
48         waitFor(launch(populateInvokers()));
49     }
50
51     private Thread JavaDoc[] populateInvokers() {
52         Thread JavaDoc[] invokers = new Thread JavaDoc[INVOKER_COUNT * 3];
53         for (int i = 0; i < INVOKER_COUNT * 3; i++) {
54             String JavaDoc s = "Invoker[" + i + "]";
55             switch (i / INVOKER_COUNT) {
56             case 0:
57                 invokers[i] = new Thread JavaDoc(new TwoWayInvoker(), "TwoWay" + s);
58                 break;
59             case 1:
60                 invokers[i] = new Thread JavaDoc(new OneWayInvoker(), "OneWay" + s);
61                 break;
62             case 2:
63                 invokers[i] = new Thread JavaDoc(new FaultInvoker(), "Fault" + s);
64                 break;
65             default:
66             }
67         }
68         return invokers;
69     }
70
71     private Thread JavaDoc[] launch(Thread JavaDoc[] invokers) {
72         for (int i = 0; i < invokers.length; i++) {
73             invokers[i].start();
74         }
75         return invokers;
76     }
77
78     private void waitFor(Thread JavaDoc[] invokers) {
79         for (int i = 0; i < invokers.length; i++) {
80             try {
81                 invokers[i].join();
82             } catch (InterruptedException JavaDoc ie) {
83                 // ignore
84
}
85         }
86         // stop the server here, instead of relying on the shutdown hook
87
// installed by the base class, as this would be too late to assert
88
// via junit that server received the expected number of calls
89
//assertTrue("server failed, see log for details", stopAllServers());
90
}
91
92     private class TwoWayInvoker implements Runnable JavaDoc {
93         public void run() {
94             String JavaDoc root = Thread.currentThread().getName() + " call: ";
95             for (int i = 0; i < INVOCATION_REPS; i++) {
96                 String JavaDoc in = root + i;
97                 String JavaDoc greeting = greeter.greetMe(in);
98                 assertNotNull("no response received from service", greeting);
99                 assertEquals("Hello " + in, greeting);
100                 String JavaDoc hi = greeter.sayHi();
101                 assertNotNull("no response received from service", hi);
102                 assertEquals("Hiya", hi);
103             }
104         }
105     }
106
107     private class OneWayInvoker implements Runnable JavaDoc {
108         public void run() {
109             String JavaDoc root = Thread.currentThread().getName() + " call: ";
110             for (int i = 0; i < INVOCATION_REPS; i++) {
111                 String JavaDoc in = root + i;
112                 greeter.greetMeOneWay(in);
113             }
114         }
115     }
116
117     private class FaultInvoker implements Runnable JavaDoc {
118         public void run() {
119             String JavaDoc fault = NoSuchCodeLitFault.class.getSimpleName();
120             for (int i = 0; i < INVOCATION_REPS; i++) {
121                 try {
122                     greeter.testDocLitFault(fault);
123                     fail("Should have thrown NoSuchCodeLitFault exception");
124                 } catch (NoSuchCodeLitFault nslf) {
125                     assertNotNull(nslf.getFaultInfo());
126                     assertNotNull(nslf.getFaultInfo().getCode());
127                 } catch (BadRecordLitFault brlf) {
128                     fail("unexpected fault: " + brlf);
129                 }
130             }
131         }
132     }
133 }
134
Popular Tags