Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
10
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
dms3
go-dms3
Commits
9c6a3b20
Commit
9c6a3b20
authored
Sep 16, 2014
by
Juan Batiz-Benet
Committed by
Brian Tiger Chow
Sep 22, 2014
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
peerstore test
parent
c08b8958
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
91 additions
and
8 deletions
+91
-8
peer/peerstore.go
peer/peerstore.go
+9
-8
peer/peerstore_test.go
peer/peerstore_test.go
+82
-0
No files found.
peer/peerstore.go
View file @
9c6a3b20
...
...
@@ -12,9 +12,9 @@ import (
// Peerstore provides a threadsafe collection for peers.
type
Peerstore
interface
{
Get
(
ID
)
(
*
Peer
,
error
)
Add
(
*
Peer
)
error
Remov
e
(
ID
)
error
All
()
(
*
m
ap
[
u
.
Key
]
*
Peer
,
error
)
Put
(
*
Peer
)
error
Delet
e
(
ID
)
error
All
()
(
*
M
ap
,
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
)
Remov
e
(
i
ID
)
error
{
func
(
p
*
peerstore
)
Delet
e
(
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
()
(
*
m
ap
[
u
.
Key
]
*
Peer
,
error
)
{
func
(
p
*
peerstore
)
All
()
(
*
M
ap
,
error
)
{
p
.
RLock
()
defer
p
.
RUnlock
()
...
...
@@ -70,7 +71,7 @@ func (p *peerstore) All() (*map[u.Key]*Peer, error) {
return
nil
,
err
}
ps
:=
&
m
ap
[
u
.
Key
]
*
Peer
{}
ps
:=
&
M
ap
{}
for
_
,
k
:=
range
l
{
val
,
err
:=
p
.
peers
.
Get
(
k
)
if
err
!=
nil
{
...
...
peer/peerstore_test.go
0 → 100644
View file @
9c6a3b20
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"
))
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment