Search in sources :

Example 1 with InvocationCallback

use of jakarta.ws.rs.client.InvocationCallback in project jaxrs-api by eclipse-ee4j.

the class JaxrsWebTestCase method invoke.

/**
 * Invoke the invocation synchronously, or asynchronously
 */
protected Response invoke(Invocation invocation) throws TestFailureException {
    Response response = null;
    switch(executionType) {
        case SYNCHRONOUS:
            response = invocation.invoke();
            break;
        case ASYNCHRONOUS:
            int cnt = 0;
            try {
                final boolean[] buffered = { false };
                InvocationCallback<Response> callback = new InvocationCallback<Response>() {

                    @Override
                    public void completed(Response res) {
                        try {
                            JaxrsWebTestCase.this.response = res;
                            // buffer before stream is closed
                            getResponse().getResponseBodyAsString();
                            buffered[0] = true;
                        } catch (IOException e) {
                            throw new RuntimeException(e);
                        }
                    }

                    @Override
                    public void failed(Throwable throwable) {
                        throw new RuntimeException(throwable);
                    }
                };
                Future<Response> future = invocation.submit(callback);
                while (!buffered[0] && cnt++ < 50) {
                    Thread.sleep(100L);
                }
                response = future.get();
            // response = invocation.submit().get();
            } catch (Exception e) {
                throw new TestFailureException(e);
            }
            if (cnt > 49) {
                throw new TestFailureException("Invocation callback has not been called within 5 second");
            }
    }
    return response;
}
Also used : HttpResponse(ee.jakarta.tck.ws.rs.common.webclient.http.HttpResponse) Response(jakarta.ws.rs.core.Response) InvocationCallback(jakarta.ws.rs.client.InvocationCallback) IOException(java.io.IOException) TestFailureException(ee.jakarta.tck.ws.rs.common.webclient.TestFailureException) IOException(java.io.IOException) TestFailureException(ee.jakarta.tck.ws.rs.common.webclient.TestFailureException)

Aggregations

TestFailureException (ee.jakarta.tck.ws.rs.common.webclient.TestFailureException)1 HttpResponse (ee.jakarta.tck.ws.rs.common.webclient.http.HttpResponse)1 InvocationCallback (jakarta.ws.rs.client.InvocationCallback)1 Response (jakarta.ws.rs.core.Response)1 IOException (java.io.IOException)1