1
0
mirror of https://github.com/ipfs/kubo.git synced 2025-08-06 03:19:47 +08:00

Peer: only add addresses once.

This commit is contained in:
Juan Batiz-Benet
2014-09-16 06:13:37 -07:00
committed by Brian Tiger Chow
parent 34a0580ea6
commit 71e411e538
2 changed files with 12 additions and 1 deletions

View File

@ -48,6 +48,11 @@ func (p *Peer) Key() u.Key {
// AddAddress adds the given Multiaddr address to Peer's addresses.
func (p *Peer) AddAddress(a *ma.Multiaddr) {
for _, addr := range p.Addresses {
if addr.Equal(a) {
return
}
}
p.Addresses = append(p.Addresses, a)
}

View File

@ -1,9 +1,10 @@
package peer
import (
"testing"
ma "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr"
mh "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multihash"
"testing"
)
func TestNetAddress(t *testing.T) {
@ -29,6 +30,11 @@ func TestNetAddress(t *testing.T) {
p := Peer{ID: ID(mh)}
p.AddAddress(tcp)
p.AddAddress(udp)
p.AddAddress(tcp)
if len(p.Addresses) == 3 {
t.Error("added same address twice")
}
tcp2 := p.NetAddress("tcp")
if tcp2 != tcp {