@@ -17,6 +17,7 @@ limitations under the License.
17
17
package deliverclient
18
18
19
19
import (
20
+ "context"
20
21
"errors"
21
22
"runtime"
22
23
"sync"
@@ -259,7 +260,6 @@ func TestDeliverServiceServiceUnavailable(t *testing.T) {
259
260
os1 := mocks .NewOrderer (5615 , t )
260
261
os2 := mocks .NewOrderer (5616 , t )
261
262
262
- time .Sleep (time .Second )
263
263
gossipServiceAdapter := & mocks.MockGossipServiceAdapter {GossipBlockDisseminations : make (chan uint64 )}
264
264
265
265
service , err := NewDeliverService (& Config {
@@ -271,37 +271,63 @@ func TestDeliverServiceServiceUnavailable(t *testing.T) {
271
271
})
272
272
assert .NoError (t , err )
273
273
li := & mocks.MockLedgerInfo {Height : 100 }
274
- os1 .SetNextExpectedSeek (100 )
275
- os2 .SetNextExpectedSeek (100 )
274
+ os1 .SetNextExpectedSeek (li . Height )
275
+ os2 .SetNextExpectedSeek (li . Height )
276
276
277
277
err = service .StartDeliverForChannel ("TEST_CHAINID" , li )
278
278
assert .NoError (t , err , "can't start delivery" )
279
- // We need to discover to which instance the client connected to
280
- go os1 .SendBlock (100 )
281
- // Is it the first instance?
282
- instance2fail := os1
283
- nextBlockSeek := uint64 (100 )
284
- select {
285
- case seq := <- gossipServiceAdapter .GossipBlockDisseminations :
286
- // Just for sanity check, ensure we got block seq 100
287
- assert .Equal (t , uint64 (100 ), seq )
288
- // Connected to the first instance
289
- // Advance ledger's height by 1
290
- atomic .StoreUint64 (& li .Height , 101 )
291
- // Backup instance should expect a seek of 101 since we got 100
292
- os2 .SetNextExpectedSeek (uint64 (101 ))
293
- nextBlockSeek = uint64 (101 )
294
- // Have backup instance prepare to send a block
295
- os2 .SendBlock (101 )
296
- case <- time .After (time .Second * 5 ):
297
- // We didn't get a block on time, so seems like we're connected to the 2nd instance
298
- // and not to the first.
299
- instance2fail = os2
279
+
280
+ waitForConnectionToSomeOSN := func () (* mocks.Orderer , * mocks.Orderer ) {
281
+ for {
282
+ if os1 .ConnCount () > 0 {
283
+ return os1 , os2
284
+ }
285
+ if os2 .ConnCount () > 0 {
286
+ return os2 , os1
287
+ }
288
+ time .Sleep (time .Millisecond * 100 )
289
+ }
300
290
}
301
291
302
- instance2fail .Fail ()
292
+ activeInstance , backupInstance := waitForConnectionToSomeOSN ()
293
+ assert .NotNil (t , activeInstance )
294
+ assert .NotNil (t , backupInstance )
295
+
296
+ // Send first block
297
+ go activeInstance .SendBlock (li .Height )
298
+
299
+ assertBlockDissemination (li .Height , gossipServiceAdapter .GossipBlockDisseminations , t )
300
+ li .Height ++
301
+
302
+ // Backup instance should expect a seek of 101 since we got 100
303
+ backupInstance .SetNextExpectedSeek (li .Height )
304
+ // Have backup instance prepare to send a block
305
+ backupInstance .SendBlock (li .Height )
306
+
307
+ // Fail instance delivery client connected to
308
+ activeInstance .Fail ()
309
+ ctx , cancel := context .WithTimeout (context .Background (), time .Second )
310
+ defer cancel ()
311
+
312
+ wg := sync.WaitGroup {}
313
+ wg .Add (1 )
314
+
315
+ go func (ctx context.Context ) {
316
+ defer wg .Done ()
317
+ select {
318
+ case <- time .After (time .Millisecond * 100 ):
319
+ if backupInstance .ConnCount () > 0 {
320
+ return
321
+ }
322
+ case <- ctx .Done ():
323
+ return
324
+ }
325
+ }(ctx )
326
+
327
+ wg .Wait ()
328
+ assert .NoError (t , ctx .Err (), "Delivery client has not failed over to alive ordering service" )
303
329
// Ensure the client asks blocks from the other ordering service node
304
- assertBlockDissemination (nextBlockSeek , gossipServiceAdapter .GossipBlockDisseminations , t )
330
+ assertBlockDissemination (li . Height , gossipServiceAdapter .GossipBlockDisseminations , t )
305
331
306
332
// Cleanup
307
333
os1 .Shutdown ()
0 commit comments