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);
}
}
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);
}
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");
}
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);
}
Aggregations