Skip to content

Commit

Permalink
Merge pull request #82 from libp2p/test/metadata
Browse files Browse the repository at this point in the history
test: add metadata test
  • Loading branch information
Stebalien authored Jun 1, 2019
2 parents bb7e91a + 31ec77d commit 04e5878
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions p2p/host/peerstore/test/peerstore_suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down Expand Up @@ -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++ {
Expand Down

0 comments on commit 04e5878

Please sign in to comment.