Fixing issues with the LatencyNodeProvider#getSortedNodes() method
This commit is contained in:
parent
8fb9bc57d6
commit
a6e5910144
2 changed files with 42 additions and 13 deletions
|
@ -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<FullNode> mFullNodeHeap;
|
||||
|
||||
public LatencyNodeProvider(){
|
||||
|
@ -50,12 +48,12 @@ public class LatencyNodeProvider implements NodeProvider {
|
|||
public List<FullNode> getSortedNodes() {
|
||||
FullNode[] nodeArray = mFullNodeHeap.toArray(new FullNode[mFullNodeHeap.size()]);
|
||||
ArrayList<FullNode> 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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<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();
|
||||
|
||||
// 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<FullNode> 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);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue