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

Peer: only add addresses once.

上级 34a0580e
...@@ -48,6 +48,11 @@ func (p *Peer) Key() u.Key { ...@@ -48,6 +48,11 @@ func (p *Peer) Key() u.Key {
// AddAddress adds the given Multiaddr address to Peer's addresses. // AddAddress adds the given Multiaddr address to Peer's addresses.
func (p *Peer) AddAddress(a *ma.Multiaddr) { func (p *Peer) AddAddress(a *ma.Multiaddr) {
for _, addr := range p.Addresses {
if addr.Equal(a) {
return
}
}
p.Addresses = append(p.Addresses, a) p.Addresses = append(p.Addresses, a)
} }
......
package peer package peer
import ( import (
"testing"
ma "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr" 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" mh "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multihash"
"testing"
) )
func TestNetAddress(t *testing.T) { func TestNetAddress(t *testing.T) {
...@@ -29,6 +30,11 @@ func TestNetAddress(t *testing.T) { ...@@ -29,6 +30,11 @@ func TestNetAddress(t *testing.T) {
p := Peer{ID: ID(mh)} p := Peer{ID: ID(mh)}
p.AddAddress(tcp) p.AddAddress(tcp)
p.AddAddress(udp) p.AddAddress(udp)
p.AddAddress(tcp)
if len(p.Addresses) == 3 {
t.Error("added same address twice")
}
tcp2 := p.NetAddress("tcp") tcp2 := p.NetAddress("tcp")
if tcp2 != tcp { if tcp2 != tcp {
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论