cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject cxf git commit: [CXF-6389] Fixing AsyncResponse.isSuspended, patch from Iris Ding applied
Date Thu, 07 May 2015 11:51:15 GMT
Repository: cxf
Updated Branches:
  refs/heads/master 97ab64496 -> 6187cb922


[CXF-6389] Fixing AsyncResponse.isSuspended, patch from Iris Ding applied


Project: http://git-wip-us.apache.org/repos/asf/cxf/repo
Commit: http://git-wip-us.apache.org/repos/asf/cxf/commit/6187cb92
Tree: http://git-wip-us.apache.org/repos/asf/cxf/tree/6187cb92
Diff: http://git-wip-us.apache.org/repos/asf/cxf/diff/6187cb92

Branch: refs/heads/master
Commit: 6187cb9223501a6a03e3d9dfe6490949aa9869fc
Parents: 97ab644
Author: Sergey Beryozkin <sberyozkin@talend.com>
Authored: Thu May 7 12:50:52 2015 +0100
Committer: Sergey Beryozkin <sberyozkin@talend.com>
Committed: Thu May 7 12:50:52 2015 +0100

----------------------------------------------------------------------
 .../cxf/jaxrs/impl/AsyncResponseImpl.java       |   3 +
 .../apache/cxf/systest/jaxrs/AsyncResource.java | 106 +++++++++++++++++++
 .../jaxrs/JAXRSContinuationsServlet3Test.java   |  43 +++++++-
 .../resources/jaxrs_async/WEB-INF/beans.xml     |   1 +
 4 files changed, 152 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cxf/blob/6187cb92/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/AsyncResponseImpl.java
----------------------------------------------------------------------
diff --git a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/AsyncResponseImpl.java
b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/AsyncResponseImpl.java
index c21e528..60c820b 100644
--- a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/AsyncResponseImpl.java
+++ b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/AsyncResponseImpl.java
@@ -132,6 +132,9 @@ public class AsyncResponseImpl implements AsyncResponse, ContinuationCallback
{
 
     @Override
     public synchronized boolean isSuspended() {
+        if (cancelled || resumedByApplication) {
+            return false;
+        }
         return initialSuspend || cont.isPending();
     }
 

http://git-wip-us.apache.org/repos/asf/cxf/blob/6187cb92/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/AsyncResource.java
----------------------------------------------------------------------
diff --git a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/AsyncResource.java
b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/AsyncResource.java
new file mode 100644
index 0000000..c5b9530
--- /dev/null
+++ b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/AsyncResource.java
@@ -0,0 +1,106 @@
+/**
+ * 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 org.apache.cxf.systest.jaxrs;
+
+import java.util.Queue;
+import java.util.concurrent.ArrayBlockingQueue;
+
+import javax.ws.rs.GET;
+import javax.ws.rs.POST;
+import javax.ws.rs.Path;
+import javax.ws.rs.QueryParam;
+import javax.ws.rs.container.AsyncResponse;
+import javax.ws.rs.container.Suspended;
+import javax.ws.rs.core.Response;
+import javax.ws.rs.core.Response.ResponseBuilder;
+import javax.ws.rs.core.Response.Status;
+
+@Path("resource")
+public class AsyncResource {
+
+    public static final String RESUMED = "Response resumed";
+    public static final String FALSE = "A method returned false";
+    public static final String TRUE = "A method return true";
+
+    private static final AsyncResponseQueue[] ASYNC_RESPONSES = { 
+        new AsyncResponseQueue(), new AsyncResponseQueue(), new AsyncResponseQueue() };
+
+    @GET
+    @Path("suspend")
+    public void suspend(@Suspended AsyncResponse asyncResponse) { 
+        ASYNC_RESPONSES[0].add(asyncResponse); 
+    }
+    
+    @GET
+    @Path("cancelvoid")
+    public String cancel(@QueryParam("stage") String stage) { 
+        AsyncResponse response = takeAsyncResponse(stage); 
+        boolean ret = response.cancel(); 
+        ret &= response.cancel(); 
+        addResponse(response, stage); 
+        return ret ? TRUE : FALSE; 
+    }
+    
+    @POST
+    @Path("resume")
+    public String resume(@QueryParam("stage") String stage, String response) { 
+        AsyncResponse async = takeAsyncResponse(stage); 
+        boolean b = resume(async, response); 
+        addResponse(async, stage); 
+        return b ? TRUE : FALSE; 
+    }
+    
+    protected static AsyncResponse takeAsyncResponse(String stageId) { 
+        return takeAsyncResponse(Integer.parseInt(stageId)); 
+    }
+    
+    protected static AsyncResponse takeAsyncResponse(int stageId) {
+        AsyncResponse asyncResponse = null;
+        asyncResponse = ASYNC_RESPONSES[stageId].take();
+        return asyncResponse;
+    }
+    
+    protected static final void addResponse(AsyncResponse response, String stageId) { 
+        int id = Integer.parseInt(stageId) + 1; 
+        ASYNC_RESPONSES[id].add(response); 
+    }
+    
+    protected static boolean resume(AsyncResponse takenResponse, Object response) { 
+        return takenResponse.resume(response); 
+    }
+    
+    protected static ResponseBuilder createErrorResponseBuilder() { 
+        return Response.status(Status.EXPECTATION_FAILED); 
+    }
+    
+    private static class AsyncResponseQueue {
+        Queue<AsyncResponse> queue = new ArrayBlockingQueue<AsyncResponse>(1);
+    
+        public void add(AsyncResponse asyncResponse) {
+            queue.add(asyncResponse);
+            
+        }
+    
+        public AsyncResponse take() {
+            return queue.remove();
+        }
+        
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/cxf/blob/6187cb92/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSContinuationsServlet3Test.java
----------------------------------------------------------------------
diff --git a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSContinuationsServlet3Test.java
b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSContinuationsServlet3Test.java
index 0a44621..de32d9c 100644
--- a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSContinuationsServlet3Test.java
+++ b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSContinuationsServlet3Test.java
@@ -16,14 +16,22 @@
  * specific language governing permissions and limitations
  * under the License.
  */
-
 package org.apache.cxf.systest.jaxrs;
 
+import java.util.concurrent.Future;
+
+import javax.ws.rs.client.AsyncInvoker;
+import javax.ws.rs.client.ClientBuilder;
+import javax.ws.rs.client.Entity;
+import javax.ws.rs.client.WebTarget;
+import javax.ws.rs.core.MediaType;
 import javax.ws.rs.core.Response;
+import javax.ws.rs.core.Response.Status;
 
 import org.apache.cxf.jaxrs.client.WebClient;
 import org.apache.cxf.jaxrs.model.AbstractResourceInfo;
 import org.apache.cxf.testutil.common.ServerLauncher;
+
 import org.junit.BeforeClass;
 import org.junit.Test;
 
@@ -61,7 +69,40 @@ public class JAXRSContinuationsServlet3Test extends AbstractJAXRSContinuationsTe
         assertTrue("server did not launch correctly", launcher.launchServer());
         Thread.sleep(4000);
     }
+    
+    @Test
+    public void testCancelVoidOnResumedTest() throws Exception { 
+        String base = "http://localhost:" + getPort() + "/async/resource/";
+        String expectedResponse = "Expected response"; 
+        Future<Response> suspend = invokeRequest(base + "suspend"); 
+        Future<Response> resume = invokeRequest(base + "resume?stage=0", expectedResponse);

+        assertString(resume, AsyncResource.TRUE); 
+        assertString(suspend, expectedResponse); 
+        Future<Response> cancel = invokeRequest(base + "cancelvoid?stage=1"); 
+        assertString(cancel, AsyncResource.FALSE); 
+    }
+
+    private static void assertString(Future<Response> future, String check) throws
Exception { 
+        Response response = future.get(); 
+        assertEquals(response.getStatus(), Status.OK.getStatusCode()); 
+        String content = response.readEntity(String.class); 
+        assertEquals(check, content); 
+    }
+
+    private <T> Future<Response> invokeRequest(String resource, T entity) { 
+        AsyncInvoker async = createAsyncInvoker(resource); 
+        return async.post(Entity.entity(entity, MediaType.TEXT_PLAIN_TYPE));
+    }
+
+    private Future<Response> invokeRequest(String resource) { 
+        AsyncInvoker async = createAsyncInvoker(resource); 
+        return async.get(); 
+    }
 
+    private AsyncInvoker createAsyncInvoker(String resource) { 
+        WebTarget target = ClientBuilder.newClient().target(resource); 
+        return target.request().async();
+    } 
     
     protected String getBaseAddress() {
         return "/async/bookstore";

http://git-wip-us.apache.org/repos/asf/cxf/blob/6187cb92/systests/jaxrs/src/test/resources/jaxrs_async/WEB-INF/beans.xml
----------------------------------------------------------------------
diff --git a/systests/jaxrs/src/test/resources/jaxrs_async/WEB-INF/beans.xml b/systests/jaxrs/src/test/resources/jaxrs_async/WEB-INF/beans.xml
index 8a37a14..9419849 100644
--- a/systests/jaxrs/src/test/resources/jaxrs_async/WEB-INF/beans.xml
+++ b/systests/jaxrs/src/test/resources/jaxrs_async/WEB-INF/beans.xml
@@ -34,6 +34,7 @@
     <jaxrs:server id="bookservice" address="/async">
         <jaxrs:serviceBeans>
             <ref bean="serviceBean"/>
+            <bean class="org.apache.cxf.systest.jaxrs.AsyncResource"/>
         </jaxrs:serviceBeans>
         <jaxrs:providers>
             <bean class="org.apache.cxf.systest.jaxrs.BookContinuationFilter"/>


Mime
View raw message