forked from hanabi1224/go-libp2p-kad-dht-patcher
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest_utils.go
57 lines (50 loc) · 1.52 KB
/
test_utils.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
package kbucketfix
import (
"context"
"fmt"
"runtime"
"testing"
"time"
kaddht "github.com/libp2p/go-libp2p-kad-dht"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/libp2p/go-libp2p/core/peerstore"
bhost "github.com/libp2p/go-libp2p/p2p/host/basic"
"github.com/libp2p/go-libp2p/p2p/net/connmgr"
swarmt "github.com/libp2p/go-libp2p/p2p/net/swarm/testing"
"github.com/stretchr/testify/require"
)
func prepareTest(t *testing.T, timeout time.Duration) (context.Context, context.CancelFunc) {
ctx, cancel := context.WithCancel(context.Background())
go func() {
select {
case <-time.After(timeout):
buf := make([]byte, 1<<16)
runtime.Stack(buf, true)
fmt.Printf("%s", buf)
cancel()
t.Errorf("timeout exceeded")
// panic("timeout exceeded")
case <-ctx.Done():
}
}()
return ctx, cancel
}
func makeHost(t *testing.T, ctx context.Context) (*bhost.BasicHost, *kaddht.IpfsDHT) {
connMgr, _ := connmgr.NewConnManager(10, 100)
dhtOpts := []kaddht.Option{
kaddht.DisableAutoRefresh(),
kaddht.Mode(kaddht.ModeServer),
}
hostOpt := new(bhost.HostOpts)
hostOpt.ConnManager = connMgr
host, err := bhost.NewHost(swarmt.GenSwarm(t, swarmt.OptDisableReuseport, swarmt.OptDisableQUIC), hostOpt)
require.NoError(t, err)
hostDHT, err := kaddht.New(ctx, host, dhtOpts...)
require.NoError(t, err)
return host, hostDHT
}
func connect(a, b *bhost.BasicHost, ctx context.Context) {
hi := peer.AddrInfo{ID: b.ID(), Addrs: b.Addrs()}
a.Peerstore().AddAddrs(hi.ID, hi.Addrs, peerstore.PermanentAddrTTL)
a.Connect(ctx, hi)
}