|
| 1 | +/* |
| 2 | + * Copyright 2012-2020 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.cloud.loadbalancer.annotation; |
| 18 | + |
| 19 | +import org.junit.jupiter.api.Test; |
| 20 | + |
| 21 | +import org.springframework.boot.autoconfigure.AutoConfigurations; |
| 22 | +import org.springframework.boot.test.context.runner.ApplicationContextRunner; |
| 23 | +import org.springframework.cloud.client.discovery.composite.CompositeDiscoveryClientAutoConfiguration; |
| 24 | +import org.springframework.cloud.client.discovery.composite.reactive.ReactiveCompositeDiscoveryClientAutoConfiguration; |
| 25 | +import org.springframework.cloud.client.loadbalancer.LoadBalanced; |
| 26 | +import org.springframework.cloud.loadbalancer.config.LoadBalancerAutoConfiguration; |
| 27 | +import org.springframework.cloud.loadbalancer.config.LoadBalancerCacheAutoConfiguration; |
| 28 | +import org.springframework.cloud.loadbalancer.core.CachingServiceInstanceListSupplier; |
| 29 | +import org.springframework.cloud.loadbalancer.core.CachingServiceInstanceSupplier; |
| 30 | +import org.springframework.cloud.loadbalancer.core.DelegatingServiceInstanceListSupplier; |
| 31 | +import org.springframework.cloud.loadbalancer.core.DiscoveryClientServiceInstanceListSupplier; |
| 32 | +import org.springframework.cloud.loadbalancer.core.HealthCheckServiceInstanceListSupplier; |
| 33 | +import org.springframework.cloud.loadbalancer.core.ServiceInstanceListSupplier; |
| 34 | +import org.springframework.cloud.loadbalancer.core.ServiceInstanceSupplier; |
| 35 | +import org.springframework.cloud.loadbalancer.core.ZonePreferenceServiceInstanceListSupplier; |
| 36 | +import org.springframework.context.annotation.Bean; |
| 37 | +import org.springframework.context.annotation.Configuration; |
| 38 | +import org.springframework.web.reactive.function.client.WebClient; |
| 39 | + |
| 40 | +import static org.assertj.core.api.BDDAssertions.then; |
| 41 | + |
| 42 | +/** |
| 43 | + * Tests for {@link LoadBalancerClientConfiguration}. |
| 44 | + * |
| 45 | + * @author Olga Maciaszek-Sharma |
| 46 | + */ |
| 47 | +class LoadBalancerClientConfigurationTests { |
| 48 | + |
| 49 | + ApplicationContextRunner reactiveDiscoveryClientRunner = new ApplicationContextRunner() |
| 50 | + .withConfiguration(AutoConfigurations.of( |
| 51 | + ReactiveCompositeDiscoveryClientAutoConfiguration.class, |
| 52 | + LoadBalancerCacheAutoConfiguration.class, |
| 53 | + LoadBalancerAutoConfiguration.class, |
| 54 | + LoadBalancerClientConfiguration.class)); |
| 55 | + |
| 56 | + ApplicationContextRunner blockingDiscoveryClientRunner = new ApplicationContextRunner() |
| 57 | + .withConfiguration( |
| 58 | + AutoConfigurations.of(CompositeDiscoveryClientAutoConfiguration.class, |
| 59 | + LoadBalancerCacheAutoConfiguration.class, |
| 60 | + LoadBalancerAutoConfiguration.class, |
| 61 | + LoadBalancerClientConfiguration.class)); |
| 62 | + |
| 63 | + @Test |
| 64 | + void shouldInstantiateDefaultServiceInstanceListSupplierWhenConfigurationsPropertyNotSet() { |
| 65 | + reactiveDiscoveryClientRunner.run(context -> { |
| 66 | + ServiceInstanceListSupplier supplier = context |
| 67 | + .getBean(ServiceInstanceListSupplier.class); |
| 68 | + then(supplier).isInstanceOf(CachingServiceInstanceListSupplier.class); |
| 69 | + then(((DelegatingServiceInstanceListSupplier) supplier).getDelegate()) |
| 70 | + .isInstanceOf(DiscoveryClientServiceInstanceListSupplier.class); |
| 71 | + }); |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + void shouldInstantiateDefaultServiceInstanceListSupplier() { |
| 76 | + reactiveDiscoveryClientRunner |
| 77 | + .withPropertyValues("spring.cloud.loadbalancer.configurations=default") |
| 78 | + .run(context -> { |
| 79 | + ServiceInstanceListSupplier supplier = context |
| 80 | + .getBean(ServiceInstanceListSupplier.class); |
| 81 | + then(supplier).isInstanceOf(CachingServiceInstanceListSupplier.class); |
| 82 | + then(((DelegatingServiceInstanceListSupplier) supplier).getDelegate()) |
| 83 | + .isInstanceOf( |
| 84 | + DiscoveryClientServiceInstanceListSupplier.class); |
| 85 | + }); |
| 86 | + } |
| 87 | + |
| 88 | + @Test |
| 89 | + void shouldInstantiateZonePreferenceServiceInstanceListSupplier() { |
| 90 | + reactiveDiscoveryClientRunner |
| 91 | + .withPropertyValues( |
| 92 | + "spring.cloud.loadbalancer.configurations=zone-preference") |
| 93 | + .run(context -> { |
| 94 | + ServiceInstanceListSupplier supplier = context |
| 95 | + .getBean(ServiceInstanceListSupplier.class); |
| 96 | + then(supplier).isInstanceOf(CachingServiceInstanceListSupplier.class); |
| 97 | + ServiceInstanceListSupplier delegate = ((DelegatingServiceInstanceListSupplier) supplier) |
| 98 | + .getDelegate(); |
| 99 | + then(delegate).isInstanceOf( |
| 100 | + ZonePreferenceServiceInstanceListSupplier.class); |
| 101 | + ServiceInstanceListSupplier secondDelegate = ((DelegatingServiceInstanceListSupplier) delegate) |
| 102 | + .getDelegate(); |
| 103 | + then(secondDelegate).isInstanceOf( |
| 104 | + DiscoveryClientServiceInstanceListSupplier.class); |
| 105 | + }); |
| 106 | + } |
| 107 | + |
| 108 | + @Test |
| 109 | + void shouldInstantiateHealthCheckServiceInstanceListSupplier() { |
| 110 | + reactiveDiscoveryClientRunner.withUserConfiguration(TestConfig.class) |
| 111 | + .withPropertyValues( |
| 112 | + "spring.cloud.loadbalancer.configurations=health-check") |
| 113 | + .run(context -> { |
| 114 | + ServiceInstanceListSupplier supplier = context |
| 115 | + .getBean(ServiceInstanceListSupplier.class); |
| 116 | + then(supplier).isInstanceOf(CachingServiceInstanceListSupplier.class); |
| 117 | + ServiceInstanceListSupplier delegate = ((DelegatingServiceInstanceListSupplier) supplier) |
| 118 | + .getDelegate(); |
| 119 | + then(delegate) |
| 120 | + .isInstanceOf(HealthCheckServiceInstanceListSupplier.class); |
| 121 | + ServiceInstanceListSupplier secondDelegate = ((DelegatingServiceInstanceListSupplier) delegate) |
| 122 | + .getDelegate(); |
| 123 | + then(secondDelegate).isInstanceOf( |
| 124 | + DiscoveryClientServiceInstanceListSupplier.class); |
| 125 | + }); |
| 126 | + } |
| 127 | + |
| 128 | + @Test |
| 129 | + void shouldInstantiateDefaultBlockingServiceInstanceListSupplierWhenConfigurationsPropertyNotSet() { |
| 130 | + blockingDiscoveryClientRunner.run(context -> { |
| 131 | + ServiceInstanceListSupplier supplier = context |
| 132 | + .getBean(ServiceInstanceListSupplier.class); |
| 133 | + then(supplier).isInstanceOf(CachingServiceInstanceListSupplier.class); |
| 134 | + then(((DelegatingServiceInstanceListSupplier) supplier).getDelegate()) |
| 135 | + .isInstanceOf(DiscoveryClientServiceInstanceListSupplier.class); |
| 136 | + }); |
| 137 | + } |
| 138 | + |
| 139 | + @Test |
| 140 | + void shouldInstantiateDefaultBlockingServiceInstanceListSupplier() { |
| 141 | + blockingDiscoveryClientRunner |
| 142 | + .withPropertyValues("spring.cloud.loadbalancer.configurations=default") |
| 143 | + .run(context -> { |
| 144 | + ServiceInstanceListSupplier supplier = context |
| 145 | + .getBean(ServiceInstanceListSupplier.class); |
| 146 | + then(supplier).isInstanceOf(CachingServiceInstanceListSupplier.class); |
| 147 | + then(((DelegatingServiceInstanceListSupplier) supplier).getDelegate()) |
| 148 | + .isInstanceOf( |
| 149 | + DiscoveryClientServiceInstanceListSupplier.class); |
| 150 | + }); |
| 151 | + } |
| 152 | + |
| 153 | + @Test |
| 154 | + void shouldInstantiateServiceInstanceSupplierRegardlessOfConfigurationProperty() { |
| 155 | + reactiveDiscoveryClientRunner |
| 156 | + .withPropertyValues( |
| 157 | + "spring.cloud.loadbalancer.configurations=zone-preference") |
| 158 | + .run(context -> { |
| 159 | + ServiceInstanceSupplier supplier = context |
| 160 | + .getBean(ServiceInstanceSupplier.class); |
| 161 | + then(supplier).isInstanceOf(CachingServiceInstanceSupplier.class); |
| 162 | + }); |
| 163 | + } |
| 164 | + |
| 165 | + @Configuration |
| 166 | + protected static class TestConfig { |
| 167 | + |
| 168 | + @Bean |
| 169 | + @LoadBalanced |
| 170 | + WebClient.Builder webClientBuilder() { |
| 171 | + return WebClient.builder(); |
| 172 | + } |
| 173 | + |
| 174 | + } |
| 175 | + |
| 176 | +} |
0 commit comments