-
Notifications
You must be signed in to change notification settings - Fork 65
/
util.go
102 lines (87 loc) · 1.84 KB
/
util.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package test
import (
"context"
"fmt"
"testing"
"github.com/libp2p/go-libp2p-core/peer"
pt "github.com/libp2p/go-libp2p-core/test"
ma "github.com/multiformats/go-multiaddr"
)
func Multiaddr(m string) ma.Multiaddr {
maddr, err := ma.NewMultiaddr(m)
if err != nil {
panic(err)
}
return maddr
}
type logpair struct {
ID peer.ID
Addr []ma.Multiaddr
}
func RandomPeer(b *testing.B, addrCount int) *logpair {
var (
pid peer.ID
err error
addrs = make([]ma.Multiaddr, addrCount)
aFmt = "/ip4/127.0.0.1/tcp/%d/ipfs/%s"
)
b.Helper()
if pid, err = pt.RandPeerID(); err != nil {
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)
}
}
return &logpair{pid, addrs}
}
func AddressProducer(ctx context.Context, b *testing.B, addrs chan *logpair, addrsPerPeer int) {
b.Helper()
defer close(addrs)
for {
p := RandomPeer(b, addrsPerPeer)
select {
case addrs <- p:
case <-ctx.Done():
return
}
}
}
func GenerateAddrs(count int) []ma.Multiaddr {
var addrs = make([]ma.Multiaddr, count)
for i := 0; i < count; i++ {
addrs[i] = Multiaddr(fmt.Sprintf("/ip4/1.1.1.%d/tcp/1111", i))
}
return addrs
}
func GeneratePeerIDs(count int) []peer.ID {
var ids = make([]peer.ID, count)
for i := 0; i < count; i++ {
ids[i], _ = pt.RandPeerID()
}
return ids
}
func AssertAddressesEqual(t *testing.T, exp, act []ma.Multiaddr) {
t.Helper()
if len(exp) != len(act) {
t.Fatalf("lengths not the same. expected %d, got %d\n", len(exp), len(act))
}
for _, a := range exp {
found := false
for _, b := range act {
if a.Equal(b) {
found = true
break
}
}
if !found {
t.Fatalf("expected address %s not found", a)
}
}
}
func check(t *testing.T, err error) {
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
}