-
Notifications
You must be signed in to change notification settings - Fork 0
/
peerset_test.go
88 lines (78 loc) · 2.23 KB
/
peerset_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
package core
import (
"testing"
"time"
"github.com/hood-chat/core/entity"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/stretchr/testify/require"
)
func getPeers(n int) []peer.AddrInfo {
peers := make([]peer.AddrInfo, 0)
for i := 0; i < n; i++ {
p, _ := entity.CreateIdentity("asd")
a, _ := p.ToContact().AdderInfo()
peers = append(peers, *a)
}
return peers
}
func TestPeerSet(t *testing.T) {
needed := NewPeerSet()
peers := getPeers(5)
// test add and remove and all done
needed.Add("t1", peers[0])
require.False(t, needed.Empty())
needed.Add("t1", peers[0])
require.False(t, needed.Empty())
needed.Remove("t1", peers[0].ID)
require.False(t, needed.Empty())
needed.Remove("t1", peers[0].ID)
require.True(t, needed.Empty())
// test turn and done
needed.Add("t1", peers[0])
turn := needed.Turn(time.Now())
require.Exactly(t, peers[0], turn[0])
turn = needed.Turn(time.Now())
require.Empty(t, turn)
require.False(t, needed.Empty())
needed.Done(peers[0].ID)
turn = needed.Turn(time.Now())
require.Empty(t, turn)
needed.Remove("t1", peers[0].ID)
require.True(t, needed.Empty())
// test force
needed.Add("t1", peers[0])
needed.Force(peers[0].ID)
turn = needed.Turn(time.Now())
require.Empty(t, turn)
needed.Remove("t1", peers[0].ID)
require.True(t, needed.Empty())
// test fail
needed.Add("t1", peers[0])
needed.Force(peers[0].ID)
needed.Failed(peers[0].ID)
require.Empty(t, needed.Turn(time.Now()))
require.Eventually(t, func() bool { return len(needed.Turn(time.Now())) != 0 }, 10*time.Second, 1*time.Second)
require.Empty(t, needed.Turn(time.Now()))
needed.Remove("t1", peers[0].ID)
require.True(t, needed.Empty())
// test counter
needed.Add("t1", peers[0])
require.False(t, needed.Empty())
needed.Add("t1", peers[0])
require.False(t, needed.Empty())
needed.Remove("t1", peers[0].ID)
require.False(t, needed.Empty())
needed.Remove("t1", peers[0].ID)
require.True(t, needed.Empty())
needed.Remove("t1", peers[0].ID)
// needed.Remove("t1", peers[0].ID)
}
func TestPeerSetRemove(t *testing.T) {
needed := NewPeerSet()
peers := getPeers(5)
needed.Add("t1", peers[0])
require.False(t, needed.Empty())
needed.Remove("t1", peers[0].ID)
needed.Remove("t1", peers[0].ID)
// needed.Remove("t1", peers[0].ID)
}