Skip to content

Commit ced5e5f

Browse files
committed
[FAB-4901]: Harden delivery service unit tests
There are a few failure discovered recently in CI of the TestDeliverServiceServiceUnavailable test, this commits takes care to harden and refactor the test to make it resilient for intermittent failures. Change-Id: I470ff6aa8c6aa3fb48312f9b4858e354777204b9 Signed-off-by: Artem Barger <[email protected]>
1 parent 3bc937e commit ced5e5f

File tree

1 file changed

+52
-26
lines changed

1 file changed

+52
-26
lines changed

core/deliverservice/deliveryclient_test.go

+52-26
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
package deliverclient
1818

1919
import (
20+
"context"
2021
"errors"
2122
"runtime"
2223
"sync"
@@ -259,7 +260,6 @@ func TestDeliverServiceServiceUnavailable(t *testing.T) {
259260
os1 := mocks.NewOrderer(5615, t)
260261
os2 := mocks.NewOrderer(5616, t)
261262

262-
time.Sleep(time.Second)
263263
gossipServiceAdapter := &mocks.MockGossipServiceAdapter{GossipBlockDisseminations: make(chan uint64)}
264264

265265
service, err := NewDeliverService(&Config{
@@ -271,37 +271,63 @@ func TestDeliverServiceServiceUnavailable(t *testing.T) {
271271
})
272272
assert.NoError(t, err)
273273
li := &mocks.MockLedgerInfo{Height: 100}
274-
os1.SetNextExpectedSeek(100)
275-
os2.SetNextExpectedSeek(100)
274+
os1.SetNextExpectedSeek(li.Height)
275+
os2.SetNextExpectedSeek(li.Height)
276276

277277
err = service.StartDeliverForChannel("TEST_CHAINID", li)
278278
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+
}
300290
}
301291

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")
303329
// Ensure the client asks blocks from the other ordering service node
304-
assertBlockDissemination(nextBlockSeek, gossipServiceAdapter.GossipBlockDisseminations, t)
330+
assertBlockDissemination(li.Height, gossipServiceAdapter.GossipBlockDisseminations, t)
305331

306332
// Cleanup
307333
os1.Shutdown()

0 commit comments

Comments
 (0)