|
| 1 | +/* |
| 2 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
| 3 | + * |
| 4 | + * Copyright (c) 2014 Oracle and/or its affiliates. All rights reserved. |
| 5 | + * |
| 6 | + * The contents of this file are subject to the terms of either the GNU |
| 7 | + * General Public License Version 2 only ("GPL") or the Common Development |
| 8 | + * and Distribution License("CDDL") (collectively, the "License"). You |
| 9 | + * may not use this file except in compliance with the License. You can |
| 10 | + * obtain a copy of the License at |
| 11 | + * http://glassfish.java.net/public/CDDL+GPL_1_1.html |
| 12 | + * or packager/legal/LICENSE.txt. See the License for the specific |
| 13 | + * language governing permissions and limitations under the License. |
| 14 | + * |
| 15 | + * When distributing the software, include this License Header Notice in each |
| 16 | + * file and include the License file at packager/legal/LICENSE.txt. |
| 17 | + * |
| 18 | + * GPL Classpath Exception: |
| 19 | + * Oracle designates this particular file as subject to the "Classpath" |
| 20 | + * exception as provided by Oracle in the GPL Version 2 section of the License |
| 21 | + * file that accompanied this code. |
| 22 | + * |
| 23 | + * Modifications: |
| 24 | + * If applicable, add the following below the License Header, with the fields |
| 25 | + * enclosed by brackets [] replaced by your own identifying information: |
| 26 | + * "Portions Copyright [year] [name of copyright owner]" |
| 27 | + * |
| 28 | + * Contributor(s): |
| 29 | + * If you wish your version of this file to be governed by only the CDDL or |
| 30 | + * only the GPL Version 2, indicate your decision by adding "[Contributor] |
| 31 | + * elects to include this software in this distribution under the [CDDL or GPL |
| 32 | + * Version 2] license." If you don't indicate a single choice of license, a |
| 33 | + * recipient has the option to distribute your version of this file under |
| 34 | + * either the CDDL, the GPL Version 2 or to extend the choice of license to |
| 35 | + * its licensees as provided above. However, if you add GPL Version 2 code |
| 36 | + * and therefore, elected the GPL Version 2 license, then the option applies |
| 37 | + * only if the new code is made subject to such option by the copyright |
| 38 | + * holder. |
| 39 | + */ |
| 40 | + |
| 41 | +package org.glassfish.jersey.tests.e2e.client; |
| 42 | + |
| 43 | +import org.glassfish.jersey.server.ResourceConfig; |
| 44 | +import org.glassfish.jersey.test.JerseyTest; |
| 45 | +import org.junit.Test; |
| 46 | + |
| 47 | +import javax.ws.rs.GET; |
| 48 | +import javax.ws.rs.Path; |
| 49 | +import javax.ws.rs.client.InvocationCallback; |
| 50 | +import javax.ws.rs.core.Application; |
| 51 | +import javax.ws.rs.core.Response; |
| 52 | +import java.util.concurrent.CancellationException; |
| 53 | +import java.util.concurrent.CountDownLatch; |
| 54 | +import java.util.concurrent.Future; |
| 55 | +import java.util.concurrent.TimeUnit; |
| 56 | +import java.util.concurrent.TimeoutException; |
| 57 | +import java.util.concurrent.locks.Condition; |
| 58 | +import java.util.concurrent.locks.ReentrantLock; |
| 59 | + |
| 60 | +import static org.junit.Assert.assertEquals; |
| 61 | +import static org.junit.Assert.assertTrue; |
| 62 | +import static org.junit.Assert.fail; |
| 63 | + |
| 64 | +/** |
| 65 | + * Tests the behaviour of the Async client when the {@link java.util.concurrent.Future} is cancelled. |
| 66 | + * |
| 67 | + * <p> |
| 68 | + * Tests, that if the async request future is cancelled by the client, |
| 69 | + * the {@link javax.ws.rs.client.InvocationCallback#completed(Object)} callback is not invoked and that |
| 70 | + * {@link java.util.concurrent.CancellationException} is correctly returned (according to spec.) to |
| 71 | + * {@link javax.ws.rs.client.InvocationCallback#failed(Throwable)} callback method. |
| 72 | + * </p> |
| 73 | + * |
| 74 | + * @author Adam Lindenthal (adam.lindenthal at oracle.com) |
| 75 | + */ |
| 76 | +public class CancelFutureClientTest extends JerseyTest { |
| 77 | + |
| 78 | + public static final long MAX_WAITING_SECONDS = 2L; |
| 79 | + final CountDownLatch countDownLatch = new CountDownLatch(1); |
| 80 | + |
| 81 | + @Override |
| 82 | + protected Application configure() { |
| 83 | + return new ResourceConfig(TestResource.class); |
| 84 | + } |
| 85 | + |
| 86 | + @Test |
| 87 | + public void testCancelFuture() throws InterruptedException, TimeoutException { |
| 88 | + Future<Response> future = target().path("test").request().async().get( |
| 89 | + new InvocationCallback<Response>() { |
| 90 | + public void completed(final Response response) { |
| 91 | + fail("[completed()] callback was invoked, although the Future should have been cancelled."); |
| 92 | + } |
| 93 | + |
| 94 | + public void failed(final Throwable throwable) { |
| 95 | + assertEquals(CancellationException.class, throwable.getClass()); |
| 96 | + countDownLatch.countDown(); |
| 97 | + } |
| 98 | + } |
| 99 | + ); |
| 100 | + if (!future.cancel(true)) { |
| 101 | + fail("The Future could not be canceled."); |
| 102 | + } |
| 103 | + |
| 104 | + // prevent the test container to stop the method execution before the callbacks can be reached. |
| 105 | + if (!countDownLatch.await(MAX_WAITING_SECONDS, TimeUnit.SECONDS)) { |
| 106 | + throw new TimeoutException("Callback was not triggered within the time limit." + countDownLatch.getCount()); |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + @Path("test") |
| 111 | + public static class TestResource { |
| 112 | + @GET |
| 113 | + public Response get() { |
| 114 | + try { |
| 115 | + Thread.sleep(1000); |
| 116 | + } catch (InterruptedException e) { |
| 117 | + e.printStackTrace(); |
| 118 | + } |
| 119 | + return Response.noContent().build(); |
| 120 | + } |
| 121 | + } |
| 122 | +} |
0 commit comments