diff options
Diffstat (limited to 'configserver-client/src/test/java/ai/vespa/hosted/client/HttpConfigServerClientTest.java')
-rw-r--r-- | configserver-client/src/test/java/ai/vespa/hosted/client/HttpConfigServerClientTest.java | 98 |
1 files changed, 98 insertions, 0 deletions
diff --git a/configserver-client/src/test/java/ai/vespa/hosted/client/HttpConfigServerClientTest.java b/configserver-client/src/test/java/ai/vespa/hosted/client/HttpConfigServerClientTest.java new file mode 100644 index 00000000000..cbf38f46f6f --- /dev/null +++ b/configserver-client/src/test/java/ai/vespa/hosted/client/HttpConfigServerClientTest.java @@ -0,0 +1,98 @@ +// Copyright Verizon Media. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +package ai.vespa.hosted.client; + +import ai.vespa.hosted.client.ConfigServerClient.ConfigServerException; +import ai.vespa.hosted.client.ConfigServerClient.HostStrategy; +import com.github.tomakehurst.wiremock.http.Fault; +import com.yahoo.vespa.athenz.api.AthenzService; +import org.apache.hc.core5.http.Method; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.RegisterExtension; + +import java.io.UncheckedIOException; +import java.net.URI; +import java.util.List; + +import static com.github.tomakehurst.wiremock.client.WireMock.aResponse; +import static com.github.tomakehurst.wiremock.client.WireMock.anyRequestedFor; +import static com.github.tomakehurst.wiremock.client.WireMock.anyUrl; +import static com.github.tomakehurst.wiremock.client.WireMock.equalTo; +import static com.github.tomakehurst.wiremock.client.WireMock.get; +import static com.github.tomakehurst.wiremock.client.WireMock.getRequestedFor; +import static com.github.tomakehurst.wiremock.client.WireMock.okJson; +import static com.github.tomakehurst.wiremock.client.WireMock.post; +import static com.github.tomakehurst.wiremock.client.WireMock.postRequestedFor; +import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo; +import static java.nio.charset.StandardCharsets.UTF_8; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +/** + * @author jonmv + */ +class HttpConfigServerClientTest { + + @RegisterExtension + final WireMockExtension server = new WireMockExtension(); + + final HttpConfigServerClient client = new HttpConfigServerClient(List.of(new AthenzService("mydomain", "yourservice")), "user"); + + @Test + void testRetries() { + // Two servers in list--two attempts on IOException. + server.stubFor(get("/root?query=foo")) + .setResponse(okJson("{}").withFault(Fault.RANDOM_DATA_THEN_CLOSE) + .build()); + assertThrows(UncheckedIOException.class, + () -> client.send(HostStrategy.ordered(List.of(URI.create("http://localhost:" + server.port()), + URI.create("http://localhost:" + server.port() + "/"))), + Method.GET) + .at("root") + .parameters("query", "foo") + .discard()); + server.verify(2, getRequestedFor(urlEqualTo("/root?query=foo"))); + server.verify(2, anyRequestedFor(anyUrl())); + server.resetRequests(); + + // Two attempts on a different IOException. + server.stubFor(post("/prefix/%2Froot")) + .setResponse(okJson("{}").withFault(Fault.EMPTY_RESPONSE) + .build()); + assertThrows(UncheckedIOException.class, + () -> client.send(HostStrategy.shuffling(List.of(URI.create("http://localhost:" + server.port() + "/prefix"), + URI.create("http://localhost:" + server.port() + "/prefix/"))), + Method.POST) + .body("hello".getBytes(UTF_8)) + .at("/root") + .stream()); + server.verify(2, postRequestedFor(urlEqualTo("/prefix/%2Froot")).withRequestBody(equalTo("hello"))); + server.verify(2, anyRequestedFor(anyUrl())); + server.resetRequests(); + + // Successful attempt returns. + server.stubFor(get("/root/boot")) + .setResponse(okJson("{}").build()); + assertEquals("{}", + client.send(HostStrategy.repeating(URI.create("http://localhost:" + server.port()), 10), + Method.GET) + .at("root", "boot") + .read(String::new)); + server.verify(1, getRequestedFor(urlEqualTo("/root/boot"))); + server.verify(1, anyRequestedFor(anyUrl())); + server.resetRequests(); + + // ConfigServerException is not retried. + server.stubFor(get("/")) + .setResponse(aResponse().withStatus(409).withBody("{\"error-code\":\"ACTIVATION_CONFLICT\",\"message\":\"hi\"}").build()); + ConfigServerException thrown = assertThrows(ConfigServerException.class, + () -> client.send(HostStrategy.repeating(URI.create("http://localhost:" + server.port()), 10), + Method.GET) + .read(String::new)); + assertEquals(ConfigServerException.ErrorCode.ACTIVATION_CONFLICT, thrown.errorId()); + assertEquals("hi", thrown.message()); + server.verify(1, getRequestedFor(urlEqualTo("/"))); + server.verify(1, anyRequestedFor(anyUrl())); + + } + +} |