@Override public int getNoOfParties() { return this.conf.noOfParties(); } }
public NetworkConfigurationImpl(int myId, Map<Integer, Party> parties) { Objects.requireNonNull(parties); checkAddressesUnique(parties); this.myId = myId; this.parties = parties; }
@Test public void testMyIdIsCorrect() { assertEquals(1, confs.get(1).getMyId()); }
/** * As getConfigurations(n, ports) but tries to find free ephemeral ports (but note that there is * no guarantee that ports will remain unused). */ public static Map<Integer, NetworkConfiguration> getNetworkConfigurations(int n) { List<Integer> ports = getFreePorts(n); return getNetworkConfigurations(ports); } }
@Test(expected = IllegalArgumentException.class) public void testUniqueAddressCheckAllSameHostsAndPorts() { Map<Integer, Party> parties = new HashMap<>(3); for (int i = 1; i < numParties; i++) { Party p = new Party(i, "host", 3000); parties.put(i, p); this.me = (i == myId) ? p : me; } this.netConf = new NetworkConfigurationImpl(myId, parties); }
protected List<NetworkConfiguration> getNetConfs(int numParties) { Map<Integer, Party> parties = new HashMap<>(numParties); List<NetworkConfiguration> confs = new ArrayList<>(numParties); List<Integer> ports = NetworkUtil.getFreePorts(numParties); int id = 1; for (Integer port : ports) { parties.put(id, new Party(id, "localhost", port)); id++; } for (int i = 1; i <= numParties; i++) { confs.add(new NetworkConfigurationImpl(i, parties)); } return confs; }
private Map<Integer, NetworkConfiguration> getNetConf() { int noOfParties = 2; List<Integer> ports = new ArrayList<>(noOfParties); for (int i = 1; i <= noOfParties; i++) { ports.add(9000 + i * (noOfParties - 1)); } return NetworkUtil.getNetworkConfigurations(ports); } }
@Test public void testGetFreePortsGivesFreePorts() throws IOException { int numPorts = 100; List<Integer> ports = NetworkUtil.getFreePorts(numPorts); for (Integer port : ports) { new ServerSocket(port).close(); } } }
@Test public void testPortIsFree() throws IOException { int port = confs.get(1).getParty(1).getPort(); ServerSocket s = new ServerSocket(port); s.close(); }
@Test public void testGetMyId() { assertEquals(me.getPartyId(), netConf.getMyId()); }
@Test public void testUniqueAddressCheckSameHostsDifferentPorts() { // validates that sanity check uses both hosts and ports to check for uniqueness Map<Integer, Party> parties = new HashMap<>(numParties); for (int i = 1; i < numParties; i++) { Party p = new Party(i, "host", 3000 + i); parties.put(i, p); this.me = (i == myId) ? p : me; } this.netConf = new NetworkConfigurationImpl(myId, parties); }
@Test public void testNoOfParties() { assertEquals(numParties, netConf.noOfParties()); }
@Before public void setUp() { confs = NetworkUtil.getNetworkConfigurations(n); }
@Test public void testGetFreePortsGivesUniquePorts() { int numPorts = 100; int numIterations = 5; for (int i = 0; i < numIterations; i++) { Set<Integer> uniquePorts = new HashSet<>(NetworkUtil.getFreePorts(numPorts)); assertEquals(numPorts, uniquePorts.size()); } }
@Test public void testUniqueAddressCheckDifferentHostsSamePorts() { // validates that sanity check uses both hosts and ports to check for uniqueness Map<Integer, Party> parties = new HashMap<>(numParties); for (int i = 1; i < numParties; i++) { Party p = new Party(i, "host" + i, 3000); parties.put(i, p); this.me = (i == myId) ? p : me; } this.netConf = new NetworkConfigurationImpl(myId, parties); }
/** * Set up a configuration with 4 parties. */ @Before public void setUp() { Map<Integer, Party> parties = new HashMap<>(4); for (int i = 1; i < numParties + 1; i++) { Party p = new Party(i, "host" + i, 3000 + i); parties.put(i, p); this.me = (i == myId) ? p : me; } this.netConf = new NetworkConfigurationImpl(myId, parties); }
@Test(expected = IllegalArgumentException.class) public void testUniqueAddressCheckSameHostsAndPorts() { Map<Integer, Party> parties = new HashMap<>(3); for (int i = 1; i < numParties; i++) { Party p = new Party(i, "host", 3000 + i); parties.put(i, p); this.me = (i == myId) ? p : me; } parties.put(1, new Party(1, "host", 3002)); this.netConf = new NetworkConfigurationImpl(myId, parties); }