Move load balancer construction test
This commit is contained in:
@@ -0,0 +1,69 @@
|
||||
/*
|
||||
* Copyright 2013-2017 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
*/
|
||||
|
||||
package org.springframework.cloud.netflix.ribbon;
|
||||
|
||||
import com.netflix.loadbalancer.ILoadBalancer;
|
||||
import com.netflix.loadbalancer.PollingServerListUpdater;
|
||||
import com.netflix.loadbalancer.ServerListUpdater;
|
||||
import com.netflix.loadbalancer.ZoneAwareLoadBalancer;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.test.annotation.DirtiesContext;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
import org.springframework.test.util.ReflectionTestUtils;
|
||||
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.instanceOf;
|
||||
import static org.hamcrest.Matchers.is;
|
||||
import static org.hamcrest.Matchers.sameInstance;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
/**
|
||||
* @author Dave Syer
|
||||
*/
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(classes = RibbonClientConfigurationIntegrationTests.TestLBConfig.class,
|
||||
properties = "test.ribbon.ServerListRefreshInterval=999")
|
||||
@DirtiesContext
|
||||
public class RibbonClientConfigurationIntegrationTests {
|
||||
|
||||
@Autowired
|
||||
private SpringClientFactory clientFactory;
|
||||
|
||||
@Test
|
||||
public void testLoadBalancerConstruction() {
|
||||
ILoadBalancer loadBalancer = clientFactory.getInstance("test", ILoadBalancer.class);
|
||||
assertThat(loadBalancer, is(instanceOf(ZoneAwareLoadBalancer.class)));
|
||||
ZoneAwareLoadBalancer lb = (ZoneAwareLoadBalancer) loadBalancer;
|
||||
ServerListUpdater serverListUpdater = (PollingServerListUpdater) ReflectionTestUtils.getField(loadBalancer, "serverListUpdater");
|
||||
Long refreshIntervalMs = (Long) ReflectionTestUtils.getField(serverListUpdater, "refreshIntervalMs");
|
||||
assertThat(refreshIntervalMs, equalTo(999L));
|
||||
|
||||
ServerListUpdater updater = clientFactory.getInstance("test", ServerListUpdater.class);
|
||||
assertThat(updater, is(sameInstance(serverListUpdater)));
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@EnableAutoConfiguration
|
||||
protected static class TestLBConfig { }
|
||||
|
||||
}
|
||||
@@ -27,31 +27,21 @@ import org.mockito.MockitoAnnotations;
|
||||
import org.springframework.beans.factory.BeanFactoryUtils;
|
||||
import org.springframework.beans.factory.ListableBeanFactory;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.boot.builder.SpringApplicationBuilder;
|
||||
import org.springframework.boot.test.util.EnvironmentTestUtils;
|
||||
import org.springframework.cloud.netflix.ribbon.RibbonClientConfiguration.OverrideRestClient;
|
||||
import org.springframework.cloud.netflix.ribbon.apache.RibbonLoadBalancingHttpClient;
|
||||
import org.springframework.cloud.netflix.ribbon.okhttp.OkHttpLoadBalancingClient;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.test.util.ReflectionTestUtils;
|
||||
|
||||
import com.netflix.client.AbstractLoadBalancerAwareClient;
|
||||
import com.netflix.client.config.CommonClientConfigKey;
|
||||
import com.netflix.client.config.DefaultClientConfigImpl;
|
||||
import com.netflix.client.config.IClientConfig;
|
||||
import com.netflix.loadbalancer.ILoadBalancer;
|
||||
import com.netflix.loadbalancer.PollingServerListUpdater;
|
||||
import com.netflix.loadbalancer.Server;
|
||||
import com.netflix.loadbalancer.ServerListUpdater;
|
||||
import com.netflix.loadbalancer.ZoneAwareLoadBalancer;
|
||||
import com.netflix.niws.client.http.RestClient;
|
||||
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.instanceOf;
|
||||
import static org.hamcrest.Matchers.is;
|
||||
import static org.hamcrest.Matchers.sameInstance;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@@ -196,26 +186,6 @@ public class RibbonClientConfigurationTests {
|
||||
requiredType).length > 0;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLoadBalancerConstruction() {
|
||||
ConfigurableApplicationContext context = new SpringApplicationBuilder(TestLBConfig.class).properties(
|
||||
"test.ribbon.ServerListRefreshInterval=999")
|
||||
.run();
|
||||
|
||||
SpringClientFactory clientFactory = context.getBean(SpringClientFactory.class);
|
||||
ILoadBalancer loadBalancer = clientFactory.getInstance("test", ILoadBalancer.class);
|
||||
assertThat(loadBalancer, is(instanceOf(ZoneAwareLoadBalancer.class)));
|
||||
ZoneAwareLoadBalancer lb = (ZoneAwareLoadBalancer) loadBalancer;
|
||||
ServerListUpdater serverListUpdater = (PollingServerListUpdater) ReflectionTestUtils.getField(loadBalancer, "serverListUpdater");
|
||||
Long refreshIntervalMs = (Long) ReflectionTestUtils.getField(serverListUpdater, "refreshIntervalMs");
|
||||
assertThat(refreshIntervalMs, equalTo(999L));
|
||||
|
||||
ServerListUpdater updater = clientFactory.getInstance("test", ServerListUpdater.class);
|
||||
assertThat(updater, is(sameInstance(serverListUpdater)));
|
||||
|
||||
context.close();
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@EnableAutoConfiguration
|
||||
protected static class TestLBConfig { }
|
||||
|
||||
Reference in New Issue
Block a user