mirror of
https://github.com/libp2p/go-libp2p-peerstore.git
synced 2024-12-27 23:40:16 +08:00
test: add metadata test
This commit is contained in:
parent
e15116528b
commit
9a662b9911
@ -21,6 +21,7 @@ var peerstoreSuite = map[string]func(pstore.Peerstore) func(*testing.T){
|
||||
"AddStreamDuplicates": testAddrStreamDuplicates,
|
||||
"PeerstoreProtoStore": testPeerstoreProtoStore,
|
||||
"BasicPeerstore": testBasicPeerstore,
|
||||
"Metadata": testMetadata,
|
||||
}
|
||||
|
||||
type PeerstoreFactory func() (pstore.Peerstore, func())
|
||||
@ -279,6 +280,46 @@ func testBasicPeerstore(ps pstore.Peerstore) func(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func testMetadata(ps pstore.Peerstore) func(t *testing.T) {
|
||||
return func(t *testing.T) {
|
||||
pids := make([]peer.ID, 10)
|
||||
for i := range pids {
|
||||
priv, _, _ := crypto.GenerateKeyPair(crypto.RSA, 512)
|
||||
p, _ := peer.IDFromPrivateKey(priv)
|
||||
pids[i] = p
|
||||
}
|
||||
for _, p := range pids {
|
||||
if err := ps.Put(p, "AgentVersion", "string"); err != nil {
|
||||
t.Errorf("failed to put %q: %s", "AgentVersion", err)
|
||||
}
|
||||
if err := ps.Put(p, "bar", 1); err != nil {
|
||||
t.Errorf("failed to put %q: %s", "bar", err)
|
||||
}
|
||||
}
|
||||
for _, p := range pids {
|
||||
v, err := ps.Get(p, "AgentVersion")
|
||||
if err != nil {
|
||||
t.Errorf("failed to find %q: %s", "AgentVersion", err)
|
||||
continue
|
||||
}
|
||||
if v != "string" {
|
||||
t.Errorf("expected %q, got %q", "string", p)
|
||||
continue
|
||||
}
|
||||
|
||||
v, err = ps.Get(p, "bar")
|
||||
if err != nil {
|
||||
t.Errorf("failed to find %q: %s", "bar", err)
|
||||
continue
|
||||
}
|
||||
if v != 1 {
|
||||
t.Errorf("expected %q, got %v", 1, v)
|
||||
continue
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func getAddrs(t *testing.T, n int) []ma.Multiaddr {
|
||||
var addrs []ma.Multiaddr
|
||||
for i := 0; i < n; i++ {
|
||||
|
Loading…
Reference in New Issue
Block a user