cluster: Make peersFromMultiaddrs remove any duplicates.

Use it to find out the number of peers in the config and prevent
peerAdd test failures.

License: MIT
Signed-off-by: Hector Sanjuan <code@hector.link>
This commit is contained in:
Hector Sanjuan 2017-11-15 18:55:55 +01:00
parent 1f93662b3e
commit 081384fb7f
2 changed files with 10 additions and 2 deletions

View File

@ -91,7 +91,7 @@ func TestClustersPeerAdd(t *testing.T) {
// check that they are part of the configuration
// This only works because each peer only has one multiaddress
// (localhost)
if len(c.config.Peers) != nClusters-1 {
if len(peersFromMultiaddrs(c.config.Peers)) != nClusters-1 {
t.Error(c.config.Peers)
t.Errorf("%s: expected different cluster peers in the configuration", c.id)
}

10
util.go
View File

@ -81,14 +81,22 @@ func multiaddrJoin(addr ma.Multiaddr, p peer.ID) ma.Multiaddr {
return addr.Encapsulate(pidAddr)
}
// returns all the different peers in the given addresses.
// each peer only will appear once in the result, even if several
// multiaddresses for it are provided.
func peersFromMultiaddrs(addrs []ma.Multiaddr) []peer.ID {
var pids []peer.ID
pm := make(map[peer.ID]struct{})
for _, addr := range addrs {
pid, _, err := multiaddrSplit(addr)
if err != nil {
continue
}
pids = append(pids, pid)
_, ok := pm[pid]
if !ok {
pm[pid] = struct{}{}
pids = append(pids, pid)
}
}
return pids
}