-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmessenger_status_updates_test.go
171 lines (132 loc) · 4.6 KB
/
messenger_status_updates_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
package protocol
import (
"crypto/ecdsa"
"testing"
"github.com/stretchr/testify/suite"
"go.uber.org/zap"
gethbridge "github.com/status-im/status-go/eth-node/bridge/geth"
"github.com/status-im/status-go/eth-node/crypto"
"github.com/status-im/status-go/eth-node/types"
"github.com/status-im/status-go/protocol/protobuf"
"github.com/status-im/status-go/protocol/tt"
"github.com/status-im/status-go/waku"
)
func TestMessengerStatusUpdatesSuite(t *testing.T) {
suite.Run(t, new(MessengerStatusUpdatesSuite))
}
type MessengerStatusUpdatesSuite struct {
suite.Suite
m *Messenger
privateKey *ecdsa.PrivateKey // private key for the main instance of Messenger
// If one wants to send messages between different instances of Messenger,
// a single waku service should be shared.
shh types.Waku
logger *zap.Logger
}
func (s *MessengerStatusUpdatesSuite) SetupTest() {
s.logger = tt.MustCreateTestLogger()
config := waku.DefaultConfig
config.MinimumAcceptedPoW = 0
shh := waku.New(&config, s.logger)
s.shh = gethbridge.NewGethWakuWrapper(shh)
s.Require().NoError(shh.Start())
s.m = s.newMessenger()
s.privateKey = s.m.identity
_, err := s.m.Start()
s.Require().NoError(err)
}
func (s *MessengerStatusUpdatesSuite) TearDownTest() {
s.Require().NoError(s.m.Shutdown())
}
func (s *MessengerStatusUpdatesSuite) newMessenger() *Messenger {
privateKey, err := crypto.GenerateKey()
s.Require().NoError(err)
messenger, err := newMessengerWithKey(s.shh, privateKey, s.logger, nil)
s.Require().NoError(err)
return messenger
}
func (s *MessengerStatusUpdatesSuite) TestNextHigherClockValueOfAutomaticStatusUpdates() {
statusUpdate1 := UserStatus{
StatusType: int(protobuf.StatusUpdate_AUTOMATIC),
Clock: 100,
CustomText: "",
PublicKey: "pub-key1",
}
err := s.m.persistence.InsertStatusUpdate(statusUpdate1)
s.Require().NoError(err)
statusUpdate2 := UserStatus{
StatusType: int(protobuf.StatusUpdate_AUTOMATIC),
Clock: 200,
CustomText: "",
PublicKey: "pub-key2",
}
err = s.m.persistence.InsertStatusUpdate(statusUpdate2)
s.Require().NoError(err)
statusUpdate3 := UserStatus{
StatusType: int(protobuf.StatusUpdate_AUTOMATIC),
Clock: 300,
CustomText: "",
PublicKey: "pub-key3",
}
err = s.m.persistence.InsertStatusUpdate(statusUpdate3)
s.Require().NoError(err)
// nextClock: clock value next higher than passed clock, of status update of type StatusUpdate_AUTOMATIC
nextClock, err := s.m.persistence.NextHigherClockValueOfAutomaticStatusUpdates(100)
s.Require().NoError(err)
s.Require().Equal(nextClock, uint64(200))
}
func (s *MessengerStatusUpdatesSuite) TestDeactivatedStatusUpdates() {
statusUpdate1 := UserStatus{
StatusType: int(protobuf.StatusUpdate_AUTOMATIC),
Clock: 100,
CustomText: "",
PublicKey: "pub-key1",
}
err := s.m.persistence.InsertStatusUpdate(statusUpdate1)
s.Require().NoError(err)
statusUpdate2 := UserStatus{
StatusType: int(protobuf.StatusUpdate_AUTOMATIC),
Clock: 200,
CustomText: "",
PublicKey: "pub-key2",
}
err = s.m.persistence.InsertStatusUpdate(statusUpdate2)
s.Require().NoError(err)
statusUpdate3 := UserStatus{
StatusType: int(protobuf.StatusUpdate_AUTOMATIC),
Clock: 400,
CustomText: "",
PublicKey: "pub-key3",
}
err = s.m.persistence.InsertStatusUpdate(statusUpdate3)
s.Require().NoError(err)
statusUpdate4 := UserStatus{
StatusType: int(protobuf.StatusUpdate_AUTOMATIC),
Clock: 400, // Adding duplicate clock value for testing
CustomText: "",
PublicKey: "pub-key4",
}
err = s.m.persistence.InsertStatusUpdate(statusUpdate4)
s.Require().NoError(err)
statusUpdate5 := UserStatus{
StatusType: int(protobuf.StatusUpdate_AUTOMATIC),
Clock: 500,
CustomText: "",
PublicKey: "pub-key5",
}
err = s.m.persistence.InsertStatusUpdate(statusUpdate5)
s.Require().NoError(err)
// Lower limit is not included, but upper limit is included
// So every status update in this range (lowerClock upperClock] will be deactivated
deactivatedAutomaticStatusUpdates, err := s.m.persistence.DeactivatedAutomaticStatusUpdates(100, 400)
s.Require().NoError(err)
count := len(deactivatedAutomaticStatusUpdates)
s.Require().Equal(3, count)
// Status is deactivated
s.Require().Equal(int(protobuf.StatusUpdate_INACTIVE), deactivatedAutomaticStatusUpdates[0].StatusType)
// Lower range starts at 201 (clock + 1)
// (clock is bumped, so that client replaces old status update with new one)
s.Require().Equal(uint64(201), deactivatedAutomaticStatusUpdates[0].Clock)
//Upper rannge ends at 401 (clock + 1)
s.Require().Equal(uint64(401), deactivatedAutomaticStatusUpdates[count-1].Clock)
}