You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cxf.apache.org by dk...@apache.org on 2011/12/08 20:11:30 UTC
svn commit: r1212038 - in /cxf/trunk:
rt/frontend/jaxws/src/test/java/org/apache/cxf/jaxws/JaxWsClientTest.java
systests/jaxws/src/test/java/org/apache/cxf/systest/jaxws/JaxWsClientThreadTest.java
Author: dkulp
Date: Thu Dec 8 19:11:29 2011
New Revision: 1212038
URL: http://svn.apache.org/viewvc?rev=1212038&view=rev
Log:
[CXF-3910] Add test cases for thread local request contexts for dispatch
clients
Modified:
cxf/trunk/rt/frontend/jaxws/src/test/java/org/apache/cxf/jaxws/JaxWsClientTest.java
cxf/trunk/systests/jaxws/src/test/java/org/apache/cxf/systest/jaxws/JaxWsClientThreadTest.java
Modified: cxf/trunk/rt/frontend/jaxws/src/test/java/org/apache/cxf/jaxws/JaxWsClientTest.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxws/src/test/java/org/apache/cxf/jaxws/JaxWsClientTest.java?rev=1212038&r1=1212037&r2=1212038&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxws/src/test/java/org/apache/cxf/jaxws/JaxWsClientTest.java (original)
+++ cxf/trunk/rt/frontend/jaxws/src/test/java/org/apache/cxf/jaxws/JaxWsClientTest.java Thu Dec 8 19:11:29 2011
@@ -26,7 +26,9 @@ import java.util.Map;
import java.util.ResourceBundle;
import javax.xml.namespace.QName;
+import javax.xml.transform.dom.DOMSource;
import javax.xml.ws.BindingProvider;
+import javax.xml.ws.Dispatch;
import javax.xml.ws.WebServiceException;
import org.apache.cxf.endpoint.Client;
@@ -112,8 +114,7 @@ public class JaxWsClientTest extends Abs
URL url = getClass().getResource("/wsdl/hello_world.wsdl");
javax.xml.ws.Service s = javax.xml.ws.Service
.create(url, serviceName);
- Greeter greeter = s.getPort(portName, Greeter.class);
- final InvocationHandler handler = Proxy.getInvocationHandler(greeter);
+ final Greeter handler = s.getPort(portName, Greeter.class);
Map<String, Object> requestContext = ((BindingProvider)handler).getRequestContext();
requestContext.put(JaxWsClientProxy.THREAD_LOCAL_REQUEST_CONTEXT, Boolean.TRUE);
@@ -143,6 +144,44 @@ public class JaxWsClientTest extends Abs
assertEquals("main thread does not see removal",
"ho", requestContext.get(key));
}
+ @Test
+ public void testRequestContextPutAndRemoveEchoDispatch() throws Exception {
+ URL url = getClass().getResource("/wsdl/hello_world.wsdl");
+ javax.xml.ws.Service s = javax.xml.ws.Service
+ .create(url, serviceName);
+
+ final Dispatch<DOMSource> disp = s.createDispatch(portName, DOMSource.class,
+ javax.xml.ws.Service.Mode.PAYLOAD);
+
+
+ Map<String, Object> requestContext = disp.getRequestContext();
+ requestContext.put(JaxWsClientProxy.THREAD_LOCAL_REQUEST_CONTEXT, Boolean.TRUE);
+
+ //re-get the context so it's not a thread safe variant
+ requestContext = disp.getRequestContext();
+
+ final String key = "Hi";
+
+ requestContext.put(key, "ho");
+
+ final Object[] result = new Object[2];
+ Thread t = new Thread() {
+ public void run() {
+ Map<String, Object> requestContext = disp.getRequestContext();
+ result[0] = requestContext.get(key);
+ requestContext.remove(key);
+ result[1] = requestContext.get(key);
+ }
+ };
+ t.start();
+ t.join();
+
+ assertEquals("thread sees the put", "ho", result[0]);
+ assertNull("thread did not remove the put", result[1]);
+
+ assertEquals("main thread does not see removal",
+ "ho", requestContext.get(key));
+ }
@Test
public void testEndpoint() throws Exception {
Modified: cxf/trunk/systests/jaxws/src/test/java/org/apache/cxf/systest/jaxws/JaxWsClientThreadTest.java
URL: http://svn.apache.org/viewvc/cxf/trunk/systests/jaxws/src/test/java/org/apache/cxf/systest/jaxws/JaxWsClientThreadTest.java?rev=1212038&r1=1212037&r2=1212038&view=diff
==============================================================================
--- cxf/trunk/systests/jaxws/src/test/java/org/apache/cxf/systest/jaxws/JaxWsClientThreadTest.java (original)
+++ cxf/trunk/systests/jaxws/src/test/java/org/apache/cxf/systest/jaxws/JaxWsClientThreadTest.java Thu Dec 8 19:11:29 2011
@@ -25,8 +25,11 @@ import java.net.MalformedURLException;
import java.net.URL;
import java.util.Map;
+import javax.xml.bind.JAXBContext;
import javax.xml.namespace.QName;
import javax.xml.ws.BindingProvider;
+import javax.xml.ws.Dispatch;
+import javax.xml.ws.Service;
import javax.xml.ws.WebServiceException;
import org.apache.cxf.endpoint.ClientImpl;
@@ -123,5 +126,93 @@ public class JaxWsClientThreadTest exten
assertTrue("property is null from last thread execution", requestContext
.get(BindingProvider.ENDPOINT_ADDRESS_PROPERTY) == null);
}
+
+
+
+ @Test
+ public void testRequestContextThreadSafetyDispatch() throws Throwable {
+
+ URL url = getClass().getResource("/wsdl/hello_world.wsdl");
+ javax.xml.ws.Service s = javax.xml.ws.Service.create(url, serviceName);
+ JAXBContext c = JAXBContext.newInstance(org.apache.hello_world_soap_http.types.ObjectFactory.class);
+ final Dispatch<Object> disp = s.createDispatch(portName, c, Service.Mode.PAYLOAD);
+
+ disp.getRequestContext().put(JaxWsClientProxy.THREAD_LOCAL_REQUEST_CONTEXT,
+ Boolean.TRUE);
+
+ Map<String, Object> requestContext = disp.getRequestContext();
+
+ String address = (String)requestContext.get(BindingProvider.ENDPOINT_ADDRESS_PROPERTY);
+
+ final Throwable errorHolder[] = new Throwable[1];
+
+ Runnable r = new Runnable() {
+ public void run() {
+ try {
+ final String protocol = "http-" + Thread.currentThread().getId();
+ for (int i = 0; i < 10; i++) {
+ String threadSpecificaddress = protocol + "://localhost:80/" + i;
+ Map<String, Object> requestContext = disp.getRequestContext();
+ requestContext.put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY,
+ threadSpecificaddress);
+ assertEquals("we get what we set", threadSpecificaddress, requestContext
+ .get(BindingProvider.ENDPOINT_ADDRESS_PROPERTY));
+ try {
+ org.apache.hello_world_soap_http.types.GreetMe gm
+ = new org.apache.hello_world_soap_http.types.GreetMe();
+ gm.setRequestType("Hi");
+ disp.invoke(gm);
+ } catch (WebServiceException expected) {
+ //expected.getCause().printStackTrace();
+ MalformedURLException mue = (MalformedURLException)expected
+ .getCause();
+ if (mue == null || mue.getMessage() == null) {
+ throw expected;
+ }
+ assertTrue("protocol contains thread id from context", mue.getMessage()
+ .indexOf(protocol) != 0);
+ }
+
+ requestContext.remove(BindingProvider.ENDPOINT_ADDRESS_PROPERTY);
+ assertTrue("property is null", requestContext
+ .get(BindingProvider.ENDPOINT_ADDRESS_PROPERTY) == null);
+
+ }
+ } catch (Throwable t) {
+ // capture assert failures
+ errorHolder[0] = t;
+ }
+ }
+ };
+
+ final int numThreads = 5;
+ Thread[] threads = new Thread[numThreads];
+ for (int i = 0; i < numThreads; i++) {
+ threads[i] = new Thread(r);
+ }
+ for (int i = 0; i < numThreads; i++) {
+ threads[i].start();
+ }
+ for (int i = 0; i < numThreads; i++) {
+ threads[i].join();
+ }
+ if (errorHolder[0] != null) {
+ throw errorHolder[0];
+ }
+
+ // main thread contextValues are un changed
+ assertTrue("address from existing context has not changed", address.equals(requestContext
+ .get(BindingProvider.ENDPOINT_ADDRESS_PROPERTY)));
+
+ // get the latest values
+
+ ((ClientImpl.EchoContext)((WrappedMessageContext)requestContext).getWrappedMap()).reload();
+ assertTrue("address is different", !address.equals(requestContext
+ .get(BindingProvider.ENDPOINT_ADDRESS_PROPERTY)));
+ // verify value reflects what other threads were doing
+ assertTrue("property is null from last thread execution", requestContext
+ .get(BindingProvider.ENDPOINT_ADDRESS_PROPERTY) == null);
+ }
+
}