Fixed problem with the comparation function between two FullNode instances

This commit is contained in:
Nelson R. Perez 2018-10-23 15:30:37 -05:00
parent b70881dfb5
commit 878bc52d78
4 changed files with 95 additions and 12 deletions

View file

@ -55,14 +55,6 @@ public class FullNode implements Comparable {
this.mUrl = mUrl;
}
/**
*
* @return The exponential moving average object instance
*/
public ExponentialMovingAverage getLatencyAverage(){
return mLatency;
}
/**
*
* @return The latest latency average value. If no measurement has been taken yet, the
@ -96,7 +88,13 @@ public class FullNode implements Comparable {
@Override
public int compareTo(Object o) {
FullNode node = (FullNode) o;
return (int) Math.ceil(mLatency.getAverage() - node.getLatencyValue());
double doubleResult = getLatencyValue() - node.getLatencyValue();
if(doubleResult > 0)
return 1;
else if(doubleResult < 0)
return -1;
else
return 0;
}
@Override

View file

@ -24,7 +24,8 @@ public class LatencyNodeProvider implements NodeProvider {
@Override
public boolean updateNode(FullNode fullNode) {
mFullNodeHeap.remove(fullNode);
boolean removed = mFullNodeHeap.remove(fullNode);
System.out.println("Removed: "+removed);
return mFullNodeHeap.offer(fullNode);
}

View file

@ -21,8 +21,7 @@ public class FullNodeTest {
@Test
public void testFullNodeAverageLatency(){
FullNode fullNode = new FullNode("wss://dummy");
fullNode.getLatencyAverage().setAlpha(0.5);
FullNode fullNode = new FullNode("wss://dummy", 0.5);
fullNode.addLatencyValue(100);
Assert.assertEquals(100.0, fullNode.getLatencyValue());
fullNode.addLatencyValue(50);

View file

@ -91,4 +91,89 @@ public class LatencyNodeProviderTest {
FullNode best = latencyNodeProvider.getBestNode();
Assert.assertSame(nodeC, best);
}
@Test
public void realisticSituationTest(){
FullNode node1 = new FullNode("wss://fi.bts.dcn.cx/ws");
FullNode node2 = new FullNode("wss://mx.palmpay.io/ws");
FullNode node3 = new FullNode("wss://miami.bitshares.apasia.tech/ws");
FullNode node4 = new FullNode("wss://valley.bitshares.apasia.tech/ws");
FullNode node5 = new FullNode("wss://atlanta.bitshares.apasia.tech/ws");
FullNode node6 = new FullNode("wss://dallas.bitshares.apasia.tech/ws");
FullNode node7 = new FullNode("wss://eu-west-2.bts.crypto-bridge.org");
FullNode node8 = new FullNode("wss://england.bitshares.apasia.tech/ws");
FullNode node9 = new FullNode("wss://eu-west-1.bts.crypto-bridge.org");
FullNode node11 = new FullNode("wss://netherlands.bitshares.apasia.tech/ws");
FullNode node12 = new FullNode("wss://api.bts.blckchnd.com");
FullNode node13 = new FullNode("wss://bitshares.nu/ws");
FullNode node14 = new FullNode("wss://bitshares.openledger.info/ws");
FullNode node15 = new FullNode("wss://citadel.li/node");
FullNode node16 = new FullNode("wss://api-ru.bts.blckchnd.com");
FullNode node17 = new FullNode("wss://dex.rnglab.org");
FullNode node18 = new FullNode("wss://nl.palmpay.io/ws");
FullNode node19 = new FullNode("wss://bitshares.crypto.fans/ws");
FullNode node20 = new FullNode("wss://bit.btsabc.org/ws");
LatencyNodeProvider provider = new LatencyNodeProvider();
provider.addNode(node1);
provider.addNode(node2);
provider.addNode(node3);
provider.addNode(node4);
provider.addNode(node5);
provider.addNode(node6);
provider.addNode(node7);
provider.addNode(node8);
provider.addNode(node9);
provider.addNode(node11);
provider.addNode(node12);
provider.addNode(node13);
provider.addNode(node14);
provider.addNode(node15);
provider.addNode(node16);
provider.addNode(node17);
provider.addNode(node18);
provider.addNode(node19);
provider.addNode(node20);
node3.addLatencyValue(458.41);
node4.addLatencyValue(458.40);
node5.addLatencyValue(620.12);
node6.addLatencyValue(682.64);
node7.addLatencyValue(842.88);
node8.addLatencyValue(842.05);
node9.addLatencyValue(911.38);
node11.addLatencyValue(930.58);
node12.addLatencyValue(1002.27);
node13.addLatencyValue(1069.96);
node14.addLatencyValue(1060.20);
node15.addLatencyValue(1025.14);
node16.addLatencyValue(1060.55);
node17.addLatencyValue(1001.44);
node18.addLatencyValue(1036.69);
node19.addLatencyValue(1047.19);
node20.addLatencyValue(1286.89);
provider.updateNode(node1);
provider.updateNode(node2);
provider.updateNode(node3);
provider.updateNode(node4);
provider.updateNode(node5);
provider.updateNode(node6);
provider.updateNode(node7);
provider.updateNode(node8);
provider.updateNode(node9);
provider.updateNode(node11);
provider.updateNode(node12);
provider.updateNode(node13);
provider.updateNode(node14);
provider.updateNode(node15);
provider.updateNode(node16);
provider.updateNode(node17);
provider.updateNode(node18);
provider.updateNode(node19);
provider.updateNode(node20);
FullNode best = provider.getBestNode();
Assert.assertSame("Expects node4 to be the best", node4, best);
}
}