package peer;
import java.util.Iterator;
import util.Log;
import junit.framework.TestCase;
public class MinimizeLatencyStrategyTest extends TestCase {
PeerHash myMap = new PeerHash();
Peer peer1;
Peer peer2;
Peer peer3;
Peer peer4;
MinimizeLatencyStrategy minStrategy;
protected void setUp() throws Exception {
super.setUp();
peer1 = new Peer("peer1",100,0);
peer2 = new Peer("peer2",200,0);
peer3 = new Peer("peer3",50,50);
peer4 = new Peer("peer4",700,0);
myMap.set(peer1);
myMap.set(peer2);
myMap.set(peer3);
myMap.set(peer4);
minStrategy = new MinimizeLatencyStrategy();
}
public void testGetNearestPeers() {
myMap.get("peer3").setLatency(30);
PeerHash nearestPeers = minStrategy.getNearestPeers(myMap, new Log());
assertTrue(nearestPeers.getHashtable().size() == 1);
assertTrue(nearestPeers.getHashtable().containsKey("peer3"));
assertFalse(nearestPeers.getHashtable().containsKey("peer1"));
assertFalse(nearestPeers.getHashtable().containsKey("peer2"));
}
public void testTwoValuesTheSame() {
myMap.get("peer2").setLatency(30);
myMap.get("peer3").setLatency(30);
PeerHash nearestPeers = minStrategy.getNearestPeers(myMap,new Log());
assertTrue(nearestPeers.getHashtable().size() == 1);
assertTrue(nearestPeers.getHashtable().containsKey("peer2") || nearestPeers.getHashtable().containsKey("peer3"));
assertFalse(nearestPeers.getHashtable().containsKey("peer1"));
}
public void testThreeValuesTheSame() {
myMap.get("peer1").setLatency(30);
myMap.get("peer2").setLatency(30);
myMap.get("peer3").setLatency(30);
PeerHash nearestPeers = minStrategy.getNearestPeers(myMap,new Log());
assertTrue(nearestPeers.getHashtable().size() == 1);
assertFalse(nearestPeers.getHashtable().containsKey("peer4"));
Iterator it = nearestPeers.getHashtable().keySet().iterator();
while(it.hasNext()) {
String key = (String) it.next();
System.out.println("Key in map : " + key);
assertTrue(nearestPeers.get(key).getLatency() == 30);
}
}
}