Merge remote-tracking branch 'origin/3.1.x' into 4.0.x
# Conflicts: # docs/src/main/asciidoc/spring-cloud-commons.adoc # spring-cloud-commons/src/main/java/org/springframework/cloud/client/loadbalancer/LoadBalancerProperties.java # spring-cloud-loadbalancer/src/test/java/org/springframework/cloud/loadbalancer/core/SameInstancePreferenceServiceInstanceListSupplierTests.java # spring-cloud-loadbalancer/src/test/java/org/springframework/cloud/loadbalancer/core/ServiceInstanceListSupplierBuilderTests.java
This commit is contained in:
@@ -94,7 +94,7 @@ public class LoadBalancerClientConfiguration {
|
||||
@Conditional(ZonePreferenceConfigurationCondition.class)
|
||||
public ServiceInstanceListSupplier zonePreferenceDiscoveryClientServiceInstanceListSupplier(
|
||||
ConfigurableApplicationContext context) {
|
||||
return ServiceInstanceListSupplier.builder().withDiscoveryClient().withZonePreference().withCaching()
|
||||
return ServiceInstanceListSupplier.builder().withDiscoveryClient().withCaching().withZonePreference()
|
||||
.build(context);
|
||||
}
|
||||
|
||||
@@ -120,8 +120,8 @@ public class LoadBalancerClientConfiguration {
|
||||
@Conditional(RequestBasedStickySessionConfigurationCondition.class)
|
||||
public ServiceInstanceListSupplier requestBasedStickySessionDiscoveryClientServiceInstanceListSupplier(
|
||||
ConfigurableApplicationContext context) {
|
||||
return ServiceInstanceListSupplier.builder().withDiscoveryClient().withRequestBasedStickySession()
|
||||
.withCaching().build(context);
|
||||
return ServiceInstanceListSupplier.builder().withDiscoveryClient().withCaching()
|
||||
.withRequestBasedStickySession().build(context);
|
||||
}
|
||||
|
||||
@Bean
|
||||
@@ -130,8 +130,8 @@ public class LoadBalancerClientConfiguration {
|
||||
@Conditional(SameInstancePreferenceConfigurationCondition.class)
|
||||
public ServiceInstanceListSupplier sameInstancePreferenceServiceInstanceListSupplier(
|
||||
ConfigurableApplicationContext context) {
|
||||
return ServiceInstanceListSupplier.builder().withDiscoveryClient().withSameInstancePreference()
|
||||
.withCaching().build(context);
|
||||
return ServiceInstanceListSupplier.builder().withDiscoveryClient().withCaching()
|
||||
.withSameInstancePreference().build(context);
|
||||
}
|
||||
|
||||
@Bean
|
||||
@@ -165,8 +165,8 @@ public class LoadBalancerClientConfiguration {
|
||||
@Conditional(ZonePreferenceConfigurationCondition.class)
|
||||
public ServiceInstanceListSupplier zonePreferenceDiscoveryClientServiceInstanceListSupplier(
|
||||
ConfigurableApplicationContext context) {
|
||||
return ServiceInstanceListSupplier.builder().withBlockingDiscoveryClient().withZonePreference()
|
||||
.withCaching().build(context);
|
||||
return ServiceInstanceListSupplier.builder().withBlockingDiscoveryClient().withCaching()
|
||||
.withZonePreference().build(context);
|
||||
}
|
||||
|
||||
@Bean
|
||||
@@ -185,8 +185,8 @@ public class LoadBalancerClientConfiguration {
|
||||
@Conditional(RequestBasedStickySessionConfigurationCondition.class)
|
||||
public ServiceInstanceListSupplier requestBasedStickySessionDiscoveryClientServiceInstanceListSupplier(
|
||||
ConfigurableApplicationContext context) {
|
||||
return ServiceInstanceListSupplier.builder().withBlockingDiscoveryClient().withRequestBasedStickySession()
|
||||
.withCaching().build(context);
|
||||
return ServiceInstanceListSupplier.builder().withBlockingDiscoveryClient().withCaching()
|
||||
.withRequestBasedStickySession().build(context);
|
||||
}
|
||||
|
||||
@Bean
|
||||
@@ -195,8 +195,8 @@ public class LoadBalancerClientConfiguration {
|
||||
@Conditional(SameInstancePreferenceConfigurationCondition.class)
|
||||
public ServiceInstanceListSupplier sameInstancePreferenceServiceInstanceListSupplier(
|
||||
ConfigurableApplicationContext context) {
|
||||
return ServiceInstanceListSupplier.builder().withBlockingDiscoveryClient().withSameInstancePreference()
|
||||
.withCaching().build(context);
|
||||
return ServiceInstanceListSupplier.builder().withBlockingDiscoveryClient().withCaching()
|
||||
.withSameInstancePreference().build(context);
|
||||
}
|
||||
|
||||
@Bean
|
||||
|
||||
@@ -40,12 +40,12 @@ public abstract class DelegatingServiceInstanceListSupplier
|
||||
}
|
||||
|
||||
public ServiceInstanceListSupplier getDelegate() {
|
||||
return this.delegate;
|
||||
return delegate;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getServiceId() {
|
||||
return this.delegate.getServiceId();
|
||||
return delegate.getServiceId();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -24,6 +24,8 @@ import org.apache.commons.logging.LogFactory;
|
||||
import reactor.core.publisher.Flux;
|
||||
|
||||
import org.springframework.cloud.client.ServiceInstance;
|
||||
import org.springframework.cloud.client.loadbalancer.Request;
|
||||
import org.springframework.cloud.client.loadbalancer.reactive.ReactiveLoadBalancer;
|
||||
|
||||
/**
|
||||
* An implementation of {@link ServiceInstanceListSupplier} that selects the previously
|
||||
@@ -40,10 +42,19 @@ public class SameInstancePreferenceServiceInstanceListSupplier extends Delegatin
|
||||
|
||||
private ServiceInstance previouslyReturnedInstance;
|
||||
|
||||
private boolean callGetWithRequestOnDelegates;
|
||||
|
||||
public SameInstancePreferenceServiceInstanceListSupplier(ServiceInstanceListSupplier delegate) {
|
||||
super(delegate);
|
||||
}
|
||||
|
||||
public SameInstancePreferenceServiceInstanceListSupplier(ServiceInstanceListSupplier delegate,
|
||||
ReactiveLoadBalancer.Factory<ServiceInstance> loadBalancerClientFactory) {
|
||||
super(delegate);
|
||||
callGetWithRequestOnDelegates = loadBalancerClientFactory.getProperties(getServiceId())
|
||||
.isCallGetWithRequestOnDelegates();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getServiceId() {
|
||||
return delegate.getServiceId();
|
||||
@@ -54,6 +65,14 @@ public class SameInstancePreferenceServiceInstanceListSupplier extends Delegatin
|
||||
return delegate.get().map(this::filteredBySameInstancePreference);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Flux<List<ServiceInstance>> get(Request request) {
|
||||
if (callGetWithRequestOnDelegates) {
|
||||
return delegate.get(request).map(this::filteredBySameInstancePreference);
|
||||
}
|
||||
return get();
|
||||
}
|
||||
|
||||
private List<ServiceInstance> filteredBySameInstancePreference(List<ServiceInstance> serviceInstances) {
|
||||
if (previouslyReturnedInstance != null && serviceInstances.contains(previouslyReturnedInstance)) {
|
||||
if (LOG.isDebugEnabled()) {
|
||||
|
||||
@@ -58,8 +58,6 @@ public final class ServiceInstanceListSupplierBuilder {
|
||||
|
||||
private Creator baseCreator;
|
||||
|
||||
private DelegateCreator cachingCreator;
|
||||
|
||||
private final List<DelegateCreator> creators = new ArrayList<>();
|
||||
|
||||
ServiceInstanceListSupplierBuilder() {
|
||||
@@ -174,8 +172,10 @@ public final class ServiceInstanceListSupplierBuilder {
|
||||
* @return the {@link ServiceInstanceListSupplierBuilder} object
|
||||
*/
|
||||
public ServiceInstanceListSupplierBuilder withSameInstancePreference() {
|
||||
DelegateCreator creator = (context,
|
||||
delegate) -> new SameInstancePreferenceServiceInstanceListSupplier(delegate);
|
||||
DelegateCreator creator = (context, delegate) -> {
|
||||
LoadBalancerClientFactory loadBalancerClientFactory = context.getBean(LoadBalancerClientFactory.class);
|
||||
return new SameInstancePreferenceServiceInstanceListSupplier(delegate, loadBalancerClientFactory);
|
||||
};
|
||||
this.creators.add(creator);
|
||||
return this;
|
||||
}
|
||||
@@ -217,8 +217,9 @@ public final class ServiceInstanceListSupplierBuilder {
|
||||
*/
|
||||
public ServiceInstanceListSupplierBuilder withZonePreference() {
|
||||
DelegateCreator creator = (context, delegate) -> {
|
||||
LoadBalancerClientFactory loadBalancerClientFactory = context.getBean(LoadBalancerClientFactory.class);
|
||||
LoadBalancerZoneConfig zoneConfig = context.getBean(LoadBalancerZoneConfig.class);
|
||||
return new ZonePreferenceServiceInstanceListSupplier(delegate, zoneConfig);
|
||||
return new ZonePreferenceServiceInstanceListSupplier(delegate, zoneConfig, loadBalancerClientFactory);
|
||||
};
|
||||
this.creators.add(creator);
|
||||
return this;
|
||||
@@ -232,8 +233,9 @@ public final class ServiceInstanceListSupplierBuilder {
|
||||
*/
|
||||
public ServiceInstanceListSupplierBuilder withZonePreference(String zoneName) {
|
||||
DelegateCreator creator = (context, delegate) -> {
|
||||
LoadBalancerClientFactory loadBalancerClientFactory = context.getBean(LoadBalancerClientFactory.class);
|
||||
LoadBalancerZoneConfig zoneConfig = new LoadBalancerZoneConfig(zoneName);
|
||||
return new ZonePreferenceServiceInstanceListSupplier(delegate, zoneConfig);
|
||||
return new ZonePreferenceServiceInstanceListSupplier(delegate, zoneConfig, loadBalancerClientFactory);
|
||||
};
|
||||
this.creators.add(creator);
|
||||
return this;
|
||||
@@ -254,19 +256,15 @@ public final class ServiceInstanceListSupplierBuilder {
|
||||
}
|
||||
|
||||
/**
|
||||
* If {@link LoadBalancerCacheManager} is available in the context, wraps created
|
||||
* {@link ServiceInstanceListSupplier} hierarchy with a
|
||||
* {@link CachingServiceInstanceListSupplier} instance to provide a caching mechanism
|
||||
* for service instances. Uses {@link ObjectProvider} to lazily resolve
|
||||
* If {@link LoadBalancerCacheManager} is available in the context, adds a
|
||||
* {@link CachingServiceInstanceListSupplier} instance to the
|
||||
* {@link ServiceInstanceListSupplier} hierarchy to provide a caching mechanism for
|
||||
* service instances. Uses {@link ObjectProvider} to lazily resolve
|
||||
* {@link LoadBalancerCacheManager}.
|
||||
* @return the {@link ServiceInstanceListSupplierBuilder} object
|
||||
*/
|
||||
public ServiceInstanceListSupplierBuilder withCaching() {
|
||||
if (cachingCreator != null && LOG.isWarnEnabled()) {
|
||||
LOG.warn(
|
||||
"Overriding a previously set cachingCreator with a CachingServiceInstanceListSupplier-based cachingCreator.");
|
||||
}
|
||||
this.cachingCreator = (context, delegate) -> {
|
||||
DelegateCreator creator = (context, delegate) -> {
|
||||
ObjectProvider<LoadBalancerCacheManager> cacheManagerProvider = context
|
||||
.getBeanProvider(LoadBalancerCacheManager.class);
|
||||
if (cacheManagerProvider.getIfAvailable() != null) {
|
||||
@@ -277,6 +275,7 @@ public final class ServiceInstanceListSupplierBuilder {
|
||||
}
|
||||
return delegate;
|
||||
};
|
||||
creators.add(creator);
|
||||
return this;
|
||||
}
|
||||
|
||||
@@ -323,9 +322,6 @@ public final class ServiceInstanceListSupplierBuilder {
|
||||
supplier = creator.apply(context, supplier);
|
||||
}
|
||||
|
||||
if (this.cachingCreator != null) {
|
||||
supplier = this.cachingCreator.apply(context, supplier);
|
||||
}
|
||||
return supplier;
|
||||
}
|
||||
|
||||
|
||||
@@ -23,6 +23,8 @@ import java.util.Map;
|
||||
import reactor.core.publisher.Flux;
|
||||
|
||||
import org.springframework.cloud.client.ServiceInstance;
|
||||
import org.springframework.cloud.client.loadbalancer.Request;
|
||||
import org.springframework.cloud.client.loadbalancer.reactive.ReactiveLoadBalancer;
|
||||
import org.springframework.cloud.loadbalancer.config.LoadBalancerZoneConfig;
|
||||
|
||||
/**
|
||||
@@ -43,17 +45,36 @@ public class ZonePreferenceServiceInstanceListSupplier extends DelegatingService
|
||||
|
||||
private String zone;
|
||||
|
||||
private boolean callGetWithRequestOnDelegates;
|
||||
|
||||
public ZonePreferenceServiceInstanceListSupplier(ServiceInstanceListSupplier delegate,
|
||||
LoadBalancerZoneConfig zoneConfig) {
|
||||
super(delegate);
|
||||
this.zoneConfig = zoneConfig;
|
||||
}
|
||||
|
||||
public ZonePreferenceServiceInstanceListSupplier(ServiceInstanceListSupplier delegate,
|
||||
LoadBalancerZoneConfig zoneConfig,
|
||||
ReactiveLoadBalancer.Factory<ServiceInstance> loadBalancerClientFactory) {
|
||||
super(delegate);
|
||||
this.zoneConfig = zoneConfig;
|
||||
callGetWithRequestOnDelegates = loadBalancerClientFactory.getProperties(getServiceId())
|
||||
.isCallGetWithRequestOnDelegates();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Flux<List<ServiceInstance>> get() {
|
||||
return getDelegate().get().map(this::filteredByZone);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Flux<List<ServiceInstance>> get(Request request) {
|
||||
if (callGetWithRequestOnDelegates) {
|
||||
return getDelegate().get(request).map(this::filteredByZone);
|
||||
}
|
||||
return get();
|
||||
}
|
||||
|
||||
private List<ServiceInstance> filteredByZone(List<ServiceInstance> serviceInstances) {
|
||||
if (zone == null) {
|
||||
zone = zoneConfig.getZone();
|
||||
|
||||
@@ -93,10 +93,10 @@ class LoadBalancerClientConfigurationTests {
|
||||
reactiveDiscoveryClientRunner.withPropertyValues("spring.cloud.loadbalancer.configurations=zone-preference")
|
||||
.run(context -> {
|
||||
ServiceInstanceListSupplier supplier = context.getBean(ServiceInstanceListSupplier.class);
|
||||
then(supplier).isInstanceOf(CachingServiceInstanceListSupplier.class);
|
||||
then(supplier).isInstanceOf(ZonePreferenceServiceInstanceListSupplier.class);
|
||||
ServiceInstanceListSupplier delegate = ((DelegatingServiceInstanceListSupplier) supplier)
|
||||
.getDelegate();
|
||||
then(delegate).isInstanceOf(ZonePreferenceServiceInstanceListSupplier.class);
|
||||
then(delegate).isInstanceOf(CachingServiceInstanceListSupplier.class);
|
||||
ServiceInstanceListSupplier secondDelegate = ((DelegatingServiceInstanceListSupplier) delegate)
|
||||
.getDelegate();
|
||||
then(secondDelegate).isInstanceOf(DiscoveryClientServiceInstanceListSupplier.class);
|
||||
@@ -136,10 +136,10 @@ class LoadBalancerClientConfigurationTests {
|
||||
.withPropertyValues("spring.cloud.loadbalancer.configurations=request-based-sticky-session")
|
||||
.run(context -> {
|
||||
ServiceInstanceListSupplier supplier = context.getBean(ServiceInstanceListSupplier.class);
|
||||
then(supplier).isInstanceOf(CachingServiceInstanceListSupplier.class);
|
||||
then(supplier).isInstanceOf(RequestBasedStickySessionServiceInstanceListSupplier.class);
|
||||
ServiceInstanceListSupplier delegate = ((DelegatingServiceInstanceListSupplier) supplier)
|
||||
.getDelegate();
|
||||
then(delegate).isInstanceOf(RequestBasedStickySessionServiceInstanceListSupplier.class);
|
||||
then(delegate).isInstanceOf(CachingServiceInstanceListSupplier.class);
|
||||
ServiceInstanceListSupplier secondDelegate = ((DelegatingServiceInstanceListSupplier) delegate)
|
||||
.getDelegate();
|
||||
then(secondDelegate).isInstanceOf(DiscoveryClientServiceInstanceListSupplier.class);
|
||||
|
||||
@@ -19,11 +19,17 @@ package org.springframework.cloud.loadbalancer.core;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import reactor.core.publisher.Flux;
|
||||
|
||||
import org.springframework.cloud.client.DefaultServiceInstance;
|
||||
import org.springframework.cloud.client.ServiceInstance;
|
||||
import org.springframework.cloud.client.loadbalancer.DefaultRequest;
|
||||
import org.springframework.cloud.client.loadbalancer.DefaultRequestContext;
|
||||
import org.springframework.cloud.client.loadbalancer.LoadBalancerProperties;
|
||||
import org.springframework.cloud.client.loadbalancer.Request;
|
||||
import org.springframework.cloud.loadbalancer.support.LoadBalancerClientFactory;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
@@ -43,8 +49,9 @@ class SameInstancePreferenceServiceInstanceListSupplierTests {
|
||||
private final DiscoveryClientServiceInstanceListSupplier delegate = mock(
|
||||
DiscoveryClientServiceInstanceListSupplier.class);
|
||||
|
||||
private final SameInstancePreferenceServiceInstanceListSupplier supplier = new SameInstancePreferenceServiceInstanceListSupplier(
|
||||
delegate);
|
||||
private final LoadBalancerClientFactory loadBalancerClientFactory = mock(LoadBalancerClientFactory.class);
|
||||
|
||||
private SameInstancePreferenceServiceInstanceListSupplier supplier;
|
||||
|
||||
private final ServiceInstance first = serviceInstance("test-1");
|
||||
|
||||
@@ -52,6 +59,14 @@ class SameInstancePreferenceServiceInstanceListSupplierTests {
|
||||
|
||||
private final ServiceInstance third = serviceInstance("test-3");
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
LoadBalancerProperties properties = new LoadBalancerProperties();
|
||||
properties.setCallGetWithRequestOnDelegates(true);
|
||||
when(loadBalancerClientFactory.getProperties(any())).thenReturn(properties);
|
||||
supplier = new SameInstancePreferenceServiceInstanceListSupplier(delegate, loadBalancerClientFactory);
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldReturnPreviouslySelectedInstanceIfAvailable() {
|
||||
when(delegate.get()).thenReturn(Flux.just(Arrays.asList(first, second, third)));
|
||||
@@ -73,7 +88,7 @@ class SameInstancePreferenceServiceInstanceListSupplierTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldReturnAllInstancesFromDelegateIfPreviouslySelectedInstanceIfAvailable() {
|
||||
void shouldReturnAllInstancesFromDelegateIfPreviouslySelectedInstanceIsNotAvailable() {
|
||||
when(delegate.get()).thenReturn(Flux.just(Arrays.asList(second, third)));
|
||||
supplier.selectedServiceInstance(first);
|
||||
|
||||
@@ -82,6 +97,17 @@ class SameInstancePreferenceServiceInstanceListSupplierTests {
|
||||
assertThat(instances).hasSize(2);
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldCallGetRequestOnDelegate() {
|
||||
Request<DefaultRequestContext> request = new DefaultRequest<>(new DefaultRequestContext());
|
||||
when(delegate.get()).thenReturn(Flux.just(Arrays.asList(first, second, third)));
|
||||
when(delegate.get(request)).thenReturn(Flux.just(Arrays.asList(first, second)));
|
||||
|
||||
List<ServiceInstance> instances = supplier.get(request).blockFirst();
|
||||
|
||||
assertThat(instances).hasSize(2);
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldCallSelectedServiceInstanceOnItsDelegate() {
|
||||
ServiceInstance firstInstance = serviceInstance("test-4");
|
||||
|
||||
@@ -37,11 +37,9 @@ public class ServiceInstanceListSupplierBuilderTests {
|
||||
public void testBuilder() {
|
||||
new ApplicationContextRunner().withUserConfiguration(CacheTestConfig.class).run(context -> {
|
||||
ServiceInstanceListSupplier supplier = ServiceInstanceListSupplier.builder().withDiscoveryClient()
|
||||
.withHealthChecks().withWeighted().withCaching().build(context);
|
||||
assertThat(supplier).isInstanceOf(CachingServiceInstanceListSupplier.class);
|
||||
.withHealthChecks().withWeighted().build(context);
|
||||
assertThat(supplier).isInstanceOf(WeightedServiceInstanceListSupplier.class);
|
||||
DelegatingServiceInstanceListSupplier delegating = (DelegatingServiceInstanceListSupplier) supplier;
|
||||
assertThat(delegating.getDelegate()).isInstanceOf(WeightedServiceInstanceListSupplier.class);
|
||||
delegating = (DelegatingServiceInstanceListSupplier) delegating.getDelegate();
|
||||
assertThat(delegating.getDelegate()).isInstanceOf(HealthCheckServiceInstanceListSupplier.class);
|
||||
delegating = (DelegatingServiceInstanceListSupplier) delegating.getDelegate();
|
||||
assertThat(delegating.getDelegate()).isInstanceOf(DiscoveryClientServiceInstanceListSupplier.class);
|
||||
|
||||
@@ -22,15 +22,22 @@ import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import reactor.core.publisher.Flux;
|
||||
|
||||
import org.springframework.cloud.client.DefaultServiceInstance;
|
||||
import org.springframework.cloud.client.ServiceInstance;
|
||||
import org.springframework.cloud.client.loadbalancer.DefaultRequest;
|
||||
import org.springframework.cloud.client.loadbalancer.DefaultRequestContext;
|
||||
import org.springframework.cloud.client.loadbalancer.LoadBalancerProperties;
|
||||
import org.springframework.cloud.client.loadbalancer.Request;
|
||||
import org.springframework.cloud.loadbalancer.config.LoadBalancerZoneConfig;
|
||||
import org.springframework.cloud.loadbalancer.support.LoadBalancerClientFactory;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatCode;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@@ -46,8 +53,9 @@ class ZonePreferenceServiceInstanceListSupplierTests {
|
||||
|
||||
private final LoadBalancerZoneConfig zoneConfig = new LoadBalancerZoneConfig(null);
|
||||
|
||||
private final ZonePreferenceServiceInstanceListSupplier supplier = new ZonePreferenceServiceInstanceListSupplier(
|
||||
delegate, zoneConfig);
|
||||
private ZonePreferenceServiceInstanceListSupplier supplier;
|
||||
|
||||
private final LoadBalancerClientFactory loadBalancerClientFactory = mock(LoadBalancerClientFactory.class);
|
||||
|
||||
private final ServiceInstance first = serviceInstance("test-1", buildZoneMetadata("zone1"));
|
||||
|
||||
@@ -59,6 +67,14 @@ class ZonePreferenceServiceInstanceListSupplierTests {
|
||||
|
||||
private final ServiceInstance fifth = serviceInstance("test-5", buildZoneMetadata(null));
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
LoadBalancerProperties properties = new LoadBalancerProperties();
|
||||
properties.setCallGetWithRequestOnDelegates(true);
|
||||
when(loadBalancerClientFactory.getProperties(any())).thenReturn(properties);
|
||||
supplier = new ZonePreferenceServiceInstanceListSupplier(delegate, zoneConfig, loadBalancerClientFactory);
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldFilterInstancesByZone() {
|
||||
zoneConfig.setZone("zone1");
|
||||
@@ -73,6 +89,19 @@ class ZonePreferenceServiceInstanceListSupplierTests {
|
||||
assertThat(filtered).doesNotContain(fifth);
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldCallGetRequestOnDelegate() {
|
||||
zoneConfig.setZone("zone1");
|
||||
Request<DefaultRequestContext> request = new DefaultRequest<>(new DefaultRequestContext());
|
||||
when(delegate.get()).thenReturn(Flux.just(Arrays.asList(first, second, third, fourth, fifth)));
|
||||
when(delegate.get(request)).thenReturn(Flux.just(Arrays.asList(first, third, fourth, fifth)));
|
||||
|
||||
List<ServiceInstance> filtered = supplier.get(request).blockFirst();
|
||||
|
||||
assertThat(filtered).hasSize(1);
|
||||
assertThat(filtered).containsOnly(first);
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldReturnAllInstancesIfNoZoneInstances() {
|
||||
zoneConfig.setZone("zone1");
|
||||
|
||||
Reference in New Issue
Block a user