Search in sources :

Example 1 with Target

use of feign.Target in project feign-reactive by kptfh.

the class HystrixReactiveHttpClientTest method getSetterFactory.

static CloudReactiveFeign.SetterFactory getSetterFactory(int testNo) {
    return new CloudReactiveFeign.SetterFactory() {

        @Override
        public HystrixObservableCommand.Setter create(Target<?> target, MethodMetadata methodMetadata) {
            String groupKey = target.name();
            String commandKey = methodMetadata.configKey();
            return HystrixObservableCommand.Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey(groupKey)).andCommandKey(HystrixCommandKey.Factory.asKey(commandKey + testNo)).andCommandPropertiesDefaults(HystrixCommandProperties.Setter().withCircuitBreakerRequestVolumeThreshold(1));
        }
    };
}
Also used : Target(feign.Target) HystrixObservableCommand(com.netflix.hystrix.HystrixObservableCommand) MethodMetadata(feign.MethodMetadata) Matchers.containsString(org.hamcrest.Matchers.containsString)

Example 2 with Target

use of feign.Target in project feign by OpenFeign.

the class Http2ClientAsyncTest method decodingExceptionGetWrappedInDecode404Mode.

@Test
public void decodingExceptionGetWrappedInDecode404Mode() throws Throwable {
    server.enqueue(new MockResponse().setResponseCode(404));
    thrown.expect(DecodeException.class);
    thrown.expectCause(isA(NoSuchElementException.class));
    final TestInterfaceAsync api = newAsyncBuilder().decode404().decoder((response, type) -> {
        assertEquals(404, response.status());
        throw new NoSuchElementException();
    }).target("http://localhost:" + server.getPort());
    unwrap(api.post());
}
Also used : ErrorDecoder(feign.codec.ErrorDecoder) Arrays(java.util.Arrays) Body(feign.Body) TypeToken(com.google.gson.reflect.TypeToken) Date(java.util.Date) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Decoder(feign.codec.Decoder) RequestInterceptor(feign.RequestInterceptor) FieldQueryMapEncoder(feign.querymap.FieldQueryMapEncoder) HttpMethod(feign.Request.HttpMethod) Gson(com.google.gson.Gson) AsyncFeign(feign.AsyncFeign) Map(java.util.Map) MockWebServer(okhttp3.mockwebserver.MockWebServer) Assertions(org.assertj.core.api.Assertions) Assert.fail(org.junit.Assert.fail) URI(java.net.URI) AsyncClient(feign.AsyncClient) FeignException(feign.FeignException) BeanQueryMapEncoder(feign.querymap.BeanQueryMapEncoder) Collection(java.util.Collection) RequestLine(feign.RequestLine) PropertyPojo(feign.PropertyPojo) Feign(feign.Feign) StandardCharsets(java.nio.charset.StandardCharsets) Executors(java.util.concurrent.Executors) Encoder(feign.codec.Encoder) List(java.util.List) Type(java.lang.reflect.Type) MockWebServerAssertions.assertThat(feign.assertj.MockWebServerAssertions.assertThat) Http2Client(feign.http2client.Http2Client) ChildPojo(feign.ChildPojo) MockResponse(okhttp3.mockwebserver.MockResponse) MapEntry.entry(org.assertj.core.data.MapEntry.entry) RequestTemplate(feign.RequestTemplate) CoreMatchers.isA(org.hamcrest.CoreMatchers.isA) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) Headers(feign.Headers) AtomicReference(java.util.concurrent.atomic.AtomicReference) ResponseMappingDecoder(feign.Feign.ResponseMappingDecoder) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) StringDecoder(feign.codec.StringDecoder) Util(feign.Util) NoSuchElementException(java.util.NoSuchElementException) ExpectedException(org.junit.rules.ExpectedException) ExecutorService(java.util.concurrent.ExecutorService) QueryMapEncoder(feign.QueryMapEncoder) ResponseMapper(feign.ResponseMapper) Response(feign.Response) Buffer(okio.Buffer) Target(feign.Target) HeaderMap(feign.HeaderMap) Assert.assertTrue(org.junit.Assert.assertTrue) IOException(java.io.IOException) Test(org.junit.Test) QueryMap(feign.QueryMap) HardCodedTarget(feign.Target.HardCodedTarget) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) Rule(org.junit.Rule) Param(feign.Param) DecodeException(feign.codec.DecodeException) Request(feign.Request) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) EncodeException(feign.codec.EncodeException) MockResponse(okhttp3.mockwebserver.MockResponse) NoSuchElementException(java.util.NoSuchElementException) Test(org.junit.Test)

Example 3 with Target

use of feign.Target in project feign by OpenFeign.

the class Http2ClientAsyncTest method throwsFeignExceptionWithoutBody.

@Test
public void throwsFeignExceptionWithoutBody() {
    server.enqueue(new MockResponse().setBody("success!"));
    final TestInterfaceAsync api = newAsyncBuilder().decoder((response, type) -> {
        throw new IOException("timeout");
    }).target("http://localhost:" + server.getPort());
    try {
        api.noContent();
    } catch (final FeignException e) {
        Assertions.assertThat(e.getMessage()).isEqualTo("timeout reading POST http://localhost:" + server.getPort() + "/");
        Assertions.assertThat(e.contentUTF8()).isEqualTo("");
    }
}
Also used : ErrorDecoder(feign.codec.ErrorDecoder) Arrays(java.util.Arrays) Body(feign.Body) TypeToken(com.google.gson.reflect.TypeToken) Date(java.util.Date) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Decoder(feign.codec.Decoder) RequestInterceptor(feign.RequestInterceptor) FieldQueryMapEncoder(feign.querymap.FieldQueryMapEncoder) HttpMethod(feign.Request.HttpMethod) Gson(com.google.gson.Gson) AsyncFeign(feign.AsyncFeign) Map(java.util.Map) MockWebServer(okhttp3.mockwebserver.MockWebServer) Assertions(org.assertj.core.api.Assertions) Assert.fail(org.junit.Assert.fail) URI(java.net.URI) AsyncClient(feign.AsyncClient) FeignException(feign.FeignException) BeanQueryMapEncoder(feign.querymap.BeanQueryMapEncoder) Collection(java.util.Collection) RequestLine(feign.RequestLine) PropertyPojo(feign.PropertyPojo) Feign(feign.Feign) StandardCharsets(java.nio.charset.StandardCharsets) Executors(java.util.concurrent.Executors) Encoder(feign.codec.Encoder) List(java.util.List) Type(java.lang.reflect.Type) MockWebServerAssertions.assertThat(feign.assertj.MockWebServerAssertions.assertThat) Http2Client(feign.http2client.Http2Client) ChildPojo(feign.ChildPojo) MockResponse(okhttp3.mockwebserver.MockResponse) MapEntry.entry(org.assertj.core.data.MapEntry.entry) RequestTemplate(feign.RequestTemplate) CoreMatchers.isA(org.hamcrest.CoreMatchers.isA) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) Headers(feign.Headers) AtomicReference(java.util.concurrent.atomic.AtomicReference) ResponseMappingDecoder(feign.Feign.ResponseMappingDecoder) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) StringDecoder(feign.codec.StringDecoder) Util(feign.Util) NoSuchElementException(java.util.NoSuchElementException) ExpectedException(org.junit.rules.ExpectedException) ExecutorService(java.util.concurrent.ExecutorService) QueryMapEncoder(feign.QueryMapEncoder) ResponseMapper(feign.ResponseMapper) Response(feign.Response) Buffer(okio.Buffer) Target(feign.Target) HeaderMap(feign.HeaderMap) Assert.assertTrue(org.junit.Assert.assertTrue) IOException(java.io.IOException) Test(org.junit.Test) QueryMap(feign.QueryMap) HardCodedTarget(feign.Target.HardCodedTarget) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) Rule(org.junit.Rule) Param(feign.Param) DecodeException(feign.codec.DecodeException) Request(feign.Request) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) EncodeException(feign.codec.EncodeException) MockResponse(okhttp3.mockwebserver.MockResponse) FeignException(feign.FeignException) IOException(java.io.IOException) Test(org.junit.Test)

Example 4 with Target

use of feign.Target in project feign-reactive by kptfh.

the class HystrixReactiveHttpClientTest method getSetterFactory.

CloudReactiveFeign.SetterFactory getSetterFactory(int testNo) {
    return new CloudReactiveFeign.SetterFactory() {

        @Override
        public HystrixObservableCommand.Setter create(Target<?> target, MethodMetadata methodMetadata) {
            String groupKey = target.name();
            HystrixCommandKey commandKey = HystrixCommandKey.Factory.asKey(methodMetadata.configKey() + testNo);
            lastCommandKey.set(commandKey);
            return HystrixObservableCommand.Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey(groupKey)).andCommandKey(commandKey).andCommandPropertiesDefaults(HystrixCommandProperties.Setter().withCircuitBreakerRequestVolumeThreshold(VOLUME_THRESHOLD).withExecutionTimeoutEnabled(false).withCircuitBreakerSleepWindowInMilliseconds(SLEEP_WINDOW).withMetricsHealthSnapshotIntervalInMilliseconds(10));
        }
    };
}
Also used : Target(feign.Target) MethodMetadata(feign.MethodMetadata) Matchers.containsString(org.hamcrest.Matchers.containsString)

Example 5 with Target

use of feign.Target in project feign-reactive by kptfh.

the class HystrixReactiveHttpClientTest method shouldOpenCircuitBreaker.

@Test
public void shouldOpenCircuitBreaker() throws IOException, InterruptedException {
    String body = "success!";
    int callsNo = 10;
    LoadBalancingReactiveHttpClientTest.mockSuccessAfterSeveralAttempts(server, "/", callsNo, SC_SERVICE_UNAVAILABLE, aResponse().withStatus(200).withHeader("Content-Type", "application/json").withBody(body));
    LoadBalancingReactiveHttpClientTest.TestInterface client = CloudReactiveFeign.<LoadBalancingReactiveHttpClientTest.TestInterface>builder().webClient(WebClient.create()).setHystrixCommandSetterFactory(getSetterFactory(testNo)).target(LoadBalancingReactiveHttpClientTest.TestInterface.class, "http://localhost:" + server.port());
    List<Object> results = IntStream.range(0, callsNo).mapToObj(i -> {
        try {
            return client.get().block();
        } catch (Throwable t) {
            return t;
        }
    }).collect(Collectors.toList());
    assertThat(server.getAllServeEvents().size()).isLessThan(callsNo);
    Throwable firstError = (Throwable) results.get(0);
    assertThat(firstError).isInstanceOf(HystrixRuntimeException.class);
    assertThat(firstError.getMessage()).contains("failed and no fallback available");
    Throwable lastError = (Throwable) results.get(results.size() - 1);
    assertThat(lastError).isInstanceOf(HystrixRuntimeException.class);
    assertThat(lastError.getMessage()).contains("short-circuited and no fallback available.");
}
Also used : IntStream(java.util.stream.IntStream) HystrixCommandGroupKey(com.netflix.hystrix.HystrixCommandGroupKey) Target(feign.Target) HystrixCommandKey(com.netflix.hystrix.HystrixCommandKey) HystrixCommandProperties(com.netflix.hystrix.HystrixCommandProperties) WireMock.aResponse(com.github.tomakehurst.wiremock.client.WireMock.aResponse) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) WebClient(org.springframework.web.reactive.function.client.WebClient) Test(org.junit.Test) Mono(reactor.core.publisher.Mono) IOException(java.io.IOException) HystrixRuntimeException(com.netflix.hystrix.exception.HystrixRuntimeException) MethodMetadata(feign.MethodMetadata) Collectors(java.util.stream.Collectors) List(java.util.List) Rule(org.junit.Rule) WireMockConfiguration.wireMockConfig(com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig) WireMockClassRule(com.github.tomakehurst.wiremock.junit.WireMockClassRule) HystrixObservableCommand(com.netflix.hystrix.HystrixObservableCommand) SC_SERVICE_UNAVAILABLE(org.apache.http.HttpStatus.SC_SERVICE_UNAVAILABLE) ClassRule(org.junit.ClassRule) ExpectedException(org.junit.rules.ExpectedException) Matchers.containsString(org.hamcrest.Matchers.containsString) Before(org.junit.Before) Matchers.containsString(org.hamcrest.Matchers.containsString) Test(org.junit.Test)

Aggregations

Target (feign.Target)7 IOException (java.io.IOException)5 Gson (com.google.gson.Gson)4 TypeToken (com.google.gson.reflect.TypeToken)4 AsyncClient (feign.AsyncClient)4 AsyncFeign (feign.AsyncFeign)4 Body (feign.Body)4 ChildPojo (feign.ChildPojo)4 Feign (feign.Feign)4 ResponseMappingDecoder (feign.Feign.ResponseMappingDecoder)4 FeignException (feign.FeignException)4 HeaderMap (feign.HeaderMap)4 Headers (feign.Headers)4 Param (feign.Param)4 PropertyPojo (feign.PropertyPojo)4 QueryMap (feign.QueryMap)4 QueryMapEncoder (feign.QueryMapEncoder)4 Request (feign.Request)4 HttpMethod (feign.Request.HttpMethod)4 RequestInterceptor (feign.RequestInterceptor)4