Search in sources :

Example 1 with WeightConfig

use of org.springframework.cloud.gateway.support.WeightConfig in project spring-cloud-gateway by spring-cloud.

the class WeightCalculatorWebFilterTests method assertWeightCalculation.

private void assertWeightCalculation(WeightCalculatorWebFilter filter, String group, int item, int weight, List<Double> normalized, Double... middleRanges) {
    String routeId = route(item);
    filter.addWeightConfig(new WeightConfig(group, routeId, weight));
    Map<String, GroupWeightConfig> groupWeights = filter.getGroupWeights();
    assertThat(groupWeights).containsKey(group);
    GroupWeightConfig config = groupWeights.get(group);
    assertThat(config.group).isEqualTo(group);
    assertThat(config.weights).hasSize(item).containsEntry(routeId, weight);
    assertThat(config.normalizedWeights).hasSize(item);
    for (int i = 0; i < normalized.size(); i++) {
        assertThat(config.normalizedWeights).containsEntry(route(i + 1), normalized.get(i));
    }
    for (int i = 0; i < normalized.size(); i++) {
        assertThat(config.rangeIndexes).containsEntry(i, route(i + 1));
    }
    assertThat(config.ranges).hasSize(item + 1).startsWith(0.0).endsWith(1.0);
    if (middleRanges.length > 0) {
        assertThat(config.ranges).contains(middleRanges);
    }
}
Also used : GroupWeightConfig(org.springframework.cloud.gateway.filter.WeightCalculatorWebFilter.GroupWeightConfig) WeightConfig(org.springframework.cloud.gateway.support.WeightConfig) GroupWeightConfig(org.springframework.cloud.gateway.filter.WeightCalculatorWebFilter.GroupWeightConfig)

Example 2 with WeightConfig

use of org.springframework.cloud.gateway.support.WeightConfig in project spring-cloud-gateway by spring-cloud.

the class WeightCalculatorWebFilterTests method receivesPredicateArgsEvent.

@Test
public void receivesPredicateArgsEvent() {
    WeightCalculatorWebFilter filter = mock(WeightCalculatorWebFilter.class);
    doNothing().when(filter).addWeightConfig(any(WeightConfig.class));
    doCallRealMethod().when(filter).handle(any(PredicateArgsEvent.class));
    HashMap<String, Object> args = new HashMap<>();
    args.put("weight.group", "group1");
    args.put("weight.weight", "1");
    PredicateArgsEvent event = new PredicateArgsEvent(this, "routeA", args);
    filter.handle(event);
    ArgumentCaptor<WeightConfig> configCaptor = ArgumentCaptor.forClass(WeightConfig.class);
    verify(filter).addWeightConfig(configCaptor.capture());
    WeightConfig weightConfig = configCaptor.getValue();
    assertThat(weightConfig.getGroup()).isEqualTo("group1");
    assertThat(weightConfig.getRouteId()).isEqualTo("routeA");
    assertThat(weightConfig.getWeight()).isEqualTo(1);
}
Also used : PredicateArgsEvent(org.springframework.cloud.gateway.event.PredicateArgsEvent) HashMap(java.util.HashMap) GroupWeightConfig(org.springframework.cloud.gateway.filter.WeightCalculatorWebFilter.GroupWeightConfig) WeightConfig(org.springframework.cloud.gateway.support.WeightConfig) Test(org.junit.Test)

Example 3 with WeightConfig

use of org.springframework.cloud.gateway.support.WeightConfig in project spring-cloud-gateway by spring-cloud.

the class WeightCalculatorWebFilterTests method testChooseRouteWithRandom.

@Test
public void testChooseRouteWithRandom() {
    WeightCalculatorWebFilter filter = new WeightCalculatorWebFilter();
    filter.addWeightConfig(new WeightConfig("groupa", "route1", 1));
    filter.addWeightConfig(new WeightConfig("groupa", "route2", 3));
    filter.addWeightConfig(new WeightConfig("groupa", "route3", 6));
    Random random = mock(Random.class);
    when(random.nextDouble()).thenReturn(0.05).thenReturn(0.2).thenReturn(0.6);
    filter.setRandom(random);
    MockServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest.get("http://localhost").build());
    WebFilterChain filterChain = mock(WebFilterChain.class);
    filter.filter(exchange, filterChain);
    Map<String, String> weights = WeightCalculatorWebFilter.getWeights(exchange);
    assertThat(weights).containsEntry("groupa", "route1");
    filter.filter(exchange, filterChain);
    weights = WeightCalculatorWebFilter.getWeights(exchange);
    assertThat(weights).containsEntry("groupa", "route2");
    filter.filter(exchange, filterChain);
    weights = WeightCalculatorWebFilter.getWeights(exchange);
    assertThat(weights).containsEntry("groupa", "route3");
}
Also used : Random(java.util.Random) WebFilterChain(org.springframework.web.server.WebFilterChain) GroupWeightConfig(org.springframework.cloud.gateway.filter.WeightCalculatorWebFilter.GroupWeightConfig) WeightConfig(org.springframework.cloud.gateway.support.WeightConfig) MockServerWebExchange(org.springframework.mock.web.server.MockServerWebExchange) Test(org.junit.Test)

Example 4 with WeightConfig

use of org.springframework.cloud.gateway.support.WeightConfig in project spring-cloud-gateway by spring-cloud.

the class WeightCalculatorWebFilter method handle.

public void handle(PredicateArgsEvent event) {
    Map<String, Object> args = event.getArgs();
    if (args.isEmpty() || !hasRelevantKey(args)) {
        return;
    }
    WeightConfig config = new WeightConfig(event.getRouteId());
    ConfigurationUtils.bind(config, args, WeightConfig.CONFIG_PREFIX, WeightConfig.CONFIG_PREFIX, validator);
    addWeightConfig(config);
}
Also used : WeightConfig(org.springframework.cloud.gateway.support.WeightConfig)

Aggregations

WeightConfig (org.springframework.cloud.gateway.support.WeightConfig)4 GroupWeightConfig (org.springframework.cloud.gateway.filter.WeightCalculatorWebFilter.GroupWeightConfig)3 Test (org.junit.Test)2 HashMap (java.util.HashMap)1 Random (java.util.Random)1 PredicateArgsEvent (org.springframework.cloud.gateway.event.PredicateArgsEvent)1 MockServerWebExchange (org.springframework.mock.web.server.MockServerWebExchange)1 WebFilterChain (org.springframework.web.server.WebFilterChain)1