提交 71e411e5 作者: Juan Batiz-Benet 提交者: Brian Tiger Chow

Peer: only add addresses once.

上级 34a0580e
......@@ -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)
}
......
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 {
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论