提交 8eb2bef1 编写于 作者: 歪脖大肚子Q 提交者: Gao Hongtao

Add Junit test for ribbon's NIWSServerListClassName #1390 (#1407)

* Add Junit test for ribbon's NIWSServerListClassName

* Add License Head to Code file

* try to solve submodule skywalking-ui conflict
上级 02ab74ad
......@@ -88,6 +88,12 @@
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>${spring.boot.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
......
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.apache.skywalking.apm.webapp.proxy;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.core.env.AbstractEnvironment;
import org.springframework.core.env.Environment;
import org.springframework.core.env.MapPropertySource;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.netflix.loadbalancer.Server;
import com.netflix.loadbalancer.ServerList;
import com.netflix.loadbalancer.ZoneAwareLoadBalancer;
import org.springframework.cloud.netflix.ribbon.SpringClientFactory;
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
@DirtiesContext
public class NIWSServerListTest {
@Autowired
private Environment env;
@Autowired
private SpringClientFactory factory;
private Map<String, String> serverListClassNames = new HashMap<String, String>();
@Before
public void initServerListClassNames() {
for (Iterator<?> iter = ((AbstractEnvironment) env).getPropertySources().iterator(); iter.hasNext();) {
Object propertySource = iter.next();
if (propertySource instanceof MapPropertySource) {
Map<String, Object> mapPropertySource = ((MapPropertySource) propertySource).getSource();
for (Map.Entry<String, Object> entry : mapPropertySource.entrySet()) {
String key = entry.getKey();
int index;
if (key.endsWith(".NIWSServerListClassName") &&
(index = key.indexOf(".ribbon")) > 0) {
String clientName = key.substring(0, index);
serverListClassNames.put(clientName,(String)entry.getValue());
}
}
}
}
}
@Test
public void serverListClass() throws ClassNotFoundException {
for (String serverListClassName : serverListClassNames.values()) {
Class<?> clazz = Class.forName(serverListClassName);
}
}
@Test
public void serverListFliter() {
for (Map.Entry<String, String> entry : serverListClassNames.entrySet()) {
String clientName = entry.getKey();
String serverListClassName = entry.getValue();
ServerList<Server> serverList = getLoadBalancer(clientName).getServerListImpl();
assertNotNull("Client: " + clientName + "'s ServerListImpl is null",serverList);
assertEquals("Clinet: " + clientName + "'s ServerListImpl not Same with setting in configs",
serverListClassName, serverList.getClass().getName());
}
}
@SuppressWarnings("unchecked")
private ZoneAwareLoadBalancer<Server> getLoadBalancer(String name) {
return (ZoneAwareLoadBalancer<Server>)this.factory.getLoadBalancer(name);
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册