You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@skywalking.apache.org by GitBox <gi...@apache.org> on 2018/06/29 03:40:41 UTC

[GitHub] wu-sheng closed pull request #1407: Add Junit test for ribbon's NIWSServerListClassName #1390

wu-sheng closed pull request #1407: Add Junit test for ribbon's NIWSServerListClassName #1390
URL: https://github.com/apache/incubator-skywalking/pull/1407
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/apm-webapp/pom.xml b/apm-webapp/pom.xml
index f81c1fc7a..9e171c418 100644
--- a/apm-webapp/pom.xml
+++ b/apm-webapp/pom.xml
@@ -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>
diff --git a/apm-webapp/src/test/java/org/apache/skywalking/apm/webapp/proxy/NIWSServerListTest.java b/apm-webapp/src/test/java/org/apache/skywalking/apm/webapp/proxy/NIWSServerListTest.java
new file mode 100644
index 000000000..d96e32c22
--- /dev/null
+++ b/apm-webapp/src/test/java/org/apache/skywalking/apm/webapp/proxy/NIWSServerListTest.java
@@ -0,0 +1,76 @@
+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);
+    }
+}


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services