-
Notifications
You must be signed in to change notification settings - Fork 28
/
deactivate_test.go
296 lines (232 loc) · 7.95 KB
/
deactivate_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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
package weshnet_test
import (
"context"
"testing"
"time"
ds "github.com/ipfs/go-datastore"
dsync "github.com/ipfs/go-datastore/sync"
mocknet "github.com/libp2p/go-libp2p/p2p/net/mock"
"github.com/stretchr/testify/require"
"berty.tech/weshnet/v2"
"berty.tech/weshnet/v2/pkg/protocoltypes"
"berty.tech/weshnet/v2/pkg/testutil"
"berty.tech/weshnet/v2/pkg/tinder"
)
func TestReactivateAccountGroup(t *testing.T) {
testutil.FilterStability(t, testutil.Flappy)
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Minute)
defer cancel()
logger, cleanup := testutil.Logger(t)
defer cleanup()
mn := mocknet.New()
defer mn.Close()
msrv := tinder.NewMockDriverServer()
// Setup 3 nodes
dsA := dsync.MutexWrap(ds.NewMapDatastore())
nodeA, closeNodeA := weshnet.NewTestingProtocol(ctx, t, &weshnet.TestingOpts{
Logger: logger.Named("nodeA"),
Mocknet: mn,
DiscoveryServer: msrv,
}, dsA)
defer closeNodeA()
dsB := dsync.MutexWrap(ds.NewMapDatastore())
nodeB, closeNodeB := weshnet.NewTestingProtocol(ctx, t, &weshnet.TestingOpts{
Logger: logger.Named("nodeB"),
Mocknet: mn,
DiscoveryServer: msrv,
}, dsB)
defer closeNodeB()
dsC := dsync.MutexWrap(ds.NewMapDatastore())
nodeC, closeNodeC := weshnet.NewTestingProtocol(ctx, t, &weshnet.TestingOpts{
Logger: logger.Named("nodeC"),
Mocknet: mn,
DiscoveryServer: msrv,
}, dsC)
defer closeNodeC()
// make connections
err := mn.LinkAll()
require.NoError(t, err)
err = mn.ConnectAllButSelf()
require.NoError(t, err)
// test communication between nodeA and nodeB
nodes := []*weshnet.TestingProtocol{nodeA, nodeB}
addAsContact(ctx, t, nodes, nodes)
sendMessageToContact(ctx, t, []string{"pre-deactivate nodeA-nodeB"}, nodes)
// reactivate nodeA account group
nodeACfg, err := nodeA.Client.ServiceGetConfiguration(ctx, &protocoltypes.ServiceGetConfiguration_Request{})
require.NoError(t, err)
require.NotNil(t, nodeACfg)
_, err = nodeA.Client.DeactivateGroup(ctx, &protocoltypes.DeactivateGroup_Request{
GroupPk: nodeACfg.AccountGroupPk,
})
require.NoError(t, err)
_, err = nodeA.Client.ActivateGroup(ctx, &protocoltypes.ActivateGroup_Request{
GroupPk: nodeACfg.AccountGroupPk,
})
require.NoError(t, err)
// test communication between nodeA and nodeC
nodes = []*weshnet.TestingProtocol{nodeA, nodeC}
addAsContact(ctx, t, nodes, nodes)
sendMessageToContact(ctx, t, []string{"post reactivate nodeA-nodeC"}, nodes)
}
func TestRaceReactivateAccountGroup(t *testing.T) {
testutil.FilterStability(t, testutil.Flappy)
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Minute)
defer cancel()
logger, cleanup := testutil.Logger(t)
defer cleanup()
mn := mocknet.New()
defer mn.Close()
msrv := tinder.NewMockDriverServer()
// Setup 2 nodes
dsA := dsync.MutexWrap(ds.NewMapDatastore())
nodeA, closeNodeA := weshnet.NewTestingProtocol(ctx, t, &weshnet.TestingOpts{
Logger: logger.Named("nodeA"),
Mocknet: mn,
DiscoveryServer: msrv,
}, dsA)
defer closeNodeA()
dsB := dsync.MutexWrap(ds.NewMapDatastore())
nodeB, closeNodeB := weshnet.NewTestingProtocol(ctx, t, &weshnet.TestingOpts{
Logger: logger.Named("nodeB"),
Mocknet: mn,
DiscoveryServer: msrv,
}, dsB)
defer closeNodeB()
// make connections
err := mn.LinkAll()
require.NoError(t, err)
err = mn.ConnectAllButSelf()
require.NoError(t, err)
// reactivate nodeA account group
nodeACfg, err := nodeA.Client.ServiceGetConfiguration(ctx, &protocoltypes.ServiceGetConfiguration_Request{})
require.NoError(t, err)
require.NotNil(t, nodeACfg)
deactivateFunc := func() {
t.Log("DeactivateGroup")
_, err := nodeA.Client.DeactivateGroup(ctx, &protocoltypes.DeactivateGroup_Request{
GroupPk: nodeACfg.AccountGroupPk,
})
require.NoError(t, err)
}
activateFunc := func() {
t.Log("ActivateGroup")
_, err := nodeA.Client.ActivateGroup(ctx, &protocoltypes.ActivateGroup_Request{
GroupPk: nodeACfg.AccountGroupPk,
})
require.NoError(t, err)
}
go deactivateFunc()
time.Sleep(1 * time.Millisecond)
go activateFunc()
// test communication between nodeA and nodeB
time.Sleep(3 * time.Second)
nodes := []*weshnet.TestingProtocol{nodeA, nodeB}
t.Log("addAsContact")
addAsContact(ctx, t, nodes, nodes)
t.Log("sendMessageToContact")
sendMessageToContact(ctx, t, []string{"nodeA-nodeB"}, nodes)
}
func TestReactivateContactGroup(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Minute)
defer cancel()
logger, cleanup := testutil.Logger(t)
defer cleanup()
opts := weshnet.TestingOpts{
Mocknet: mocknet.New(),
Logger: logger,
ConnectFunc: weshnet.ConnectAll,
}
nodes, cleanup := weshnet.NewTestingProtocolWithMockedPeers(ctx, t, &opts, nil, 2)
defer cleanup()
addAsContact(ctx, t, nodes, nodes)
// send messages before deactivating
sendMessageToContact(ctx, t, []string{"pre-deactivate"}, nodes)
// get contact group
contactGroup := getContactGroup(ctx, t, nodes[0], nodes[1])
// deactivate contact group
_, err := nodes[0].Client.DeactivateGroup(ctx, &protocoltypes.DeactivateGroup_Request{
GroupPk: contactGroup.Group.PublicKey,
})
require.NoError(t, err)
// reactivate group
_, err = nodes[0].Client.ActivateGroup(ctx, &protocoltypes.ActivateGroup_Request{
GroupPk: contactGroup.Group.PublicKey,
})
require.NoError(t, err)
// send message after reactivating
sendMessageToContact(ctx, t, []string{"post-reactivate"}, nodes)
}
func TestRaceReactivateContactGroup(t *testing.T) {
testutil.FilterStability(t, testutil.Flappy)
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Minute)
defer cancel()
logger, cleanup := testutil.Logger(t)
defer cleanup()
opts := weshnet.TestingOpts{
Mocknet: mocknet.New(),
Logger: logger,
ConnectFunc: weshnet.ConnectAll,
}
nodes, cleanup := weshnet.NewTestingProtocolWithMockedPeers(ctx, t, &opts, nil, 2)
defer cleanup()
t.Log("addAsContact")
addAsContact(ctx, t, nodes, nodes)
// send messages before deactivating
t.Log("sendMessageToContact")
sendMessageToContact(ctx, t, []string{"pre-deactivate"}, nodes)
// get contact group
contactGroup := getContactGroup(ctx, t, nodes[0], nodes[1])
// deactivate contact group
deactivateFunc := func() {
t.Log("DeactivateGroup")
_, err := nodes[0].Client.DeactivateGroup(ctx, &protocoltypes.DeactivateGroup_Request{
GroupPk: contactGroup.Group.PublicKey,
})
require.NoError(t, err)
}
// reactivate group
activateFunc := func() {
t.Log("ActivateGroup")
_, err := nodes[0].Client.ActivateGroup(ctx, &protocoltypes.ActivateGroup_Request{
GroupPk: contactGroup.Group.PublicKey,
})
require.NoError(t, err)
}
go deactivateFunc()
time.Sleep(1 * time.Millisecond)
go activateFunc()
// send message after reactivating
time.Sleep(5 * time.Second)
t.Log("sendMessageToContact")
sendMessageToContact(ctx, t, []string{"post-reactivate"}, nodes)
}
func TestReactivateMultimemberGroup(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Minute)
defer cancel()
logger, cleanup := testutil.Logger(t)
defer cleanup()
opts := weshnet.TestingOpts{
Mocknet: mocknet.New(),
Logger: logger,
ConnectFunc: weshnet.ConnectAll,
}
nodes, cleanup := weshnet.NewTestingProtocolWithMockedPeers(ctx, t, &opts, nil, 2)
defer cleanup()
// Create MultiMember Group
group := weshnet.CreateMultiMemberGroupInstance(ctx, t, nodes[0], nodes[1])
// Send message before deactivation
sendMessageOnGroup(ctx, t, nodes, nodes, group.PublicKey, []string{"pre-deactivate"})
// deactivate multimember group
_, err := nodes[0].Client.DeactivateGroup(ctx, &protocoltypes.DeactivateGroup_Request{
GroupPk: group.PublicKey,
})
require.NoError(t, err)
// reactivate group
_, err = nodes[0].Client.ActivateGroup(ctx, &protocoltypes.ActivateGroup_Request{
GroupPk: group.PublicKey,
})
require.NoError(t, err)
// Send message after reactivation
sendMessageOnGroup(ctx, t, nodes, nodes, group.PublicKey, []string{"post-deactivate"})
}