Search in sources :

Example 96 with SelectorData

use of org.apache.shenyu.common.dto.SelectorData in project incubator-shenyu by apache.

the class Resilience4JPluginTest method circuitBreakerTest.

@Test
@SuppressWarnings({ "rawtypes", "unchecked" })
public void circuitBreakerTest() {
    RuleData data = mock(RuleData.class);
    data.setSelectorId("SHENYU");
    data.setId("SHENYU");
    Resilience4JHandle resilience4JHandle = GsonUtils.getGson().fromJson(HANDLER, Resilience4JHandle.class);
    Resilience4JHandler.CACHED_HANDLE.get().cachedHandle(CacheKeyUtils.INST.getKey(data), resilience4JHandle);
    CombinedExecutor combinedExecutor = new CombinedExecutor();
    resilience4JPlugin = new Resilience4JPlugin(combinedExecutor, new RateLimiterExecutor());
    Mono mono = Mono.error(CallNotPermittedException.createCallNotPermittedException(circuitBreaker)).onErrorResume(throwable -> {
        if (throwable instanceof CallNotPermittedException) {
            exchange.getResponse().setStatusCode(HttpStatus.INTERNAL_SERVER_ERROR);
        }
        return Mono.error(throwable);
    });
    when(data.getHandle()).thenReturn(HANDLER);
    when(chain.execute(exchange)).thenReturn(mono);
    when(data.getSelectorId()).thenReturn("circuitBreaker");
    when(data.getName()).thenReturn("ruleData");
    Resilience4JHandler.CACHED_HANDLE.get().cachedHandle(CacheKeyUtils.INST.getKey(data), resilience4JHandle);
    SelectorData selectorData = mock(SelectorData.class);
    StepVerifier.create(resilience4JPlugin.doExecute(exchange, chain, selectorData, data)).expectSubscription().expectError().verify();
    assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, exchange.getResponse().getStatusCode());
}
Also used : CallNotPermittedException(io.github.resilience4j.circuitbreaker.CallNotPermittedException) RuleData(org.apache.shenyu.common.dto.RuleData) CombinedExecutor(org.apache.shenyu.plugin.resilience4j.executor.CombinedExecutor) Mono(reactor.core.publisher.Mono) Resilience4JHandle(org.apache.shenyu.common.dto.convert.rule.Resilience4JHandle) RateLimiterExecutor(org.apache.shenyu.plugin.resilience4j.executor.RateLimiterExecutor) SelectorData(org.apache.shenyu.common.dto.SelectorData) Test(org.junit.jupiter.api.Test)

Aggregations

SelectorData (org.apache.shenyu.common.dto.SelectorData)96 Test (org.junit.jupiter.api.Test)60 RuleData (org.apache.shenyu.common.dto.RuleData)47 ShenyuContext (org.apache.shenyu.plugin.api.context.ShenyuContext)18 ShenyuPluginChain (org.apache.shenyu.plugin.api.ShenyuPluginChain)16 ServerWebExchange (org.springframework.web.server.ServerWebExchange)16 List (java.util.List)14 Objects (java.util.Objects)12 PluginData (org.apache.shenyu.common.dto.PluginData)12 Mono (reactor.core.publisher.Mono)12 Map (java.util.Map)10 MetaData (org.apache.shenyu.common.dto.MetaData)10 PluginEnum (org.apache.shenyu.common.enums.PluginEnum)10 Logger (org.slf4j.Logger)10 LoggerFactory (org.slf4j.LoggerFactory)10 ArrayList (java.util.ArrayList)8 Optional (java.util.Optional)8 CollectionUtils (org.apache.commons.collections4.CollectionUtils)7 AbstractShenyuPlugin (org.apache.shenyu.plugin.base.AbstractShenyuPlugin)7 Collectors (java.util.stream.Collectors)6