@@ -22,6 +22,8 @@ import (
22
22
"testing"
23
23
"time"
24
24
25
+ "sync"
26
+
25
27
"github.com/hyperledger/fabric/gossip/common"
26
28
"github.com/stretchr/testify/assert"
27
29
)
@@ -51,6 +53,16 @@ func compareInts(this interface{}, that interface{}) common.InvalidationResult {
51
53
return common .MessageInvalidated
52
54
}
53
55
56
+ func nonReplaceInts (this interface {}, that interface {}) common.InvalidationResult {
57
+ a := this .(int )
58
+ b := that .(int )
59
+ if a == b {
60
+ return common .MessageInvalidated
61
+ }
62
+
63
+ return common .MessageNoAction
64
+ }
65
+
54
66
func TestSize (t * testing.T ) {
55
67
msgStore := NewMessageStore (alwaysNoAction , noopTrigger )
56
68
msgStore .Add (0 )
@@ -108,6 +120,7 @@ func TestNewMessagesInvalidated(t *testing.T) {
108
120
}
109
121
110
122
func TestConcurrency (t * testing.T ) {
123
+ t .Parallel ()
111
124
stopFlag := int32 (0 )
112
125
msgStore := NewMessageStore (compareInts , noopTrigger )
113
126
looper := func (f func ()) func () {
@@ -141,3 +154,131 @@ func TestConcurrency(t *testing.T) {
141
154
142
155
atomic .CompareAndSwapInt32 (& stopFlag , 0 , 1 )
143
156
}
157
+
158
+ func TestExpiration (t * testing.T ) {
159
+ t .Parallel ()
160
+ expired := make ([]int , 0 )
161
+ msgTTL := time .Second * 3
162
+
163
+ msgStore := NewMessageStoreExpirable (nonReplaceInts , noopTrigger , msgTTL , nil , nil , func (m interface {}) {
164
+ expired = append (expired , m .(int ))
165
+ })
166
+
167
+ for i := 0 ; i < 10 ; i ++ {
168
+ assert .True (t , msgStore .Add (i ), "Adding" , i )
169
+ }
170
+
171
+ assert .Equal (t , 10 , msgStore .Size (), "Wrong number of items in store - first batch" )
172
+
173
+ time .Sleep (time .Second * 2 )
174
+
175
+ for i := 0 ; i < 10 ; i ++ {
176
+ assert .False (t , msgStore .CheckValid (i ))
177
+ assert .False (t , msgStore .Add (i ))
178
+ }
179
+
180
+ for i := 10 ; i < 20 ; i ++ {
181
+ assert .True (t , msgStore .CheckValid (i ))
182
+ assert .True (t , msgStore .Add (i ))
183
+ assert .False (t , msgStore .CheckValid (i ))
184
+ }
185
+ assert .Equal (t , 20 , msgStore .Size (), "Wrong number of items in store - second batch" )
186
+
187
+ time .Sleep (time .Second * 2 )
188
+
189
+ for i := 0 ; i < 20 ; i ++ {
190
+ assert .False (t , msgStore .Add (i ))
191
+ }
192
+
193
+ assert .Equal (t , 10 , msgStore .Size (), "Wrong number of items in store - after first batch expiration" )
194
+ assert .Equal (t , 10 , len (expired ), "Wrong number of expired msgs - after first batch expiration" )
195
+
196
+ time .Sleep (time .Second * 4 )
197
+
198
+ assert .Equal (t , 0 , msgStore .Size (), "Wrong number of items in store - after second batch expiration" )
199
+ assert .Equal (t , 20 , len (expired ), "Wrong number of expired msgs - after second batch expiration" )
200
+
201
+ for i := 0 ; i < 10 ; i ++ {
202
+ assert .True (t , msgStore .CheckValid (i ))
203
+ assert .True (t , msgStore .Add (i ))
204
+ assert .False (t , msgStore .CheckValid (i ))
205
+ }
206
+
207
+ assert .Equal (t , 10 , msgStore .Size (), "Wrong number of items in store - after second batch expiration and first banch re-added" )
208
+
209
+ }
210
+
211
+ func TestExpirationConcurrency (t * testing.T ) {
212
+ t .Parallel ()
213
+ expired := make ([]int , 0 )
214
+ msgTTL := time .Second * 3
215
+ lock := & sync.RWMutex {}
216
+
217
+ msgStore := NewMessageStoreExpirable (nonReplaceInts , noopTrigger , msgTTL ,
218
+ func () {
219
+ lock .Lock ()
220
+ },
221
+ func () {
222
+ lock .Unlock ()
223
+ },
224
+ func (m interface {}) {
225
+ expired = append (expired , m .(int ))
226
+ })
227
+
228
+ lock .Lock ()
229
+ for i := 0 ; i < 10 ; i ++ {
230
+ assert .True (t , msgStore .Add (i ), "Adding" , i )
231
+ }
232
+ assert .Equal (t , 10 , msgStore .Size (), "Wrong number of items in store - first batch" )
233
+ lock .Unlock ()
234
+
235
+ time .Sleep (time .Second * 2 )
236
+
237
+ lock .Lock ()
238
+ time .Sleep (time .Second * 2 )
239
+
240
+ for i := 0 ; i < 10 ; i ++ {
241
+ assert .False (t , msgStore .Add (i ))
242
+ }
243
+
244
+ assert .Equal (t , 10 , msgStore .Size (), "Wrong number of items in store - after first batch expiration, external lock taken" )
245
+ assert .Equal (t , 0 , len (expired ), "Wrong number of expired msgs - after first batch expiration, external lock taken" )
246
+ lock .Unlock ()
247
+
248
+ time .Sleep (time .Second * 1 )
249
+
250
+ lock .Lock ()
251
+ for i := 0 ; i < 10 ; i ++ {
252
+ assert .False (t , msgStore .Add (i ))
253
+ }
254
+
255
+ assert .Equal (t , 0 , msgStore .Size (), "Wrong number of items in store - after first batch expiration, expiration should run" )
256
+ assert .Equal (t , 10 , len (expired ), "Wrong number of expired msgs - after first batch expiration, expiration should run" )
257
+
258
+ lock .Unlock ()
259
+ }
260
+
261
+ func TestStop (t * testing.T ) {
262
+ t .Parallel ()
263
+ expired := make ([]int , 0 )
264
+ msgTTL := time .Second * 3
265
+
266
+ msgStore := NewMessageStoreExpirable (nonReplaceInts , noopTrigger , msgTTL , nil , nil , func (m interface {}) {
267
+ expired = append (expired , m .(int ))
268
+ })
269
+
270
+ for i := 0 ; i < 10 ; i ++ {
271
+ assert .True (t , msgStore .Add (i ), "Adding" , i )
272
+ }
273
+
274
+ assert .Equal (t , 10 , msgStore .Size (), "Wrong number of items in store - first batch" )
275
+
276
+ msgStore .Stop ()
277
+
278
+ time .Sleep (time .Second * 4 )
279
+
280
+ assert .Equal (t , 10 , msgStore .Size (), "Wrong number of items in store - after first batch expiration, but store was stopped, so no expiration" )
281
+ assert .Equal (t , 0 , len (expired ), "Wrong number of expired msgs - after first batch expiration, but store was stopped, so no expiration" )
282
+
283
+ msgStore .Stop ()
284
+ }
0 commit comments