-
Notifications
You must be signed in to change notification settings - Fork 246
/
communities_events_eventual_consistency_test.go
188 lines (154 loc) · 6.22 KB
/
communities_events_eventual_consistency_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
package protocol
import (
"testing"
"time"
"github.com/stretchr/testify/suite"
"github.com/status-im/status-go/protocol/communities"
"github.com/status-im/status-go/protocol/protobuf"
"github.com/status-im/status-go/protocol/requests"
"github.com/status-im/status-go/protocol/tt"
"github.com/status-im/status-go/waku"
)
func TestCommunityEventsEventualConsistencySuite(t *testing.T) {
suite.Run(t, new(CommunityEventsEventualConsistencySuite))
}
type CommunityEventsEventualConsistencySuite struct {
AdminCommunityEventsSuiteBase
messagesOrderController *MessagesOrderController
}
func (s *CommunityEventsEventualConsistencySuite) SetupTest() {
s.logger = tt.MustCreateTestLogger()
s.collectiblesServiceMock = &CollectiblesServiceMock{}
s.mockedBalances = createMockedWalletBalance(&s.Suite)
config := waku.DefaultConfig
config.MinimumAcceptedPoW = 0
shh := waku.New(&config, s.logger)
wakuWrapper, err := newTestWakuWrapper(&config, s.logger)
s.Require().NoError(err)
s.Require().NoError(shh.Start())
s.shh = wakuWrapper
s.messagesOrderController = NewMessagesOrderController(messagesOrderRandom)
s.messagesOrderController.Start(wakuWrapper.SubscribePostEvents())
s.owner = s.newMessenger("", []string{})
s.admin = s.newMessenger(accountPassword, []string{eventsSenderAccountAddress})
s.alice = s.newMessenger(accountPassword, []string{aliceAccountAddress})
_, err = s.owner.Start()
s.Require().NoError(err)
_, err = s.admin.Start()
s.Require().NoError(err)
_, err = s.alice.Start()
s.Require().NoError(err)
}
func (s *CommunityEventsEventualConsistencySuite) TearDownTest() {
s.AdminCommunityEventsSuiteBase.TearDownTest()
s.messagesOrderController.Stop()
}
func (s *CommunityEventsEventualConsistencySuite) newMessenger(password string, walletAddresses []string) *Messenger {
return newTestCommunitiesMessenger(&s.Suite, s.shh, testCommunitiesMessengerConfig{
testMessengerConfig: testMessengerConfig{
logger: s.logger,
messagesOrderController: s.messagesOrderController,
},
password: password,
walletAddresses: walletAddresses,
mockedBalances: &s.mockedBalances,
collectiblesService: s.collectiblesServiceMock,
})
}
type requestToJoinActionType int
const (
requestToJoinAccept requestToJoinActionType = iota
requestToJoinReject
)
func (s *CommunityEventsEventualConsistencySuite) testRequestsToJoin(actions []requestToJoinActionType, messagesOrder messagesOrderType) {
community := setUpOnRequestCommunityAndRoles(s, protobuf.CommunityMember_ROLE_ADMIN, []*Messenger{})
s.Require().True(community.IsControlNode())
// set up additional user that will send request to join
user := s.newMessenger("", []string{})
s.SetupAdditionalMessengers([]*Messenger{user})
advertiseCommunityToUserOldWay(&s.Suite, community, s.owner, user)
// user sends request to join
requestToJoin := &requests.RequestToJoinCommunity{CommunityID: community.ID(), ENSName: "testName"}
response, err := user.RequestToJoinCommunity(requestToJoin)
s.Require().NoError(err)
s.Require().NotNil(response)
s.Require().Len(response.RequestsToJoinCommunity(), 1)
sentRequest := response.RequestsToJoinCommunity()[0]
checkRequestToJoin := func(r *MessengerResponse) bool {
for _, request := range r.RequestsToJoinCommunity() {
if request.ENSName == requestToJoin.ENSName {
return true
}
}
return false
}
// admin receives request to join
response, err = WaitOnMessengerResponse(
s.admin,
checkRequestToJoin,
"event sender did not receive community request to join",
)
s.Require().NoError(err)
s.Require().Len(response.RequestsToJoinCommunity(), 1)
for _, action := range actions {
switch action {
case requestToJoinAccept:
acceptRequestToJoin := &requests.AcceptRequestToJoinCommunity{ID: sentRequest.ID}
_, err = s.admin.AcceptRequestToJoinCommunity(acceptRequestToJoin)
s.Require().NoError(err)
case requestToJoinReject:
rejectRequestToJoin := &requests.DeclineRequestToJoinCommunity{ID: sentRequest.ID}
_, err = s.admin.DeclineRequestToJoinCommunity(rejectRequestToJoin)
s.Require().NoError(err)
}
}
// ensure all messages are pushed to waku
/*
FIXME: we should do it smarter, as follows:
```
hashes1, err := admin.SendEvent()
hashes2, err := admin.SendEvent()
WaitForHashes([][]byte{hashes1, hashes2}, admin.waku)
s.messagesOrderController.setCustomOrder([][]{hashes1, hashes2})
```
*/
time.Sleep(1 * time.Second)
// ensure events are received in order
s.messagesOrderController.order = messagesOrder
response, err = s.owner.RetrieveAll()
s.Require().NoError(err)
lastAction := actions[len(actions)-1]
responseChecker := func(mr *MessengerResponse) bool {
if len(mr.RequestsToJoinCommunity()) == 0 || len(mr.Communities()) == 0 {
return false
}
switch lastAction {
case requestToJoinAccept:
return mr.RequestsToJoinCommunity()[0].State == communities.RequestToJoinStateAccepted &&
mr.Communities()[0].HasMember(&user.identity.PublicKey)
case requestToJoinReject:
return mr.RequestsToJoinCommunity()[0].State == communities.RequestToJoinStateDeclined &&
!mr.Communities()[0].HasMember(&user.identity.PublicKey)
}
return false
}
switch messagesOrder {
case messagesOrderAsPosted:
_, err = WaitOnSignaledMessengerResponse(s.owner, responseChecker, "lack of eventual consistency")
s.Require().NoError(err)
case messagesOrderReversed:
s.Require().True(responseChecker(response))
}
}
func (s *CommunityEventsEventualConsistencySuite) TestAdminAcceptRejectRequestToJoin_InOrder() {
s.testRequestsToJoin([]requestToJoinActionType{requestToJoinAccept, requestToJoinReject}, messagesOrderAsPosted)
}
func (s *CommunityEventsEventualConsistencySuite) TestAdminAcceptRejectRequestToJoin_OutOfOrder() {
s.testRequestsToJoin([]requestToJoinActionType{requestToJoinAccept, requestToJoinReject}, messagesOrderReversed)
}
func (s *CommunityEventsEventualConsistencySuite) TestAdminRejectAcceptRequestToJoin_InOrder() {
s.testRequestsToJoin([]requestToJoinActionType{requestToJoinReject, requestToJoinAccept}, messagesOrderAsPosted)
}
func (s *CommunityEventsEventualConsistencySuite) TestAdminRejectAcceptRequestToJoin_OutOfOrder() {
s.testRequestsToJoin([]requestToJoinActionType{requestToJoinReject, requestToJoinAccept}, messagesOrderReversed)
}