users@jersey.java.net

[Jersey] Issues with JerseyTest and multiple requests to the asynchronous resource method

From: Oleksandr Bodriagov (Polystar) <"Oleksandr>
Date: Wed, 13 Aug 2014 13:40:27 +0000

Hello

I am developing a REST api using the following components: Jersey 2.11 and Jetty 9.2.1

Here is an asynchronous resource method that simulates the case of answering with some HTTP status code instead of running some expensive operation in a new Thread:

import javax.ws.rs.GET;

import javax.ws.rs.Path;

import javax.ws.rs.PathParam;

import javax.ws.rs.Produces;

import javax.ws.rs.container.AsyncResponse;

import javax.ws.rs.container.Suspended;

import javax.ws.rs.core.MediaType;

import javax.ws.rs.core.Response;


@Path("/testquery")

public class TestResource {


    @GET

    @Path("/{id}")

    @Produces(MediaType.APPLICATION_JSON)

    public void asyncGetData(@Suspended final AsyncResponse asyncResponse,

            @PathParam("id") final String requestId) {


        // does nothing except answering GONE

        asyncResponse.resume(Response.status(Response.Status.GONE).build());

    }

}

Here is the test case that sends two similar requests to this asynchronous resource method:

import static org.junit.Assert.assertEquals;


import java.io.IOException;

import java.util.UUID;


import javax.ws.rs.core.Application;

import javax.ws.rs.core.Response;

import javax.ws.rs.core.Response.Status;


import org.glassfish.jersey.message.MessageProperties;

import org.glassfish.jersey.server.ResourceConfig;

import org.glassfish.jersey.test.JerseyTest;

import org.junit.Test;


public class TestResourceTest extends JerseyTest{



    @Override

    protected Application configure() {

        ResourceConfig config = new ResourceConfig(TestResource.class);

        config.property(MessageProperties.XML_SECURITY_DISABLE, Boolean.TRUE);

        return config;

    }


    @Test

    public void simpleTest() {

        String queryId = UUID.randomUUID().toString();



        System.out.println("Sending the first request");

        Response resp1 = target("testquery/" + queryId).request().get();

        assertEquals(Status.GONE.getStatusCode(), resp1.getStatus());



        System.out.println("Sending the second request");

        Response resp2 = target("testquery/" + queryId).request().get();

        assertEquals(Status.GONE.getStatusCode(), resp2.getStatus());


        System.out.println("Finished");

    }

}

It turns out that this test gets in an infinite loop after executing the second request. The method asyncGetData is run over and over again. The console output is the following:

Aug 13, 2014 3:36:17 PM org.glassfish.jersey.test.jetty.JettyTestContainerFactory$JettyTestContainer <init>

INFO: Creating JettyTestContainer configured at the base URI http://localhost:9998/

2014-08-13 15:36:17.294:INFO::main: Logging initialized @505ms

2014-08-13 15:36:17.798:INFO:oejs.Server:main: jetty-9.2.1.v20140609

2014-08-13 15:36:17.821:INFO:oejs.ServerConnector:main: Started ServerConnector_at_11b93a10{HTTP/1.1}{0.0.0.0:9998}

2014-08-13 15:36:17.822:INFO:oejs.Server:main: Started @1038ms

Test: Sending the first request

Aug 13, 2014 3:36:18 PM org.hibernate.validator.internal.util.Version <clinit>

INFO: HV000001: Hibernate Validator 5.0.0.Final

Server: processing the request

Server: processing the request

Server: processing the request

Test: Sending the second request

Server: processing the request

Server: processing the request

Server: processing the request

Server: processing the request

...


Does anyone have any idea what I can do to make this test work? I need to send two exactly same requests in one test.
Thanks in advance!


Regards,

Alex