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:
parent
1f93662b3e
commit
081384fb7f
|
@ -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)
|
||||
}
|
||||
|
|
8
util.go
8
util.go
|
@ -81,15 +81,23 @@ 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
|
||||
}
|
||||
_, ok := pm[pid]
|
||||
if !ok {
|
||||
pm[pid] = struct{}{}
|
||||
pids = append(pids, pid)
|
||||
}
|
||||
}
|
||||
return pids
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user