/
backoffconnector_test.go
96 lines (76 loc) · 2.45 KB
/
backoffconnector_test.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
package backoff
import (
"context"
"fmt"
"sync"
"testing"
"time"
"github.com/libp2p/go-libp2p-core/host"
"github.com/libp2p/go-libp2p-core/peer"
bhost "github.com/libp2p/go-libp2p-blankhost"
swarmt "github.com/libp2p/go-libp2p-swarm/testing"
"github.com/stretchr/testify/require"
)
type maxDialHost struct {
host.Host
mux sync.Mutex
timesDialed map[peer.ID]int
maxTimesToDial map[peer.ID]int
}
func (h *maxDialHost) Connect(ctx context.Context, ai peer.AddrInfo) error {
pid := ai.ID
h.mux.Lock()
defer h.mux.Unlock()
numDials := h.timesDialed[pid]
numDials += 1
h.timesDialed[pid] = numDials
if maxDials, ok := h.maxTimesToDial[pid]; ok && numDials > maxDials {
return fmt.Errorf("should not be dialing peer %s", pid.String())
}
return h.Host.Connect(ctx, ai)
}
func getNetHosts(t *testing.T, n int) []host.Host {
var out []host.Host
for i := 0; i < n; i++ {
netw := swarmt.GenSwarm(t)
h := bhost.NewBlankHost(netw)
t.Cleanup(func() { h.Close() })
out = append(out, h)
}
return out
}
func loadCh(peers []host.Host) <-chan peer.AddrInfo {
ch := make(chan peer.AddrInfo, len(peers))
for _, p := range peers {
ch <- p.Peerstore().PeerInfo(p.ID())
}
close(ch)
return ch
}
func TestBackoffConnector(t *testing.T) {
hosts := getNetHosts(t, 5)
primary := &maxDialHost{
Host: hosts[0],
timesDialed: make(map[peer.ID]int),
maxTimesToDial: map[peer.ID]int{
hosts[1].ID(): 1,
hosts[2].ID(): 2,
},
}
bc, err := NewBackoffConnector(primary, 10, time.Minute, NewFixedBackoff(250*time.Millisecond))
require.NoError(t, err)
bc.Connect(context.Background(), loadCh(hosts))
require.Eventually(t, func() bool { return len(primary.Network().Conns()) == len(hosts)-1 }, 3*time.Second, 10*time.Millisecond)
for _, c := range primary.Network().Conns() {
c.Close()
}
require.Eventually(t, func() bool { return len(primary.Network().Conns()) == 0 }, 3*time.Second, 10*time.Millisecond)
bc.Connect(context.Background(), loadCh(hosts))
require.Empty(t, primary.Network().Conns(), "shouldn't be connected to any peers")
time.Sleep(time.Millisecond * 500)
bc.Connect(context.Background(), loadCh(hosts))
require.Eventually(t, func() bool { return len(primary.Network().Conns()) == len(hosts)-2 }, 3*time.Second, 10*time.Millisecond)
// make sure we actually don't connect to host 1 any more
time.Sleep(100 * time.Millisecond)
require.Len(t, primary.Network().Conns(), len(hosts)-2, "wrong number of connections")
}