From fc5138ba7876a733d70ee8118894bb1c3d3231db Mon Sep 17 00:00:00 2001 From: Ron Date: Tue, 11 Jun 2019 18:57:06 +0300 Subject: [PATCH] added test to cover the case where both petitioner and responder have no blocks --- .../test/block_sync_server_test.go | 35 ++++++++++++++++--- services/blockstorage/test/harness.go | 5 +++ 2 files changed, 35 insertions(+), 5 deletions(-) diff --git a/services/blockstorage/test/block_sync_server_test.go b/services/blockstorage/test/block_sync_server_test.go index 45498ed7b..24c5d5595 100644 --- a/services/blockstorage/test/block_sync_server_test.go +++ b/services/blockstorage/test/block_sync_server_test.go @@ -65,13 +65,14 @@ func TestSourceRespondToAvailabilityRequests(t *testing.T) { }) } -func TestSourceDoesNotRespondToAvailabilityRequestIfSourceIsBehindPetitioner(t *testing.T) { +func TestSourceDoesNotRespondToAvailabilityRequestIfSourceIsNotAheadOfPetitioner(t *testing.T) { test.WithContext(func(ctx context.Context) { harness := newBlockStorageHarness(t). withSyncBroadcast(1). expectValidateConsensusAlgos(). start(ctx) - harness.commitBlock(ctx, builders.BlockPair().WithHeight(primitives.BlockHeight(1)).Build()) + + _, _ = harness.commitBlock(ctx, builders.BlockPair().WithHeight(primitives.BlockHeight(1)).Build()) harness.gossip.Never("SendBlockAvailabilityResponse", mock.Any, mock.Any) @@ -79,7 +80,29 @@ func TestSourceDoesNotRespondToAvailabilityRequestIfSourceIsBehindPetitioner(t * _, err := harness.blockStorage.HandleBlockAvailabilityRequest(ctx, msg) require.NoError(t, err, "expecting a happy flow (without sending the response)") - harness.verifyMocks(t, 1) + + harness.verifyMocks(t, 1) // eventually + harness.verifyMocksConsistently(t, 1) + + }) +} + +func TestSourceDoesNotRespondToAvailabilityRequestIfBothAreAtZero(t *testing.T) { + test.WithContext(func(ctx context.Context) { + harness := newBlockStorageHarness(t). + withSyncBroadcast(1). + expectValidateConsensusAlgos(). + start(ctx) + + harness.gossip.Never("SendBlockAvailabilityResponse", mock.Any, mock.Any) + + msg := builders.BlockAvailabilityRequestInput().WithLastCommittedBlockHeight(primitives.BlockHeight(0)).Build() + _, err := harness.blockStorage.HandleBlockAvailabilityRequest(ctx, msg) + + require.NoError(t, err, "expecting a happy flow (without sending the response)") + + harness.verifyMocks(t, 1) // eventually + harness.verifyMocksConsistently(t, 1) }) } @@ -143,7 +166,7 @@ func TestSourceRespondsWithChunks(t *testing.T) { } harness.gossip.When("SendBlockSyncResponse", mock.Any, mock.AnyIf("response should hold correct blocks", chunksResponseVerifier)).Return(nil, nil).Times(1) - harness.blockStorage.HandleBlockSyncRequest(ctx, msg) + _, _ = harness.blockStorage.HandleBlockSyncRequest(ctx, msg) harness.verifyMocks(t, 1) }) } @@ -170,6 +193,8 @@ func TestSourceIgnoresBlockSyncRequestIfSourceIsBehind(t *testing.T) { _, err := harness.blockStorage.HandleBlockSyncRequest(ctx, msg) require.Error(t, err, "expected source to return an error") - harness.verifyMocks(t, 1) + + harness.verifyMocks(t, 1) // eventually + harness.verifyMocksConsistently(t, 1) }) } diff --git a/services/blockstorage/test/harness.go b/services/blockstorage/test/harness.go index bcb8d2320..462483445 100644 --- a/services/blockstorage/test/harness.go +++ b/services/blockstorage/test/harness.go @@ -116,6 +116,11 @@ func (d *harness) expectCommitStateDiffTimes(times int) { d.stateStorage.When("CommitStateDiff", mock.Any, mock.Any).Return(csdOut, nil).Times(times) } +func (d *harness) verifyMocksConsistently(t *testing.T, times int) { + err := test.ConsistentlyVerify(test.EVENTUALLY_ACCEPTANCE_TIMEOUT*time.Duration(times), d.gossip, d.stateStorage, d.consensus) + require.NoError(t, err) +} + func (d *harness) verifyMocks(t *testing.T, times int) { err := test.EventuallyVerify(test.EVENTUALLY_ACCEPTANCE_TIMEOUT*time.Duration(times), d.gossip, d.stateStorage, d.consensus) require.NoError(t, err)