Skip to content

Commit e70ab84

Browse files
C0rWingennadylaventman
authored andcommitted
[FAB-4901]: Harden delivery service unit tests.
In commit 74cc3b, delivery service was extended with timeout assertion to check that client switched over to backup ordering service, while it was a bit too small so client didn't have enough time to switch to alive orderer endpoint. This commit also takes care to fix the bug of failover waiting loop (missed for statement). Moreover adding a few more assertions to enable spot the problem it case of test failure. Change-Id: Ie33f4e6c0ac3998cc790778784d8cb732a333803 Signed-off-by: Artem Barger <[email protected]> Signed-off-by: Gennady Laventman <[email protected]>
1 parent 53bb204 commit e70ab84

File tree

3 files changed

+23
-10
lines changed

3 files changed

+23
-10
lines changed

core/deliverservice/blocksprovider/blocksprovider.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ type blocksProviderImpl struct {
104104

105105
const wrongStatusThreshold = 10
106106

107-
var maxRetryDelay = time.Second * 10
107+
var MaxRetryDelay = time.Second * 10
108108

109109
var logger *logging.Logger // package-level logger
110110

@@ -152,7 +152,7 @@ func (b *blocksProviderImpl) DeliverBlocks() {
152152
errorStatusCounter = 0
153153
logger.Warningf("[%s] Got error %v", b.chainID, t)
154154
}
155-
maxDelay := float64(maxRetryDelay)
155+
maxDelay := float64(MaxRetryDelay)
156156
currDelay := float64(time.Duration(math.Pow(2, float64(statusCounter))) * 100 * time.Millisecond)
157157
time.Sleep(time.Duration(math.Min(maxDelay, currDelay)))
158158
if currDelay < maxDelay {

core/deliverservice/blocksprovider/blocksprovider_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ import (
3232
)
3333

3434
func init() {
35-
maxRetryDelay = time.Second
35+
MaxRetryDelay = time.Second
3636
}
3737

3838
type mockMCS struct {

core/deliverservice/deliveryclient_test.go

+20-7
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package deliverclient
1919
import (
2020
"context"
2121
"errors"
22+
"fmt"
2223
"runtime"
2324
"sync"
2425
"sync/atomic"
@@ -250,6 +251,9 @@ func TestDeliverServiceFailover(t *testing.T) {
250251
}
251252

252253
func TestDeliverServiceServiceUnavailable(t *testing.T) {
254+
orgMaxRetryDelay := blocksprovider.MaxRetryDelay
255+
blocksprovider.MaxRetryDelay = time.Millisecond * 200
256+
defer func() { blocksprovider.MaxRetryDelay = orgMaxRetryDelay }()
253257
defer ensureNoGoroutineLeak(t)()
254258
// Scenario: bring up 2 ordering service instances,
255259
// Make the instance the client connects to fail after a delivery of a block and send SERVICE_UNAVAILABLE
@@ -292,6 +296,10 @@ func TestDeliverServiceServiceUnavailable(t *testing.T) {
292296
activeInstance, backupInstance := waitForConnectionToSomeOSN()
293297
assert.NotNil(t, activeInstance)
294298
assert.NotNil(t, backupInstance)
299+
// Check that delivery client get connected to active
300+
assert.Equal(t, activeInstance.ConnCount(), 1)
301+
// and not connected to backup instances
302+
assert.Equal(t, backupInstance.ConnCount(), 0)
295303

296304
// Send first block
297305
go activeInstance.SendBlock(li.Height)
@@ -306,26 +314,30 @@ func TestDeliverServiceServiceUnavailable(t *testing.T) {
306314

307315
// Fail instance delivery client connected to
308316
activeInstance.Fail()
309-
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
317+
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
310318
defer cancel()
311319

312320
wg := sync.WaitGroup{}
313321
wg.Add(1)
314322

315323
go func(ctx context.Context) {
316324
defer wg.Done()
317-
select {
318-
case <-time.After(time.Millisecond * 100):
319-
if backupInstance.ConnCount() > 0 {
325+
for {
326+
select {
327+
case <-time.After(time.Millisecond * 100):
328+
if backupInstance.ConnCount() > 0 {
329+
return
330+
}
331+
case <-ctx.Done():
320332
return
321333
}
322-
case <-ctx.Done():
323-
return
324334
}
325335
}(ctx)
326336

327337
wg.Wait()
328338
assert.NoError(t, ctx.Err(), "Delivery client has not failed over to alive ordering service")
339+
// Check that delivery client was indeed connected
340+
assert.Equal(t, backupInstance.ConnCount(), 1)
329341
// Ensure the client asks blocks from the other ordering service node
330342
assertBlockDissemination(li.Height, gossipServiceAdapter.GossipBlockDisseminations, t)
331343

@@ -452,7 +464,8 @@ func assertBlockDissemination(expectedSeq uint64, ch chan uint64, t *testing.T)
452464
case seq := <-ch:
453465
assert.Equal(t, expectedSeq, seq)
454466
case <-time.After(time.Second * 5):
455-
assert.Fail(t, "Didn't gossip a new block within a timely manner")
467+
assert.FailNow(t, fmt.Sprintf("Didn't gossip a new block with seq num %d within a timely manner", expectedSeq))
468+
t.Fatal()
456469
}
457470
}
458471

0 commit comments

Comments
 (0)