@@ -31,6 +31,10 @@ import (
31
31
"github.com/stretchr/testify/mock"
32
32
)
33
33
34
+ func init () {
35
+ maxRetryDelay = time .Second
36
+ }
37
+
34
38
type mockMCS struct {
35
39
mock.Mock
36
40
}
@@ -103,6 +107,19 @@ func assertDelivery(t *testing.T, ga *mocks.MockGossipServiceAdapter, deliverer
103
107
}
104
108
}
105
109
110
+ func waitUntilOrFail (t * testing.T , pred func () bool ) {
111
+ timeout := time .Second * 30
112
+ start := time .Now ()
113
+ limit := start .UnixNano () + timeout .Nanoseconds ()
114
+ for time .Now ().UnixNano () < limit {
115
+ if pred () {
116
+ return
117
+ }
118
+ time .Sleep (timeout / 60 )
119
+ }
120
+ assert .Fail (t , "Timeout expired!" )
121
+ }
122
+
106
123
/*
107
124
Test to check whenever blocks provider starts calling new blocks from the
108
125
oldest and that eventually it terminates after the Stop method has been called.
@@ -179,7 +196,7 @@ func TestBlocksProvider_CheckTerminationDeliveryResponseStatus(t *testing.T) {
179
196
}
180
197
}
181
198
182
- func TestBlocksProvider_DeliveryServiceUnavailable (t * testing.T ) {
199
+ func TestBlocksProvider_DeliveryWrongStatus (t * testing.T ) {
183
200
sendBlock := func (seqNum uint64 ) * orderer.DeliverResponse {
184
201
return & orderer.DeliverResponse {
185
202
Type : & orderer.DeliverResponse_Block {
@@ -203,15 +220,16 @@ func TestBlocksProvider_DeliveryServiceUnavailable(t *testing.T) {
203
220
}
204
221
}
205
222
206
- bd := mocks.MockBlocksDeliverer {DisconnectCalled : make (chan struct {}, 1 )}
223
+ bd := mocks.MockBlocksDeliverer {DisconnectCalled : make (chan struct {}, 10 )}
207
224
mcs := & mockMCS {}
208
225
mcs .On ("VerifyBlock" , mock .Anything ).Return (nil )
209
226
gossipServiceAdapter := & mocks.MockGossipServiceAdapter {GossipBlockDisseminations : make (chan uint64 , 2 )}
210
227
provider := & blocksProviderImpl {
211
- chainID : "***TEST_CHAINID***" ,
212
- gossip : gossipServiceAdapter ,
213
- client : & bd ,
214
- mcs : mcs ,
228
+ chainID : "***TEST_CHAINID***" ,
229
+ gossip : gossipServiceAdapter ,
230
+ client : & bd ,
231
+ mcs : mcs ,
232
+ wrongStatusThreshold : wrongStatusThreshold ,
215
233
}
216
234
217
235
attempts := int32 (0 )
@@ -223,6 +241,14 @@ func TestBlocksProvider_DeliveryServiceUnavailable(t *testing.T) {
223
241
case int32 (2 ):
224
242
return sendStatus (common .Status_SERVICE_UNAVAILABLE ), nil
225
243
case int32 (3 ):
244
+ return sendStatus (common .Status_BAD_REQUEST ), nil
245
+ case int32 (4 ):
246
+ return sendStatus (common .Status_FORBIDDEN ), nil
247
+ case int32 (5 ):
248
+ return sendStatus (common .Status_NOT_FOUND ), nil
249
+ case int32 (6 ):
250
+ return sendStatus (common .Status_INTERNAL_SERVER_ERROR ), nil
251
+ case int32 (7 ):
226
252
return sendBlock (1 ), nil
227
253
default :
228
254
provider .Stop ()
@@ -236,12 +262,90 @@ func TestBlocksProvider_DeliveryServiceUnavailable(t *testing.T) {
236
262
select {
237
263
case seq := <- gossipServiceAdapter .GossipBlockDisseminations :
238
264
assert .Equal (t , uint64 (i ), seq )
239
- case <- time .After (time .Second * 3 ):
265
+ case <- time .After (time .Second * 10 ):
240
266
assert .Fail (t , "Didn't receive a block within a timely manner" )
241
267
}
242
268
}
243
269
// Make sure disconnect was called in between the deliveries
244
- assert .Len (t , bd .DisconnectCalled , 1 )
270
+ assert .Len (t , bd .DisconnectCalled , 5 )
271
+ }
272
+
273
+ func TestBlocksProvider_DeliveryWrongStatusClose (t * testing.T ) {
274
+ // Test emulate receive of wrong statuses from orderer
275
+ // Once test get sequence of wrongStatusThreshold (5) FORBIDDEN or BAD_REQUEST statuses,
276
+ // it stop blocks deliver go routine
277
+ // At start is sends all 5 statuses and check is each one of them caused disconnect and reconnect
278
+ // but blocks deliver still running
279
+ // At next step it sends 2 FORBIDDEN or BAD_REQUEST statuses, followed by SERVICE_UNAVAILABLE
280
+ // and 4 more FORBIDDEN or BAD_REQUEST statuses. It checks if enough disconnects called, but
281
+ // blocks deliver still running
282
+ // At the end, it send 2 FORBIDDEN or BAD_REQUEST statuses and check is blocks deliver stopped
283
+
284
+ sendStatus := func (status common.Status ) * orderer.DeliverResponse {
285
+ return & orderer.DeliverResponse {
286
+ Type : & orderer.DeliverResponse_Status {
287
+ Status : status ,
288
+ },
289
+ }
290
+ }
291
+
292
+ bd := mocks.MockBlocksDeliverer {DisconnectCalled : make (chan struct {}, 100 ), CloseCalled : make (chan struct {}, 1 )}
293
+ mcs := & mockMCS {}
294
+ mcs .On ("VerifyBlock" , mock .Anything ).Return (nil )
295
+ gossipServiceAdapter := & mocks.MockGossipServiceAdapter {GossipBlockDisseminations : make (chan uint64 , 2 )}
296
+ provider := & blocksProviderImpl {
297
+ chainID : "***TEST_CHAINID***" ,
298
+ gossip : gossipServiceAdapter ,
299
+ client : & bd ,
300
+ mcs : mcs ,
301
+ wrongStatusThreshold : 5 ,
302
+ }
303
+
304
+ incomingMsgs := make (chan * orderer.DeliverResponse )
305
+
306
+ bd .MockRecv = func (mock * mocks.MockBlocksDeliverer ) (* orderer.DeliverResponse , error ) {
307
+ inMsg := <- incomingMsgs
308
+ return inMsg , nil
309
+ }
310
+
311
+ go provider .DeliverBlocks ()
312
+
313
+ incomingMsgs <- sendStatus (common .Status_SERVICE_UNAVAILABLE )
314
+ incomingMsgs <- sendStatus (common .Status_BAD_REQUEST )
315
+ incomingMsgs <- sendStatus (common .Status_FORBIDDEN )
316
+ incomingMsgs <- sendStatus (common .Status_NOT_FOUND )
317
+ incomingMsgs <- sendStatus (common .Status_INTERNAL_SERVER_ERROR )
318
+
319
+ waitUntilOrFail (t , func () bool {
320
+ return len (bd .DisconnectCalled ) == 5
321
+ })
322
+
323
+ waitUntilOrFail (t , func () bool {
324
+ return len (bd .CloseCalled ) == 0
325
+ })
326
+
327
+ incomingMsgs <- sendStatus (common .Status_FORBIDDEN )
328
+ incomingMsgs <- sendStatus (common .Status_BAD_REQUEST )
329
+ incomingMsgs <- sendStatus (common .Status_SERVICE_UNAVAILABLE )
330
+ incomingMsgs <- sendStatus (common .Status_FORBIDDEN )
331
+ incomingMsgs <- sendStatus (common .Status_BAD_REQUEST )
332
+ incomingMsgs <- sendStatus (common .Status_FORBIDDEN )
333
+ incomingMsgs <- sendStatus (common .Status_BAD_REQUEST )
334
+
335
+ waitUntilOrFail (t , func () bool {
336
+ return len (bd .DisconnectCalled ) == 12
337
+ })
338
+
339
+ waitUntilOrFail (t , func () bool {
340
+ return len (bd .CloseCalled ) == 0
341
+ })
342
+
343
+ incomingMsgs <- sendStatus (common .Status_BAD_REQUEST )
344
+ incomingMsgs <- sendStatus (common .Status_FORBIDDEN )
345
+
346
+ waitUntilOrFail (t , func () bool {
347
+ return len (bd .CloseCalled ) == 1
348
+ })
245
349
}
246
350
247
351
func TestBlockFetchFailure (t * testing.T ) {
0 commit comments