go-libp2p-peerstore/test/utils.go

59 lines
1.0 KiB
Go
Raw Normal View History

package test
2018-06-05 14:18:06 +08:00
import (
2018-07-25 07:48:16 +08:00
"context"
2018-06-05 14:18:06 +08:00
"fmt"
"testing"
2018-09-11 21:34:26 +08:00
peer "github.com/libp2p/go-libp2p-peer"
2018-09-01 01:49:07 +08:00
pt "github.com/libp2p/go-libp2p-peer/test"
2018-09-02 19:10:55 +08:00
ma "github.com/multiformats/go-multiaddr"
2018-06-05 14:18:06 +08:00
)
2018-09-04 18:34:49 +08:00
func multiaddr(m string) ma.Multiaddr {
maddr, err := ma.NewMultiaddr(m)
if err != nil {
panic(err)
}
return maddr
}
2018-06-05 14:18:06 +08:00
type peerpair struct {
2018-09-01 01:49:07 +08:00
ID peer.ID
Addr []ma.Multiaddr
2018-06-05 14:18:06 +08:00
}
func randomPeer(b *testing.B, addrCount int) *peerpair {
var (
pid peer.ID
err error
addrs = make([]ma.Multiaddr, addrCount)
aFmt = "/ip4/127.0.0.1/tcp/%d/ipfs/%s"
)
2018-06-05 14:18:06 +08:00
b.Helper()
2018-09-01 01:49:07 +08:00
if pid, err = pt.RandPeerID(); err != nil {
2018-06-05 14:18:06 +08:00
b.Fatal(err)
}
for i := 0; i < addrCount; i++ {
if addrs[i], err = ma.NewMultiaddr(fmt.Sprintf(aFmt, i, pid.Pretty())); err != nil {
b.Fatal(err)
}
2018-06-05 14:18:06 +08:00
}
return &peerpair{pid, addrs}
2018-06-05 14:18:06 +08:00
}
func addressProducer(ctx context.Context, b *testing.B, addrs chan *peerpair, addrsPerPeer int) {
b.Helper()
2018-06-05 14:18:06 +08:00
defer close(addrs)
for {
p := randomPeer(b, addrsPerPeer)
2018-07-25 07:48:16 +08:00
select {
case addrs <- p:
case <-ctx.Done():
return
2018-06-05 14:18:06 +08:00
}
}
}