github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/engine/consensus/approvals/caching_assignment_collector_test.go (about)

     1  package approvals
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/require"
     7  	"github.com/stretchr/testify/suite"
     8  
     9  	"github.com/onflow/flow-go/engine"
    10  	"github.com/onflow/flow-go/model/flow"
    11  	"github.com/onflow/flow-go/utils/unittest"
    12  )
    13  
    14  // CachingAssignmentCollectorTestSuite is a test suite for testing CachingAssignmentCollector. Contains a minimal set of
    15  // helper mocks to test the behavior.
    16  type CachingAssignmentCollectorTestSuite struct {
    17  	suite.Suite
    18  
    19  	executedBlock *flow.Header
    20  	result        *flow.ExecutionResult
    21  	collector     *CachingAssignmentCollector
    22  }
    23  
    24  func TestCachingAssignmentCollector(t *testing.T) {
    25  	suite.Run(t, new(CachingAssignmentCollectorTestSuite))
    26  }
    27  
    28  func (s *CachingAssignmentCollectorTestSuite) SetupTest() {
    29  	s.executedBlock = unittest.BlockHeaderFixture()
    30  	s.result = unittest.ExecutionResultFixture(func(result *flow.ExecutionResult) {
    31  		result.BlockID = s.executedBlock.ID()
    32  	})
    33  	s.collector = NewCachingAssignmentCollector(AssignmentCollectorBase{
    34  		executedBlock: s.executedBlock,
    35  		result:        s.result,
    36  		resultID:      s.result.ID(),
    37  	})
    38  }
    39  
    40  // TestCheckEmergencySealing tests that emergency sealing is no-op
    41  func (s *CachingAssignmentCollectorTestSuite) TestCheckEmergencySealing() {
    42  	// should be no-op
    43  	err := s.collector.CheckEmergencySealing(nil, 0)
    44  	require.NoError(s.T(), err)
    45  }
    46  
    47  // TestProcessApproval tests that collector caches approval when requested to process it
    48  func (s *CachingAssignmentCollectorTestSuite) TestProcessApproval() {
    49  	approval := unittest.ResultApprovalFixture()
    50  	err := s.collector.ProcessApproval(approval)
    51  	require.Error(s.T(), err)
    52  
    53  	approval = unittest.ResultApprovalFixture(unittest.WithExecutionResultID(s.result.ID()))
    54  	err = s.collector.ProcessApproval(approval)
    55  	require.Error(s.T(), err)
    56  	require.True(s.T(), engine.IsInvalidInputError(err))
    57  
    58  	var expected []*flow.ResultApproval
    59  	for i := 0; i < 5; i++ {
    60  		approval := unittest.ResultApprovalFixture(
    61  			unittest.WithBlockID(s.executedBlock.ID()),
    62  			unittest.WithExecutionResultID(s.result.ID()),
    63  			unittest.WithChunk(uint64(i)))
    64  		err := s.collector.ProcessApproval(approval)
    65  		require.NoError(s.T(), err)
    66  		expected = append(expected, approval)
    67  	}
    68  	require.ElementsMatch(s.T(), expected, s.collector.GetApprovals())
    69  }
    70  
    71  // TestProcessIncorporatedResult tests that collector caches result when requested to processes flow.IncorporatedResult
    72  func (s *CachingAssignmentCollectorTestSuite) TestProcessIncorporatedResult() {
    73  	// processing invalid result should error
    74  	err := s.collector.ProcessIncorporatedResult(unittest.IncorporatedResult.Fixture(
    75  		unittest.IncorporatedResult.WithResult(unittest.ExecutionResultFixture()),
    76  	))
    77  	require.Error(s.T(), err)
    78  
    79  	// processing valid IR should result in no error
    80  	var expected []*flow.IncorporatedResult
    81  	for i := 0; i < 5; i++ {
    82  		IR := unittest.IncorporatedResult.Fixture(
    83  			unittest.IncorporatedResult.WithResult(s.result),
    84  		)
    85  		err := s.collector.ProcessIncorporatedResult(IR)
    86  		require.NoError(s.T(), err)
    87  		expected = append(expected, IR)
    88  	}
    89  
    90  	require.ElementsMatch(s.T(), expected, s.collector.GetIncorporatedResults())
    91  }
    92  
    93  func (s *CachingAssignmentCollectorTestSuite) TestProcessingStatus() {
    94  	require.Equal(s.T(), CachingApprovals, s.collector.ProcessingStatus())
    95  }
    96  
    97  // TestRequestMissingApprovals tests that requesting missing approvals is no-op
    98  func (s *CachingAssignmentCollectorTestSuite) TestRequestMissingApprovals() {
    99  	// should be no-op
   100  	_, err := s.collector.RequestMissingApprovals(nil, 0)
   101  	require.NoError(s.T(), err)
   102  }