|
1 | 1 | package org.springframework.cloud.client.loadbalancer;
|
2 | 2 |
|
3 |
| -import static org.hamcrest.Matchers.empty; |
4 |
| -import static org.hamcrest.Matchers.hasSize; |
5 |
| -import static org.hamcrest.Matchers.instanceOf; |
6 |
| -import static org.hamcrest.Matchers.is; |
7 |
| -import static org.hamcrest.Matchers.notNullValue; |
8 |
| -import static org.junit.Assert.assertThat; |
9 |
| - |
10 | 3 | import java.net.URI;
|
11 | 4 | import java.util.Collection;
|
12 | 5 | import java.util.List;
|
13 | 6 | import java.util.Map;
|
14 | 7 | import java.util.Random;
|
15 | 8 |
|
16 |
| -import lombok.SneakyThrows; |
17 |
| - |
18 | 9 | import org.junit.Test;
|
19 | 10 | import org.springframework.beans.factory.annotation.Autowired;
|
20 | 11 | import org.springframework.boot.builder.SpringApplicationBuilder;
|
|
27 | 18 | import org.springframework.http.client.ClientHttpRequestInterceptor;
|
28 | 19 | import org.springframework.web.client.RestTemplate;
|
29 | 20 |
|
| 21 | +import static org.hamcrest.Matchers.empty; |
| 22 | +import static org.hamcrest.Matchers.hasSize; |
| 23 | +import static org.hamcrest.Matchers.instanceOf; |
| 24 | +import static org.hamcrest.Matchers.is; |
| 25 | +import static org.hamcrest.Matchers.notNullValue; |
| 26 | +import static org.junit.Assert.assertThat; |
| 27 | + |
| 28 | +import lombok.SneakyThrows; |
| 29 | + |
30 | 30 | /**
|
31 | 31 | * @author Spencer Gibb
|
32 | 32 | */
|
33 | 33 | public class LoadBalancerAutoConfigurationTests {
|
34 | 34 |
|
35 |
| - @Test |
36 |
| - public void restTemplateGetsLoadBalancerInterceptor() { |
37 |
| - ConfigurableApplicationContext context = init(OneRestTemplate.class); |
38 |
| - final Map<String, RestTemplate> restTemplates = context.getBeansOfType(RestTemplate.class); |
39 |
| - |
40 |
| - assertThat(restTemplates, is(notNullValue())); |
41 |
| - assertThat(restTemplates.values(), hasSize(1)); |
42 |
| - RestTemplate restTemplate = restTemplates.values().iterator().next(); |
43 |
| - assertThat(restTemplate, is(notNullValue())); |
44 |
| - |
45 |
| - assertLoadBalanced(restTemplate); |
46 |
| - } |
47 |
| - |
48 |
| - protected void assertLoadBalanced(RestTemplate restTemplate) { |
49 |
| - List<ClientHttpRequestInterceptor> interceptors = restTemplate.getInterceptors(); |
50 |
| - assertThat(interceptors, hasSize(1)); |
51 |
| - ClientHttpRequestInterceptor interceptor = interceptors.get(0); |
52 |
| - assertThat(interceptor, is(instanceOf(LoadBalancerInterceptor.class))); |
53 |
| - } |
54 |
| - |
55 |
| - @Test |
56 |
| - public void multipleRestTemplates() { |
57 |
| - ConfigurableApplicationContext context = init(TwoRestTemplates.class); |
58 |
| - final Map<String, RestTemplate> restTemplates = context.getBeansOfType(RestTemplate.class); |
59 |
| - |
60 |
| - assertThat(restTemplates, is(notNullValue())); |
61 |
| - Collection<RestTemplate> templates = restTemplates.values(); |
62 |
| - assertThat(templates, hasSize(2)); |
63 |
| - |
64 |
| - TwoRestTemplates.Two two = context.getBean(TwoRestTemplates.Two.class); |
65 |
| - |
66 |
| - assertThat(two.loadBalanced, is(notNullValue())); |
67 |
| - assertLoadBalanced(two.loadBalanced); |
68 |
| - |
69 |
| - assertThat(two.nonLoadBalanced, is(notNullValue())); |
70 |
| - assertThat(two.nonLoadBalanced.getInterceptors(), is(empty())); |
71 |
| - } |
72 |
| - |
73 |
| - |
74 |
| - protected ConfigurableApplicationContext init(Class<?> config) { |
75 |
| - return new SpringApplicationBuilder().web(false).sources(config, LoadBalancerAutoConfiguration.class).run(); |
76 |
| - } |
77 |
| - |
78 |
| - @Configuration |
79 |
| - protected static class OneRestTemplate { |
80 |
| - |
81 |
| - @Bean |
82 |
| - LoadBalancerClient loadBalancerClient() { |
83 |
| - return new NoopLoadBalancerClient(); |
84 |
| - } |
85 |
| - |
86 |
| - } |
87 |
| - |
88 |
| - @Configuration |
89 |
| - protected static class TwoRestTemplates { |
90 |
| - |
91 |
| - @Primary |
92 |
| - @Bean |
93 |
| - RestTemplate restTemplate() { |
94 |
| - return new RestTemplate(); |
95 |
| - } |
96 |
| - |
97 |
| - @Bean |
98 |
| - LoadBalancerClient loadBalancerClient() { |
99 |
| - return new NoopLoadBalancerClient(); |
100 |
| - } |
101 |
| - |
102 |
| - @Configuration |
103 |
| - protected static class Two { |
104 |
| - @Autowired |
105 |
| - RestTemplate nonLoadBalanced; |
106 |
| - |
107 |
| - @Autowired |
108 |
| - @LoadBalanced |
109 |
| - RestTemplate loadBalanced; |
110 |
| - } |
111 |
| - |
112 |
| - } |
113 |
| - |
114 |
| - private static class NoopLoadBalancerClient implements LoadBalancerClient { |
115 |
| - private final Random random = new Random(); |
116 |
| - |
117 |
| - @Override |
118 |
| - public ServiceInstance choose(String serviceId) { |
119 |
| - return new DefaultServiceInstance(serviceId, serviceId, random.nextInt(40000), false); |
120 |
| - } |
121 |
| - |
122 |
| - @Override |
123 |
| - @SneakyThrows |
124 |
| - public <T> T execute(String serviceId, LoadBalancerRequest<T> request) { |
125 |
| - return request.apply(choose(serviceId)); |
126 |
| - } |
127 |
| - |
128 |
| - @Override |
129 |
| - public URI reconstructURI(ServiceInstance instance, URI original) { |
130 |
| - return DefaultServiceInstance.getUri(instance); |
131 |
| - } |
132 |
| - } |
| 35 | + @Test |
| 36 | + public void restTemplateGetsLoadBalancerInterceptor() { |
| 37 | + ConfigurableApplicationContext context = init(OneRestTemplate.class); |
| 38 | + final Map<String, RestTemplate> restTemplates = context |
| 39 | + .getBeansOfType(RestTemplate.class); |
| 40 | + |
| 41 | + assertThat(restTemplates, is(notNullValue())); |
| 42 | + assertThat(restTemplates.values(), hasSize(1)); |
| 43 | + RestTemplate restTemplate = restTemplates.values().iterator().next(); |
| 44 | + assertThat(restTemplate, is(notNullValue())); |
| 45 | + |
| 46 | + assertLoadBalanced(restTemplate); |
| 47 | + } |
| 48 | + |
| 49 | + protected void assertLoadBalanced(RestTemplate restTemplate) { |
| 50 | + List<ClientHttpRequestInterceptor> interceptors = restTemplate.getInterceptors(); |
| 51 | + assertThat(interceptors, hasSize(1)); |
| 52 | + ClientHttpRequestInterceptor interceptor = interceptors.get(0); |
| 53 | + assertThat(interceptor, is(instanceOf(LoadBalancerInterceptor.class))); |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + public void multipleRestTemplates() { |
| 58 | + ConfigurableApplicationContext context = init(TwoRestTemplates.class); |
| 59 | + final Map<String, RestTemplate> restTemplates = context |
| 60 | + .getBeansOfType(RestTemplate.class); |
| 61 | + |
| 62 | + assertThat(restTemplates, is(notNullValue())); |
| 63 | + Collection<RestTemplate> templates = restTemplates.values(); |
| 64 | + assertThat(templates, hasSize(2)); |
| 65 | + |
| 66 | + TwoRestTemplates.Two two = context.getBean(TwoRestTemplates.Two.class); |
| 67 | + |
| 68 | + assertThat(two.loadBalanced, is(notNullValue())); |
| 69 | + assertLoadBalanced(two.loadBalanced); |
| 70 | + |
| 71 | + assertThat(two.nonLoadBalanced, is(notNullValue())); |
| 72 | + assertThat(two.nonLoadBalanced.getInterceptors(), is(empty())); |
| 73 | + } |
| 74 | + |
| 75 | + protected ConfigurableApplicationContext init(Class<?> config) { |
| 76 | + return new SpringApplicationBuilder().web(false) |
| 77 | + .properties("spring.aop.proxyTargetClass=true") |
| 78 | + .sources(config, LoadBalancerAutoConfiguration.class).run(); |
| 79 | + } |
| 80 | + |
| 81 | + @Configuration |
| 82 | + protected static class OneRestTemplate { |
| 83 | + |
| 84 | + @LoadBalanced |
| 85 | + @Bean |
| 86 | + RestTemplate loadBalancedRestTemplate() { |
| 87 | + return new RestTemplate(); |
| 88 | + } |
| 89 | + |
| 90 | + @Bean |
| 91 | + LoadBalancerClient loadBalancerClient() { |
| 92 | + return new NoopLoadBalancerClient(); |
| 93 | + } |
| 94 | + |
| 95 | + } |
| 96 | + |
| 97 | + @Configuration |
| 98 | + protected static class TwoRestTemplates { |
| 99 | + |
| 100 | + @Primary |
| 101 | + @Bean |
| 102 | + RestTemplate restTemplate() { |
| 103 | + return new RestTemplate(); |
| 104 | + } |
| 105 | + |
| 106 | + @LoadBalanced |
| 107 | + @Bean |
| 108 | + RestTemplate loadBalancedRestTemplate() { |
| 109 | + return new RestTemplate(); |
| 110 | + } |
| 111 | + |
| 112 | + @Bean |
| 113 | + LoadBalancerClient loadBalancerClient() { |
| 114 | + return new NoopLoadBalancerClient(); |
| 115 | + } |
| 116 | + |
| 117 | + @Configuration |
| 118 | + protected static class Two { |
| 119 | + @Autowired |
| 120 | + RestTemplate nonLoadBalanced; |
| 121 | + |
| 122 | + @Autowired |
| 123 | + @LoadBalanced |
| 124 | + RestTemplate loadBalanced; |
| 125 | + } |
| 126 | + |
| 127 | + } |
| 128 | + |
| 129 | + private static class NoopLoadBalancerClient implements LoadBalancerClient { |
| 130 | + private final Random random = new Random(); |
| 131 | + |
| 132 | + @Override |
| 133 | + public ServiceInstance choose(String serviceId) { |
| 134 | + return new DefaultServiceInstance(serviceId, serviceId, |
| 135 | + this.random.nextInt(40000), false); |
| 136 | + } |
| 137 | + |
| 138 | + @Override |
| 139 | + @SneakyThrows |
| 140 | + public <T> T execute(String serviceId, LoadBalancerRequest<T> request) { |
| 141 | + return request.apply(choose(serviceId)); |
| 142 | + } |
| 143 | + |
| 144 | + @Override |
| 145 | + public URI reconstructURI(ServiceInstance instance, URI original) { |
| 146 | + return DefaultServiceInstance.getUri(instance); |
| 147 | + } |
| 148 | + } |
133 | 149 | }
|
0 commit comments