Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ISSUE #3887] The method of verifying no exception thrown is not appropriate #3889

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,8 @@ public T select() {
public LoadBalanceType getType() {
return LoadBalanceType.WEIGHT_RANDOM;
}

boolean isSameWeightGroup() {
return sameWeightGroup;
}
Comment on lines +77 to +80
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about using a @Getter lombok annotation?

Well, this method doesn't take up a lot of space and can be left unchanged in this PR if you don't feel it's necessary.

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

package org.apache.eventmesh.common.loadbalance;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand All @@ -35,25 +35,24 @@ public class RandomLoadBalanceSelectorTest {
private RandomLoadBalanceSelector<String> randomLoadBalanceSelector;

@Before
public void befor() {
List<String> address = new ArrayList<>();
address.add("A");
address.add("B");
address.add("C");
public void before() {
List<String> address = Arrays.asList("A", "B", "C");
randomLoadBalanceSelector = new RandomLoadBalanceSelector<>(address);
}


@Test
public void testSelect() {
Map<String, Integer> addressToNum = new HashMap<>();
for (int i = 0; i < 100; i++) {
String select = randomLoadBalanceSelector.select();
addressToNum.put(select, addressToNum.getOrDefault(select, 0) + 1);
try {
Map<String, Integer> addressToNum = new HashMap<>();
for (int i = 0; i < 100; i++) {
String select = randomLoadBalanceSelector.select();
addressToNum.put(select, addressToNum.getOrDefault(select, 0) + 1);
}
addressToNum.forEach((key, value) -> log.info("{} : {}", key, value));
} catch (Exception e) {
Assert.fail("Test failed");
}
addressToNum.forEach((key, value) -> log.info("{} : {}", key, value));
// just assert success if no exception
Assert.assertTrue(true);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@
import static java.util.stream.Collectors.groupingBy;
import static java.util.stream.Collectors.summingInt;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
Expand All @@ -38,10 +37,11 @@ public class WeightRandomLoadBalanceSelectorTest {

@Test
public void testSelect() throws Exception {
List<Weight<String>> weightList = new ArrayList<>();
weightList.add(new Weight<>("192.168.0.1", 10));
weightList.add(new Weight<>("192.168.0.2", 20));
weightList.add(new Weight<>("192.168.0.3", 40));
List<Weight<String>> weightList = Arrays.asList(
new Weight<>("192.168.0.1", 10),
new Weight<>("192.168.0.2", 20),
new Weight<>("192.168.0.3", 40)
);
WeightRandomLoadBalanceSelector<String> weightRandomLoadBalanceSelector = new WeightRandomLoadBalanceSelector<>(weightList);
Assert.assertEquals(LoadBalanceType.WEIGHT_RANDOM, weightRandomLoadBalanceSelector.getType());
int testRange = 100_000;
Expand All @@ -53,17 +53,22 @@ public void testSelect() throws Exception {
log.info("{}: {}", key, value);
});
log.info("addressToNum: {}", addressToNum);
// the error less than 5%
Assert.assertTrue(Math.abs(addressToNum.get("192.168.0.3") - addressToNum.get("192.168.0.2") * 2) < testRange / 20);
Assert.assertTrue(Math.abs(addressToNum.get("192.168.0.3") - addressToNum.get("192.168.0.1") * 4) < testRange / 20);
// the error rate <= 5%
Assert.assertEquals(1,
(double) addressToNum.get("192.168.0.2") / (addressToNum.get("192.168.0.1") * 2), 0.05);
Assert.assertEquals(1,
(double) addressToNum.get("192.168.0.3") / (addressToNum.get("192.168.0.2") * 2), 0.05);
Assert.assertEquals(1,
(double) addressToNum.get("192.168.0.3") / (addressToNum.get("192.168.0.1") * 4), 0.05);
}

@Test
public void testSameWeightSelect() throws Exception {
List<Weight<String>> weightList = new ArrayList<>();
weightList.add(new Weight<>("192.168.0.1", 10));
weightList.add(new Weight<>("192.168.0.2", 10));
weightList.add(new Weight<>("192.168.0.3", 10));
List<Weight<String>> weightList = Arrays.asList(
new Weight<>("192.168.0.1", 10),
new Weight<>("192.168.0.2", 10),
new Weight<>("192.168.0.3", 10)
);
WeightRandomLoadBalanceSelector<String> weightRandomLoadBalanceSelector = new WeightRandomLoadBalanceSelector<>(weightList);
Assert.assertEquals(LoadBalanceType.WEIGHT_RANDOM, weightRandomLoadBalanceSelector.getType());

Expand All @@ -72,15 +77,18 @@ public void testSameWeightSelect() throws Exception {
.mapToObj(i -> weightRandomLoadBalanceSelector.select())
.collect(groupingBy(Function.identity(), summingInt(i -> 1)));

Field field = WeightRandomLoadBalanceSelector.class.getDeclaredField("sameWeightGroup");
field.setAccessible(true);
boolean sameWeightGroup = (boolean) field.get(weightRandomLoadBalanceSelector);
boolean sameWeightGroup = weightRandomLoadBalanceSelector.isSameWeightGroup();
Assert.assertTrue(sameWeightGroup);

addressToNum.forEach((key, value) -> {
log.info("{}: {}", key, value);
});
// the error less than 5%
Assert.assertTrue(Math.abs(addressToNum.get("192.168.0.3") - addressToNum.get("192.168.0.2")) < testRange / 20);
// the error rate <= 5%
Assert.assertEquals(1,
(double) addressToNum.get("192.168.0.3") / addressToNum.get("192.168.0.2"), 0.05);
Assert.assertEquals(1,
(double) addressToNum.get("192.168.0.3") / addressToNum.get("192.168.0.1"), 0.05);
Assert.assertEquals(1,
(double) addressToNum.get("192.168.0.2") / addressToNum.get("192.168.0.1"), 0.05);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

package org.apache.eventmesh.common.loadbalance;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand All @@ -26,36 +26,40 @@
import org.junit.Before;
import org.junit.Test;


import lombok.extern.slf4j.Slf4j;



@Slf4j
public class WeightRoundRobinLoadBalanceSelectorTest {

private WeightRoundRobinLoadBalanceSelector<String> weightRoundRobinLoadBalanceSelector;

@Before
public void before() {
List<Weight<String>> weightList = new ArrayList<>();
weightList.add(new Weight<>("A", 10));
weightList.add(new Weight<>("B", 20));
weightList.add(new Weight<>("C", 30));
List<Weight<String>> weightList = Arrays.asList(
new Weight<>("A", 10),
new Weight<>("B", 20),
new Weight<>("C", 30)
);
weightRoundRobinLoadBalanceSelector = new WeightRoundRobinLoadBalanceSelector<>(weightList);
}

@Test
public void testSelect() {
Map<String, Integer> addressToNum = new HashMap<>();
Map<String, Integer> addressToNum = new HashMap<>(3);
for (int i = 0; i < 100_000; i++) {
String select = weightRoundRobinLoadBalanceSelector.select();
addressToNum.put(select, addressToNum.getOrDefault(select, 0) + 1);
}
addressToNum.forEach((key, value) -> {
log.info("{}: {}", key, value);
});
Assert.assertTrue(addressToNum.get("B") > addressToNum.get("A"));
Assert.assertTrue(addressToNum.get("C") > addressToNum.get("B"));
Assert.assertEquals(0.5, (double) addressToNum.get("A") / addressToNum.get("B"), 0.0001);
Assert.assertEquals(
(double) 1 / 3, (double) addressToNum.get("A") / addressToNum.get("C"), 0.0001);
Assert.assertEquals(
(double) 2 / 3, (double) addressToNum.get("B") / addressToNum.get("C"), 0.0001);
}

@Test
Expand Down