Fixing issues with the LatencyNodeProvider#getSortedNodes() method

This commit is contained in:
Nelson R. Perez 2018-10-22 13:53:47 -05:00
parent 8fb9bc57d6
commit a6e5910144
2 changed files with 42 additions and 13 deletions

View file

@ -1,13 +1,11 @@
package cy.agorise.graphenej.network; package cy.agorise.graphenej.network;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.PriorityQueue; import java.util.PriorityQueue;
public class LatencyNodeProvider implements NodeProvider { public class LatencyNodeProvider implements NodeProvider {
private final String TAG = "LatencyNodeProvider";
private PriorityQueue<FullNode> mFullNodeHeap; private PriorityQueue<FullNode> mFullNodeHeap;
public LatencyNodeProvider(){ public LatencyNodeProvider(){
@ -50,12 +48,12 @@ public class LatencyNodeProvider implements NodeProvider {
public List<FullNode> getSortedNodes() { public List<FullNode> getSortedNodes() {
FullNode[] nodeArray = mFullNodeHeap.toArray(new FullNode[mFullNodeHeap.size()]); FullNode[] nodeArray = mFullNodeHeap.toArray(new FullNode[mFullNodeHeap.size()]);
ArrayList<FullNode> nodeList = new ArrayList<>(); ArrayList<FullNode> nodeList = new ArrayList<>();
for(FullNode fullNode : nodeList){ for(FullNode fullNode : nodeArray){
if(fullNode != null){ if(fullNode != null){
nodeList.add(fullNode); nodeList.add(fullNode);
} }
} }
Collections.sort(nodeList); Collections.sort(nodeList);
return Arrays.asList(nodeArray); return nodeList;
} }
} }

View file

@ -29,12 +29,45 @@ public class LatencyNodeProviderTest {
} }
@Test @Test
public void testSorting(){ public void testSortedList(){
setupTestNodes();
// Confirming that the getSortedNodes gives us a sorted list of nodes in increasing latency order
List<FullNode> fullNodeList = latencyNodeProvider.getSortedNodes();
Assert.assertSame(nodeC, fullNodeList.get(0));
Assert.assertSame(nodeB, fullNodeList.get(1));
Assert.assertSame(nodeA, fullNodeList.get(2));
// Adding more nodes with different latencies measurements
FullNode nodeD = new FullNode("wss://nodeD");
FullNode nodeE = new FullNode("wss://nodeE");
FullNode nodeF = new FullNode("wss://nodef");
// Adding latencies measurements
nodeD.addLatencyValue(900);
nodeE.addLatencyValue(1);
nodeF.addLatencyValue(1500);
// Updating the LatencyNodeProvider
latencyNodeProvider.updateNode(nodeD);
latencyNodeProvider.updateNode(nodeE);
latencyNodeProvider.updateNode(nodeF);
FullNode bestNode = latencyNodeProvider.getBestNode();
// Checking for best node
Assert.assertSame("Verifying that the nodeE is the best now", nodeE, bestNode);
fullNodeList = latencyNodeProvider.getSortedNodes();
FullNode worstNode = fullNodeList.get(fullNodeList.size() - 1);
// Checking for worst node
Assert.assertSame("Verifying that the nodeF is the worst now", nodeF, worstNode);
}
@Test
public void testScoreUpdate(){
setupTestNodes(); setupTestNodes();
// Confirming that the best node is nodeC // Confirming that the best node is nodeC
FullNode bestNode = latencyNodeProvider.getBestNode(); FullNode bestNode = latencyNodeProvider.getBestNode();
System.out.println("Best node latency: "+bestNode.getLatencyValue());
Assert.assertSame("Check that the best node is nodeC", nodeC, bestNode); Assert.assertSame("Check that the best node is nodeC", nodeC, bestNode);
// Improving nodeA score by feeding it with new better latency measurements // Improving nodeA score by feeding it with new better latency measurements
@ -51,13 +84,11 @@ public class LatencyNodeProviderTest {
} }
@Test @Test
public void testSortedList(){ public void testLargeNumbers(){
setupTestNodes(); setupTestNodes();
nodeA.addLatencyValue(Long.MAX_VALUE);
// Confirming that the getSortedNodes gives us a sorted list of nodes in increasing latency order latencyNodeProvider.updateNode(nodeA);
List<FullNode> fullNodeList = latencyNodeProvider.getSortedNodes(); FullNode best = latencyNodeProvider.getBestNode();
Assert.assertSame(nodeC, fullNodeList.get(0)); Assert.assertSame(nodeC, best);
Assert.assertSame(nodeB, fullNodeList.get(1));
Assert.assertSame(nodeA, fullNodeList.get(2));
} }
} }