Commit 9c6a3b20 authored by Juan Batiz-Benet's avatar Juan Batiz-Benet Committed by Brian Tiger Chow

peerstore test

parent c08b8958
......@@ -12,9 +12,9 @@ import (
// Peerstore provides a threadsafe collection for peers.
type Peerstore interface {
Get(ID) (*Peer, error)
Add(*Peer) error
Remove(ID) error
All() (*map[u.Key]*Peer, error)
Put(*Peer) error
Delete(ID) error
All() (*Map, error)
}
type peerstore struct {
......@@ -33,7 +33,8 @@ func (p *peerstore) Get(i ID) (*Peer, error) {
p.RLock()
defer p.RUnlock()
val, err := p.peers.Get(ds.NewKey(string(i)))
k := ds.NewKey(string(i))
val, err := p.peers.Get(k)
if err != nil {
return nil, err
}
......@@ -45,7 +46,7 @@ func (p *peerstore) Get(i ID) (*Peer, error) {
return peer, nil
}
func (p *peerstore) Add(peer *Peer) error {
func (p *peerstore) Put(peer *Peer) error {
p.Lock()
defer p.Unlock()
......@@ -53,7 +54,7 @@ func (p *peerstore) Add(peer *Peer) error {
return p.peers.Put(k, peer)
}
func (p *peerstore) Remove(i ID) error {
func (p *peerstore) Delete(i ID) error {
p.Lock()
defer p.Unlock()
......@@ -61,7 +62,7 @@ func (p *peerstore) Remove(i ID) error {
return p.peers.Delete(k)
}
func (p *peerstore) All() (*map[u.Key]*Peer, error) {
func (p *peerstore) All() (*Map, error) {
p.RLock()
defer p.RUnlock()
......@@ -70,7 +71,7 @@ func (p *peerstore) All() (*map[u.Key]*Peer, error) {
return nil, err
}
ps := &map[u.Key]*Peer{}
ps := &Map{}
for _, k := range l {
val, err := p.peers.Get(k)
if err != nil {
......
package peer
import (
"errors"
"testing"
ma "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr"
)
func setupPeer(id string, addr string) (*Peer, error) {
tcp, err := ma.NewMultiaddr(addr)
if err != nil {
return nil, err
}
p := &Peer{ID: ID(id)}
p.AddAddress(tcp)
return p, nil
}
func TestPeerstore(t *testing.T) {
ps := NewPeerstore()
p11, _ := setupPeer("11140beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a31", "/ip4/127.0.0.1/tcp/1234")
p21, _ := setupPeer("11140beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a32", "/ip4/127.0.0.1/tcp/2345")
// p31, _ := setupPeer("11140beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33", "/ip4/127.0.0.1/tcp/3456")
// p41, _ := setupPeer("11140beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a34", "/ip4/127.0.0.1/tcp/4567")
err := ps.Put(p11)
if err != nil {
t.Error(err)
}
p12, err := ps.Get(ID("11140beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a31"))
if err != nil {
t.Error(err)
}
if p11 != p12 {
t.Error(errors.New("peers should be the same"))
}
err = ps.Put(p21)
if err != nil {
t.Error(err)
}
p22, err := ps.Get(ID("11140beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a32"))
if err != nil {
t.Error(err)
}
if p21 != p22 {
t.Error(errors.New("peers should be the same"))
}
_, err = ps.Get(ID("11140beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33"))
if err == nil {
t.Error(errors.New("should've been an error here"))
}
err = ps.Delete(ID("11140beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a31"))
if err != nil {
t.Error(err)
}
_, err = ps.Get(ID("11140beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a31"))
if err == nil {
t.Error(errors.New("should've been an error here"))
}
p22, err = ps.Get(ID("11140beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a32"))
if err != nil {
t.Error(err)
}
if p21 != p22 {
t.Error(errors.New("peers should be the same"))
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment