Skip to content

Commit

Permalink
[type:test] Improve the unit test coverage. (apache#3831)
Browse files Browse the repository at this point in the history
* [type:test] Improve the unit test coverage of shenyu-loadbalancer.

* [type:test] Improve the unit test coverage of shenyu-web.

* [type:test] Improve the unit test coverage of shenyu-web.

* [type:test] Improve the unit test coverage of shenyu-web.

* [type:test] Improve the unit test coverage of shenyu-web.

* [type:test] Improve the unit test coverage of shenyu-web.

* [type:test] Improve the unit test coverage of shenyu-web.

* [type:test] Improve the unit test coverage of shenyu-web.

* [type:test] Improve the unit test coverage of shenyu-web.

* [type:test] Improve the unit test coverage of shenyu-loadbalancer.

* [type:test] Improve the unit test coverage of shenyu-loadbalancer.

* [type:test] Improve the unit test coverage of shenyu-loadbalancer.

* [type:test] Improve the unit test coverage of shenyu-loadbalancer.

* [type:test] Improve the unit test coverage of shenyu-loadbalancer.

* [type:test] Improve the unit test coverage of shenyu-loadbalancer.

* [type:test] Improve the unit test coverage of shenyu-loadbalancer.

* [type:test] Improve the unit test coverage of shenyu-loadbalancer.

* [type:test] Improve the unit test coverage of shenyu-loadbalancer.

* [type:test] Improve the unit test coverage of shenyu-loadbalancer.

* [type:test] Improve the unit test coverage of shenyu-loadbalancer.

* [type:test] Improve the unit test coverage of shenyu-loadbalancer.

* [type:test] Improve the unit test coverage of shenyu-loadbalancer.

* [type:test] Improve the unit test coverage of shenyu-loadbalancer.

* [type:test] Improve the unit test coverage of shenyu-web.

* [type:test] Improve the unit test coverage of shenyu-web.

* [type:test] Improve the unit test coverage of shenyu-loadbalancer.

* [type:test] Improve the unit test coverage of shenyu-loadbalancer.

* [type:test] Improve the unit test coverage of shenyu-loadbalancer.

* [type:test] Improve the unit test coverage of shenyu-loadbalancer.

* [type:test] Improve the unit test coverage of shenyu-loadbalancer.
  • Loading branch information
yunlongn authored Aug 29, 2022
1 parent 053c459 commit a13a935
Show file tree
Hide file tree
Showing 23 changed files with 779 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ public final class TarsServiceBeanPostProcessorTest {

private final TarsDemoService2 tarsDemoService2 = new TarsDemoService2();

private final TarsDemoService3 tarsDemoService3 = new TarsDemoService3();

@Mock
private ApplicationContext applicationContext;

Expand All @@ -71,6 +73,7 @@ public void init() {
Map<String, Object> results = new LinkedHashMap();
results.put("tarsDemoService", tarsDemoService);
results.put("tarsDemoService2", tarsDemoService2);
results.put("tarsDemoService3", tarsDemoService3);
when(applicationContext.getBeansWithAnnotation(any())).thenReturn(results);
contextRefreshedEvent = new ContextRefreshedEvent(applicationContext);

Expand Down Expand Up @@ -136,4 +139,12 @@ public String test(final String hello) {
return hello + "";
}
}

@ShenyuTarsService(serviceName = "testObj3")
@ShenyuTarsClient
static class TarsDemoService3 {
public String test(final String hello) {
return hello + "";
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@
/**
* The type Load balance Factory.
*/
public class LoadBalancerFactory {
public final class LoadBalancerFactory {

private LoadBalancerFactory() {
}

/**
* Selector upstream.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* 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.shenyu.loadbalancer.cache;

import org.apache.shenyu.common.config.ShenyuConfig;
import org.apache.shenyu.common.utils.Singleton;
import org.apache.shenyu.loadbalancer.entity.Upstream;
import org.junit.jupiter.api.TestMethodOrder;
import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Assertions;

import java.util.ArrayList;
import java.util.List;


/**
* The type UpstreamCacheManager check task test.
*/
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class UpstreamCacheManagerTest {

private static final String SELECTOR_ID = "SELECTOR_ID";

@Test
@Order(1)
public void initUpstreamCacheManagerTest() throws InterruptedException {
final ShenyuConfig shenyuConfig = new ShenyuConfig();
shenyuConfig.getUpstreamCheck().setEnabled(true);
shenyuConfig.getUpstreamCheck().setPrintEnabled(true);
shenyuConfig.getUpstreamCheck().setPrintInterval(1);
Singleton.INST.single(ShenyuConfig.class, shenyuConfig);
Assertions.assertNotNull(UpstreamCacheManager.getInstance());
Thread.sleep(3);
}

@Test
@Order(2)
public void submitTest() {
final UpstreamCacheManager upstreamCacheManager = UpstreamCacheManager.getInstance();
List<Upstream> upstreamList = new ArrayList<>(2);
upstreamCacheManager.submit(SELECTOR_ID, upstreamList);
upstreamList.add(Upstream.builder().url("url").status(true).build());
upstreamList.add(Upstream.builder().status(true).build());
upstreamCacheManager.submit(SELECTOR_ID, upstreamList);
// hit `existUpstream.stream().filter`
upstreamList.clear();
upstreamList.add(Upstream.builder().url("url2").status(true).build());
upstreamList.add(Upstream.builder().url("url").status(true).build());
upstreamCacheManager.submit(SELECTOR_ID, upstreamList);
}

@Test
@Order(3)
public void removeByKeyTest() {
final UpstreamCacheManager upstreamCacheManager = UpstreamCacheManager.getInstance();
upstreamCacheManager.removeByKey(SELECTOR_ID);
}

@Test
@Order(4)
public void findUpstreamListBySelectorIdTest() {
final UpstreamCacheManager upstreamCacheManager = UpstreamCacheManager.getInstance();
Assertions.assertNull(upstreamCacheManager.findUpstreamListBySelectorId(SELECTOR_ID));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ public void testRun() {
healthCheckTask.run();
Awaitility.await().pollDelay(1, TimeUnit.SECONDS).untilAsserted(() -> assertFalse(healthCheckTask.getCheckStarted().get()));
assertTrue(healthCheckTask.getHealthyUpstream().get(selectorId1).size() > 0);
healthCheckTask.print();
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* 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.shenyu.loadbalancer.cache;

import org.apache.shenyu.loadbalancer.entity.Upstream;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

/**
* The type UpstreamWithSelectorId check task test.
*/
public class UpstreamWithSelectorIdTest {

private static final String SELECTOR_ID = "selectorId";

@Test
public void initUpstreamCacheManagerTest() {
final UpstreamWithSelectorId upstream = new UpstreamWithSelectorId(SELECTOR_ID, null);
Assertions.assertEquals(upstream, upstream);
final UpstreamWithSelectorId upstream2 = new UpstreamWithSelectorId(SELECTOR_ID, null);
Assertions.assertEquals(upstream, upstream2);
upstream.setUpstream(null);
upstream.setSelectorId(null);
Assertions.assertTrue(upstream.hashCode() >= 0);
Assertions.assertNotNull(upstream.toString());
Assertions.assertNotEquals(upstream, upstream2);
Assertions.assertNotEquals(upstream, null);
Assertions.assertNotEquals(upstream, SELECTOR_ID);
final UpstreamWithSelectorId upstream3 = new UpstreamWithSelectorId(SELECTOR_ID, Upstream.builder().build());
Assertions.assertNotEquals(upstream, upstream3);
final UpstreamWithSelectorId upstream4 = new UpstreamWithSelectorId(null, Upstream.builder().build());
Assertions.assertNotEquals(upstream, upstream4);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
* 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.shenyu.loadbalancer.entity;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

/**
* The type Upstream check task test.
*/
public class UpstreamTest {

@Test
public void upstreamTest() {
Upstream upstream = Upstream.builder()
.group("group")
.url("url")
.timestamp(1)
.warmup(1)
.version("version")
.weight(1)
.status(true)
.build();
upstream.setGroup("group");
upstream.setHealthy(true);
upstream.setUrl("url");
upstream.setLastHealthTimestamp(1L);
upstream.setStatus(true);
upstream.setLastUnhealthyTimestamp(1L);
upstream.setVersion("version");
Assertions.assertEquals(upstream.buildDomain(), "http://url");
Assertions.assertEquals(upstream.getGroup(), "group");
Assertions.assertNull(upstream.getProtocol());
Assertions.assertEquals(upstream.getLastHealthTimestamp(), 1L);
Assertions.assertEquals(upstream.getLastUnhealthyTimestamp(), 1L);
Assertions.assertEquals(upstream.getUrl(), "url");
Assertions.assertEquals(upstream.getWarmup(), 1);
Assertions.assertEquals(upstream.getWeight(), 1);
Assertions.assertEquals(upstream.getVersion(), "version");
Assertions.assertEquals(upstream.getTimestamp(), 1);
Assertions.assertTrue(upstream.isHealthy());
Assertions.assertTrue(upstream.isStatus());
Upstream upstream2 = Upstream.builder()
.protocol("https://")
.url("url")
.weight(1)
.status(true)
.build();
Assertions.assertEquals(upstream2.buildDomain(), "https://url");
Assertions.assertNotEquals(upstream, upstream2);
Assertions.assertNotEquals(upstream, null);
Assertions.assertNotEquals(upstream, "");
Assertions.assertEquals(upstream, upstream);
Upstream upstream3 = Upstream.builder()
.protocol("https://")
.url("url")
.weight(1)
.status(true)
.build();
Assertions.assertEquals(upstream2, upstream3);
Assertions.assertNotNull(upstream2.toString());
Assertions.assertTrue(upstream2.hashCode() >= 0);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,24 @@
package org.apache.shenyu.loadbalancer.spi;

import org.apache.shenyu.loadbalancer.entity.Upstream;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.MockedStatic;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.List;
import java.util.SortedMap;
import java.util.concurrent.ConcurrentSkipListMap;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.mockStatic;

/**
* The type Hash balance test.
Expand All @@ -37,6 +44,8 @@ public final class HashLoadBalanceTest {

private Method hash;

private List<Upstream> onlyOneList;

private List<Upstream> hashLoadBalancesOrdered;

private List<Upstream> hashLoadBalancesDisordered;
Expand All @@ -53,6 +62,12 @@ public final class HashLoadBalanceTest {
public void setUp() throws Exception {
this.hash = HashLoadBalancer.class.getDeclaredMethod("hash", String.class);
this.hash.setAccessible(true);
this.onlyOneList = Stream.of(1)
.map(weight -> Upstream.builder()
.url("upstream-" + weight)
.status(false)
.build())
.collect(Collectors.toList());
this.hashLoadBalancesOrdered = Stream.of(1, 2, 3)
.map(weight -> Upstream.builder()
.url("upstream-" + weight)
Expand Down Expand Up @@ -94,19 +109,77 @@ public void setUp() throws Exception {
}
}

/**
* Hash load balance test.
*/
@Test
public void onlyOneListTest() {
final String ip = "127.0.0.1";
final HashLoadBalancer hashLoadBalance = new HashLoadBalancer();
Assertions.assertNotNull(hashLoadBalance.select(onlyOneList, ip));
Assertions.assertEquals(hashLoadBalance.getWeight(Upstream.builder().status(false).build()), 0);
Assertions.assertEquals(hashLoadBalance.getWeight(Upstream.builder()
.timestamp(System.currentTimeMillis() - 3L)
.warmup(4)
.weight(1)
.build()), 1);
Assertions.assertEquals(hashLoadBalance.getWeight(Upstream.builder()
.timestamp(System.currentTimeMillis() + 3L)
.warmup(4)
.weight(1)
.build()), 1);
Assertions.assertEquals(hashLoadBalance.getWeight(Upstream.builder()
.timestamp(System.currentTimeMillis() - 3L)
.warmup(4)
.weight(4)
.build()), 3);
Assertions.assertEquals(hashLoadBalance.getWeight(Upstream.builder()
.timestamp(System.currentTimeMillis() - 3L)
.warmup(1)
.weight(1)
.build()), 1);
Assertions.assertEquals(hashLoadBalance.getWeight(Upstream.builder()
.warmup(1)
.weight(0)
.build()), 0);
Assertions.assertEquals(hashLoadBalance.getWeight(Upstream.builder()
.warmup(0)
.weight(1)
.build()), 1);

}

/**
* Hash load balance test.
*/
@Test
public void hashLoadBalanceOrderedWeightTest() throws Exception {
final String ip = "127.0.0.1";
final HashLoadBalancer hashLoadBalance = new HashLoadBalancer();
Assertions.assertNull(hashLoadBalance.select(null, ip));
final Upstream upstream = hashLoadBalance.select(hashLoadBalancesOrdered, ip);
final Long hashKey = Long.parseLong(hash.invoke(null, ip).toString());
final SortedMap<Long, Upstream> lastRing = treeMapOrdered.tailMap(hashKey);
final Upstream assertUp = lastRing.get(lastRing.firstKey());
assertEquals(assertUp.getUrl(), upstream.getUrl());
}

@Test
public void selectTest() {
final String ip = "SHENYU-upstream-2-HASH-100";
final HashLoadBalancer hashLoadBalance = new HashLoadBalancer();
Assertions.assertNull(hashLoadBalance.select(null, ip));
final Upstream upstream = hashLoadBalance.select(hashLoadBalancesOrdered, ip);
assertEquals(treeMapOrdered.firstEntry().getValue().getUrl(), upstream.getUrl());
}

@Test
public void hashErrorTest() throws NoSuchAlgorithmException {
final String ip = "127.0.0.1";
MockedStatic<MessageDigest> messageDigestMockedStatic = mockStatic(MessageDigest.class);
messageDigestMockedStatic.when((MockedStatic.Verification) MessageDigest.getInstance("MD5")).thenThrow(NoSuchAlgorithmException.class);
assertThrows(InvocationTargetException.class, () -> hash.invoke(null, ip));
messageDigestMockedStatic.close();
}

@Test
Expand Down
Loading

0 comments on commit a13a935

Please sign in to comment.