Search in sources :

Example 1 with ResponseWithDelay

use of com.spotify.apollo.test.response.ResponseWithDelay in project apollo by spotify.

the class StubClient method send.

@Override
public CompletionStage<Response<ByteString>> send(Request request) {
    final ResponseSource responseSource = responseSource(request);
    if (responseSource == null) {
        final NoMatchingResponseFoundException notFound = new NoMatchingResponseFoundException(formatRequestNotMatchedExceptionMessage(request));
        final CompletableFuture<Response<ByteString>> notFoundFuture = new CompletableFuture<>();
        notFoundFuture.completeExceptionally(notFound);
        return notFoundFuture;
    }
    // Create response task
    final ResponseWithDelay responseWithDelay = responseSource.create(request);
    // Schedule a response in the future
    final CompletableFuture<Response<ByteString>> future = new CompletableFuture<>();
    final Runnable replyTask = () -> {
        Response<ByteString> response = responseWithDelay.getResponse();
        requestsAndResponses.add(RequestResponsePair.create(request, response));
        future.complete(response);
    };
    if (responseWithDelay.getDelayMillis() <= 0) {
        replyTask.run();
    } else {
        executor.schedule(replyTask, responseWithDelay.getDelayMillis(), TimeUnit.MILLISECONDS);
    }
    return future;
}
Also used : Response(com.spotify.apollo.Response) CompletableFuture(java.util.concurrent.CompletableFuture) ResponseWithDelay(com.spotify.apollo.test.response.ResponseWithDelay) ResponseSource(com.spotify.apollo.test.response.ResponseSource)

Example 2 with ResponseWithDelay

use of com.spotify.apollo.test.response.ResponseWithDelay in project apollo by spotify.

the class StubClientTest method shouldDisallowInfiniteIterativeReplies.

@Test
public void shouldDisallowInfiniteIterativeReplies() throws Exception {
    ResponseWithDelay response1 = ResponseWithDelay.forResponse(Response.forPayload(encodeUtf8("first response")));
    ResponseWithDelay response2 = ResponseWithDelay.forResponse(Response.forPayload(encodeUtf8("second response")));
    List<ResponseWithDelay> responses = Arrays.asList(response1, response2);
    ResponseSource constantResponses = Responses.sequence(responses);
    stubClient.respond(constantResponses).to("http://ping");
    assertThat(getStringFromPing().toCompletableFuture().get(), is("first response"));
    assertThat(getStringFromPing().toCompletableFuture().get(), is("second response"));
    try {
        getStringFromPing().toCompletableFuture().get();
        fail();
    } catch (IllegalStateException e) {
        assertThat(e.getMessage(), is("No more responses specified!"));
    }
}
Also used : ResponseWithDelay(com.spotify.apollo.test.response.ResponseWithDelay) ResponseSource(com.spotify.apollo.test.response.ResponseSource) Test(org.junit.Test)

Example 3 with ResponseWithDelay

use of com.spotify.apollo.test.response.ResponseWithDelay in project apollo by spotify.

the class StubClientTest method shouldReturnStatusCodeIntegers.

@Test
public void shouldReturnStatusCodeIntegers() throws Exception {
    ResponseWithDelay response = ResponseWithDelay.forResponse(Response.of(Status.createForCode(666), encodeUtf8("constant response")));
    ResponseSource responses = Responses.constant(response);
    stubClient.respond(responses).to("http://ping");
    Response<ByteString> reply = getResponseFromPing().toCompletableFuture().get();
    assertThat(reply.status(), withCode(666));
}
Also used : ResponseWithDelay(com.spotify.apollo.test.response.ResponseWithDelay) ResponseSource(com.spotify.apollo.test.response.ResponseSource) ByteString(okio.ByteString) Test(org.junit.Test)

Example 4 with ResponseWithDelay

use of com.spotify.apollo.test.response.ResponseWithDelay in project apollo by spotify.

the class StubClientTest method shouldReturnIterativeRepliesWithVaryingStatusCodes.

@Test
public void shouldReturnIterativeRepliesWithVaryingStatusCodes() throws Exception {
    ResponseWithDelay response1 = ResponseWithDelay.forResponse(Response.of(Status.ACCEPTED, encodeUtf8("first response")));
    ResponseWithDelay response2 = ResponseWithDelay.forResponse(Response.of(Status.FORBIDDEN, encodeUtf8("second response")));
    List<ResponseWithDelay> responses = Arrays.asList(response1, response2);
    ResponseSource responseSequence = Responses.sequence(responses);
    stubClient.respond(responseSequence).to("http://ping");
    CompletionStage<Response<ByteString>> reply1 = getResponseFromPing();
    CompletionStage<Response<ByteString>> reply2 = getResponseFromPing();
    Response<ByteString> message1 = reply1.toCompletableFuture().get();
    Response<ByteString> message2 = reply2.toCompletableFuture().get();
    assertThat(message1.status(), is(Status.ACCEPTED));
    assertThat(message2.status(), is(Status.FORBIDDEN));
    assertThat(message1.payload().get().utf8(), is("first response"));
    assertThat(message2.payload().get().utf8(), is("second response"));
}
Also used : Response(com.spotify.apollo.Response) ResponseWithDelay(com.spotify.apollo.test.response.ResponseWithDelay) ResponseSource(com.spotify.apollo.test.response.ResponseSource) ByteString(okio.ByteString) Test(org.junit.Test)

Example 5 with ResponseWithDelay

use of com.spotify.apollo.test.response.ResponseWithDelay in project apollo by spotify.

the class StubClientTest method shouldReturnConstantReplies.

@Test
public void shouldReturnConstantReplies() throws Exception {
    ResponseWithDelay response = ResponseWithDelay.forResponse(Response.forPayload(encodeUtf8("constant response")));
    ResponseSource responses = Responses.constant(response);
    stubClient.respond(responses).to("http://ping");
    assertThat(getStringFromPing().toCompletableFuture().get(), is("constant response"));
    assertThat(getStringFromPing().toCompletableFuture().get(), is("constant response"));
}
Also used : ResponseWithDelay(com.spotify.apollo.test.response.ResponseWithDelay) ResponseSource(com.spotify.apollo.test.response.ResponseSource) Test(org.junit.Test)

Aggregations

ResponseSource (com.spotify.apollo.test.response.ResponseSource)6 ResponseWithDelay (com.spotify.apollo.test.response.ResponseWithDelay)6 Test (org.junit.Test)5 Response (com.spotify.apollo.Response)2 ByteString (okio.ByteString)2 CompletableFuture (java.util.concurrent.CompletableFuture)1