forked from berty/berty
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtinder_swiper_test.go
186 lines (161 loc) · 4.66 KB
/
tinder_swiper_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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package bertyprotocol
import (
"context"
"encoding/hex"
"fmt"
"testing"
"time"
"github.com/libp2p/go-libp2p-core/peer"
p2pmocknet "github.com/libp2p/go-libp2p/p2p/net/mock"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"berty.tech/berty/v2/go/internal/ipfsutil"
"berty.tech/berty/v2/go/internal/testutil"
)
func TestRoundTimePeriod_Next(t *testing.T) {
cases := []struct {
time time.Time
period time.Duration
out time.Time
next time.Time
}{
{
time: time.Date(2020, 4, 10, 12, 0, 0, 0, time.UTC),
period: time.Hour,
out: time.Date(2020, 4, 10, 12, 0, 0, 0, time.UTC),
next: time.Date(2020, 4, 10, 13, 0, 0, 0, time.UTC),
},
{
time: time.Date(2020, 4, 10, 12, 0, 0, 0, time.UTC),
period: -time.Hour,
out: time.Date(2020, 4, 10, 12, 0, 0, 0, time.UTC),
next: time.Date(2020, 4, 10, 13, 0, 0, 0, time.UTC),
},
{
time: time.Date(2020, 4, 10, 12, 34, 56, 0, time.UTC),
period: time.Hour,
out: time.Date(2020, 4, 10, 12, 0, 0, 0, time.UTC),
next: time.Date(2020, 4, 10, 13, 0, 0, 0, time.UTC),
},
}
for i, tc := range cases {
t.Run(fmt.Sprintf("tc: %d", i), func(t *testing.T) {
rounded := roundTimePeriod(tc.time, tc.period)
require.Equal(t, tc.out, rounded)
next := nextTimePeriod(tc.time, tc.period)
require.Equal(t, tc.next, next)
})
}
}
func TestGenerateRendezvousPointForPeriod(t *testing.T) {
baseTimeA := time.Date(2020, 4, 10, 12, 0, 0, 0, time.UTC)
baseTimeB := time.Date(2020, 4, 10, 13, 0, 0, 0, time.UTC)
cases := []struct {
topic []byte
seed []byte
expected string // as hex string
time time.Time
}{
{
topic: []byte("topicA"),
seed: []byte("seedA"),
expected: "8b7fdc831ca90f78995f32d8b9cf7bc8682a7fc250fe13a9b7c5c0851a3b8cbc",
time: baseTimeA,
},
{
topic: []byte("topicA"),
seed: []byte("seedA"),
expected: "ec86e0cb471195733ebbeb04277aafcfc60a19c09195cf04e60b50857465c27f",
time: baseTimeB,
},
{
topic: []byte("topicA"),
seed: []byte("seedB"),
expected: "f87f5dfc4e8a68be75d6008ab3aa0a4295b6049e9ec21f03d0c1410895171683",
time: baseTimeA,
},
{
topic: []byte("topicB"),
seed: []byte("seedA"),
expected: "6350bd507198a9acb816dcadae8c5c6ff6c96ee6c9606c8ebe15c1f645ac4c4e",
time: baseTimeA,
},
}
for i, tc := range cases {
t.Run(fmt.Sprintf("tc: %d", i), func(t *testing.T) {
point := generateRendezvousPointForPeriod(tc.topic, tc.seed, tc.time)
require.Equal(t, tc.expected, hex.EncodeToString(point))
})
}
}
func TestAnnounceWatchForPeriod(t *testing.T) {
testutil.FilterSpeed(t, testutil.Slow)
cases := []struct {
expectedPeersFound int
topicA []byte
topicB []byte
seedA []byte
seedB []byte
}{
{
expectedPeersFound: 0,
topicA: []byte("topicA"),
topicB: []byte("topicB"),
seedA: []byte("seedA"),
seedB: []byte("seedA"),
},
{
expectedPeersFound: 1,
topicA: []byte("topicA"),
topicB: []byte("topicA"),
seedA: []byte("seedA"),
seedB: []byte("seedA"),
},
}
logger, cleanup := testutil.Logger(t)
defer cleanup()
for i, tc := range cases {
t.Run(fmt.Sprintf("tc: %d", i), func(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
mn := p2pmocknet.New(ctx)
rdvp, err := mn.GenPeer()
require.NoError(t, err, "failed to generate mocked peer")
defer rdvp.Close()
_, rdv_cleanup := ipfsutil.TestingRDVP(ctx, t, rdvp)
defer rdv_cleanup()
opts := &ipfsutil.TestingAPIOpts{
Logger: logger,
Mocknet: mn,
RDVPeer: rdvp.Peerstore().PeerInfo(rdvp.ID()),
}
apiA, cleanup := ipfsutil.TestingCoreAPIUsingMockNet(ctx, t, opts)
defer cleanup()
apiB, cleanup := ipfsutil.TestingCoreAPIUsingMockNet(ctx, t, opts)
defer cleanup()
err = mn.LinkAll()
require.NoError(t, err)
err = mn.ConnectAllButSelf()
require.NoError(t, err)
swiperA := NewSwiper(opts.Logger, apiA.PubSub(), time.Hour)
swiperB := NewSwiper(opts.Logger, apiB.PubSub(), time.Hour)
swiperA.Announce(ctx, tc.topicA, tc.seedA)
time.Sleep(time.Millisecond * 100)
ch := make(chan peer.AddrInfo)
doneFn := func() {}
go swiperB.WatchTopic(ctx, tc.topicB, tc.seedB, ch, doneFn)
var foundPeers int
for foundPeers = 0; foundPeers < tc.expectedPeersFound; foundPeers++ {
select {
case <-ctx.Done():
break
case <-ch:
}
}
assert.Equal(t, len(ch), 0)
assert.Equal(t, tc.expectedPeersFound, foundPeers)
})
}
}
func TestAnnounceForPeriod(t *testing.T) {
}