diff options
author | jonmv <venstad@gmail.com> | 2022-04-28 10:12:06 +0200 |
---|---|---|
committer | jonmv <venstad@gmail.com> | 2022-04-28 10:12:06 +0200 |
commit | 1a4c0d52ab509782dacf128624665789c9b0f59b (patch) | |
tree | 6de8bb1c6bfa1e4f8f8792146a03d93881933b37 /http-client/src/test | |
parent | cd04e0c3a06499971c82678e88510b257d4d6faa (diff) |
Split out the general part of the configserver-client
Diffstat (limited to 'http-client/src/test')
-rw-r--r-- | http-client/src/test/java/ai/vespa/hosted/client/HttpConfigServerClientTest.java | 111 | ||||
-rw-r--r-- | http-client/src/test/java/ai/vespa/hosted/client/WireMockExtension.java | 42 |
2 files changed, 153 insertions, 0 deletions
diff --git a/http-client/src/test/java/ai/vespa/hosted/client/HttpConfigServerClientTest.java b/http-client/src/test/java/ai/vespa/hosted/client/HttpConfigServerClientTest.java new file mode 100644 index 00000000000..1bb352568bc --- /dev/null +++ b/http-client/src/test/java/ai/vespa/hosted/client/HttpConfigServerClientTest.java @@ -0,0 +1,111 @@ +// Copyright Yahoo. 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.HostStrategy; +import ai.vespa.hosted.client.ConfigServerClient.ResponseException; +import com.github.tomakehurst.wiremock.http.Fault; +import org.apache.hc.client5.http.impl.classic.HttpClients; +import org.apache.hc.core5.http.Method; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.RegisterExtension; + +import java.io.IOException; +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(); + + ConfigServerClient client; + + @BeforeEach + void setup() { + client = AbstractConfigServerClient.wrapping(HttpClients.createMinimal()); + } + + @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/root")) + .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/root")).withRequestBody(equalTo("hello"))); + server.verify(2, anyRequestedFor(anyUrl())); + server.resetRequests(); + + // Successful attempt returns. + server.stubFor(get("/root/boot/toot")) + .setResponse(okJson("{}").build()); + assertEquals("{}", + client.send(HostStrategy.repeating(URI.create("http://localhost:" + server.port()), 10), + Method.GET) + .at("root", "boot") + .at("toot") + .read(String::new)); + server.verify(1, getRequestedFor(urlEqualTo("/root/boot/toot"))); + server.verify(1, anyRequestedFor(anyUrl())); + server.resetRequests(); + + // ResponseException is not retried. + server.stubFor(get("/")) + .setResponse(aResponse().withStatus(409).withBody("hi").build()); + ResponseException thrown = assertThrows(ResponseException.class, + () -> client.send(HostStrategy.repeating(URI.create("http://localhost:" + server.port()), 10), + Method.GET) + .read(String::new)); + assertEquals("GET http://localhost:" + server.port() + "/ failed with status 409 and body 'hi'", thrown.getMessage()); + server.verify(1, getRequestedFor(urlEqualTo("/"))); + server.verify(1, anyRequestedFor(anyUrl())); + server.resetRequests(); + } + + @AfterEach + void teardown() throws IOException { + client.close(); + } + +} diff --git a/http-client/src/test/java/ai/vespa/hosted/client/WireMockExtension.java b/http-client/src/test/java/ai/vespa/hosted/client/WireMockExtension.java new file mode 100644 index 00000000000..d95650727c0 --- /dev/null +++ b/http-client/src/test/java/ai/vespa/hosted/client/WireMockExtension.java @@ -0,0 +1,42 @@ +// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +package ai.vespa.hosted.client; + +import com.github.tomakehurst.wiremock.WireMockServer; +import com.github.tomakehurst.wiremock.core.Options; +import com.github.tomakehurst.wiremock.core.WireMockConfiguration; +import org.junit.jupiter.api.extension.AfterEachCallback; +import org.junit.jupiter.api.extension.BeforeEachCallback; +import org.junit.jupiter.api.extension.ExtensionContext; + +/** + * Allows wiremock to be used as a JUnit 5 extension, like + * <pre> + * + * @RegisterExtension + * WireMockExtension mockServer1 = new WireMockExtension(); + * </pre> + */ +public class WireMockExtension extends WireMockServer implements BeforeEachCallback, AfterEachCallback { + + public WireMockExtension() { + this(WireMockConfiguration.options() + .dynamicPort() + .dynamicHttpsPort()); + } + + public WireMockExtension(Options options) { + super(options); + } + + @Override + public void beforeEach(ExtensionContext extensionContext) { + start(); + } + + @Override + public void afterEach(ExtensionContext extensionContext) { + stop(); + resetAll(); + } + +} |