From a6e5910144fc7f2c36a96770e77b8e6d1ad90e32 Mon Sep 17 00:00:00 2001 From: "Nelson R. Perez" Date: Mon, 22 Oct 2018 13:53:47 -0500 Subject: [PATCH] Fixing issues with the LatencyNodeProvider#getSortedNodes() method --- .../network/LatencyNodeProvider.java | 6 +-- .../network/LatencyNodeProviderTest.java | 49 +++++++++++++++---- 2 files changed, 42 insertions(+), 13 deletions(-) diff --git a/graphenej/src/main/java/cy/agorise/graphenej/network/LatencyNodeProvider.java b/graphenej/src/main/java/cy/agorise/graphenej/network/LatencyNodeProvider.java index 643df1e..1e12939 100644 --- a/graphenej/src/main/java/cy/agorise/graphenej/network/LatencyNodeProvider.java +++ b/graphenej/src/main/java/cy/agorise/graphenej/network/LatencyNodeProvider.java @@ -1,13 +1,11 @@ package cy.agorise.graphenej.network; import java.util.ArrayList; -import java.util.Arrays; import java.util.Collections; import java.util.List; import java.util.PriorityQueue; public class LatencyNodeProvider implements NodeProvider { - private final String TAG = "LatencyNodeProvider"; private PriorityQueue mFullNodeHeap; public LatencyNodeProvider(){ @@ -50,12 +48,12 @@ public class LatencyNodeProvider implements NodeProvider { public List getSortedNodes() { FullNode[] nodeArray = mFullNodeHeap.toArray(new FullNode[mFullNodeHeap.size()]); ArrayList nodeList = new ArrayList<>(); - for(FullNode fullNode : nodeList){ + for(FullNode fullNode : nodeArray){ if(fullNode != null){ nodeList.add(fullNode); } } Collections.sort(nodeList); - return Arrays.asList(nodeArray); + return nodeList; } } diff --git a/graphenej/src/test/java/cy/agorise/graphenej/network/LatencyNodeProviderTest.java b/graphenej/src/test/java/cy/agorise/graphenej/network/LatencyNodeProviderTest.java index 23c94b1..156d714 100644 --- a/graphenej/src/test/java/cy/agorise/graphenej/network/LatencyNodeProviderTest.java +++ b/graphenej/src/test/java/cy/agorise/graphenej/network/LatencyNodeProviderTest.java @@ -29,12 +29,45 @@ public class LatencyNodeProviderTest { } @Test - public void testSorting(){ + public void testSortedList(){ + setupTestNodes(); + + // Confirming that the getSortedNodes gives us a sorted list of nodes in increasing latency order + List 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(); // Confirming that the best node is nodeC FullNode bestNode = latencyNodeProvider.getBestNode(); - System.out.println("Best node latency: "+bestNode.getLatencyValue()); Assert.assertSame("Check that the best node is nodeC", nodeC, bestNode); // Improving nodeA score by feeding it with new better latency measurements @@ -51,13 +84,11 @@ public class LatencyNodeProviderTest { } @Test - public void testSortedList(){ + public void testLargeNumbers(){ setupTestNodes(); - - // Confirming that the getSortedNodes gives us a sorted list of nodes in increasing latency order - List fullNodeList = latencyNodeProvider.getSortedNodes(); - Assert.assertSame(nodeC, fullNodeList.get(0)); - Assert.assertSame(nodeB, fullNodeList.get(1)); - Assert.assertSame(nodeA, fullNodeList.get(2)); + nodeA.addLatencyValue(Long.MAX_VALUE); + latencyNodeProvider.updateNode(nodeA); + FullNode best = latencyNodeProvider.getBestNode(); + Assert.assertSame(nodeC, best); } }