Skip to content

Commit

Permalink
[RESTEASY-1812] Tests for reactive client
Browse files Browse the repository at this point in the history
  • Loading branch information
marekkopecky authored and asoldano committed Feb 20, 2018
1 parent 4d1f1c3 commit e3a3d84
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 2 deletions.
Expand Up @@ -2,6 +2,7 @@

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;

import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
Expand Down Expand Up @@ -576,14 +577,14 @@ void doTestRxClientMethodClassEntity(boolean useCustomInvoker) throws Exception
Assert.assertEquals(useCustomInvoker, invoker instanceof TestRxInvoker && TestRxInvoker.used);
client.close();
}

@Test
public void testRxClientMethodGenericTypeEntity() throws Exception
{
doTestRxClientMethodGenericTypeEntity(false);
doTestRxClientMethodGenericTypeEntity(true);
}

void doTestRxClientMethodGenericTypeEntity(boolean useCustomInvoker) throws Exception
{
final Client client = newClient(useCustomInvoker);
Expand All @@ -595,5 +596,31 @@ void doTestRxClientMethodGenericTypeEntity(boolean useCustomInvoker) throws Exce
Assert.assertEquals(useCustomInvoker, invoker instanceof TestRxInvoker && TestRxInvoker.used);
client.close();
}

/**
* @tpTestDetails end-point method returns String data after some delay (3s)
* client use RxInvoker. Data should not be prepared right after CompletionStage object are returned from client
* CompletionStage should return correct data after 3s delay
* @tpSince RESTEasy 3.5
*/
@Test
public void testGetDataWithDelay() throws Exception
{
doTestGetDataWithDelay(false);
doTestGetDataWithDelay(true);
}

void doTestGetDataWithDelay(boolean useCustomInvoker) throws Exception
{
final Client client = newClient(useCustomInvoker);
Builder builder = client.target(generateURL("/sleep")).request();
RxInvoker<?> invoker = useCustomInvoker ? builder.rx(TestRxInvoker.class) : builder.rx();
Future<String> future = ((CompletableFuture<String>) invoker.get(String.class)).toCompletableFuture();
Assert.assertFalse(future.isDone());
String response = future.get();
Assert.assertEquals("get", response);
Assert.assertEquals(useCustomInvoker, invoker instanceof TestRxInvoker && TestRxInvoker.used);
client.close();
}
}

Expand Up @@ -4,6 +4,10 @@
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
Expand All @@ -15,6 +19,7 @@
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

Expand Down Expand Up @@ -42,6 +47,23 @@ public Response get()
return Response.ok("get").build();
}

@GET
@Path("sleep")
@Produces("text/plain")
public CompletionStage<String> sleep() {
CompletableFuture<String> cs = new CompletableFuture<>();
ExecutorService executor = Executors.newSingleThreadExecutor();
executor.submit(() -> {
try {
Thread.sleep(3000L);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
cs.complete("get");
});
return cs;
}

@Path("put")
@PUT
@Consumes(MediaType.TEXT_PLAIN)
Expand Down
Expand Up @@ -122,6 +122,11 @@ public CompletionStage<String> exceptionDelay(@Context HttpRequest req) {
executor.submit(
new Runnable() {
public void run() {
try {
Thread.sleep(3000L); // make sure that response will be created after end-point method ends
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
Response response = Response.status(444).entity(EXCEPTION).build();
cs.completeExceptionally(new WebApplicationException(response));
}
Expand Down

0 comments on commit e3a3d84

Please sign in to comment.