mirror of
https://github.com/libp2p/go-libp2p-peerstore.git
synced 2026-08-22 15:33:27 +08:00
improve test coverage
This commit is contained in:
57
peerinfo_test.go
Normal file
57
peerinfo_test.go
Normal file
@@ -0,0 +1,57 @@
|
||||
package peerstore
|
||||
|
||||
import (
|
||||
peer "github.com/ipfs/go-libp2p-peer"
|
||||
ma "github.com/jbenet/go-multiaddr"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func mustAddr(t *testing.T, s string) ma.Multiaddr {
|
||||
addr, err := ma.NewMultiaddr(s)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
return addr
|
||||
}
|
||||
|
||||
func TestPeerInfoMarshal(t *testing.T) {
|
||||
a := mustAddr(t, "/ip4/1.2.3.4/tcp/4536")
|
||||
b := mustAddr(t, "/ip4/1.2.3.8/udp/7777")
|
||||
id, err := peer.IDB58Decode("QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
pi := &PeerInfo{
|
||||
ID: id,
|
||||
Addrs: []ma.Multiaddr{a, b},
|
||||
}
|
||||
|
||||
data, err := pi.MarshalJSON()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
pi2 := new(PeerInfo)
|
||||
if err := pi2.UnmarshalJSON(data); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if pi2.ID != pi.ID {
|
||||
t.Fatal("ids didnt match after marshal")
|
||||
}
|
||||
|
||||
if !pi.Addrs[0].Equal(pi2.Addrs[0]) {
|
||||
t.Fatal("wrong addrs")
|
||||
}
|
||||
|
||||
if !pi.Addrs[1].Equal(pi2.Addrs[1]) {
|
||||
t.Fatal("wrong addrs")
|
||||
}
|
||||
|
||||
lgbl := pi2.Loggable()
|
||||
if lgbl["peerID"] != id.Pretty() {
|
||||
t.Fatal("loggables gave wrong peerID output")
|
||||
}
|
||||
}
|
||||
@@ -221,3 +221,25 @@ func TestPeerstoreProtoStore(t *testing.T) {
|
||||
t.Fatal("got wrong supported array: ", supported)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBasicPeerstore(t *testing.T) {
|
||||
ps := NewPeerstore()
|
||||
|
||||
var pids []peer.ID
|
||||
addrs := getAddrs(t, 10)
|
||||
for i, a := range addrs {
|
||||
p := peer.ID(fmt.Sprint(i))
|
||||
pids = append(pids, p)
|
||||
ps.AddAddr(p, a, PermanentAddrTTL)
|
||||
}
|
||||
|
||||
peers := ps.Peers()
|
||||
if len(peers) != 10 {
|
||||
t.Fatal("expected ten peers")
|
||||
}
|
||||
|
||||
pinfo := ps.PeerInfo(pids[0])
|
||||
if !pinfo.Addrs[0].Equal(addrs[0]) {
|
||||
t.Fatal("stored wrong address")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user