github.com/koko1123/flow-go-1@v0.29.6/engine/consensus/approvals/caches_test.go (about)

     1  package approvals
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/require"
     7  
     8  	"github.com/koko1123/flow-go-1/model/flow"
     9  	"github.com/koko1123/flow-go-1/utils/unittest"
    10  )
    11  
    12  // TestApprovalsCache_Get_Put_All tests common use cases for approvals cache.
    13  func TestApprovalsCache_Get_Put_All(t *testing.T) {
    14  	numElements := uint(10)
    15  	cache := NewApprovalsCache(numElements)
    16  	approvals := make([]*flow.ResultApproval, numElements)
    17  	for i := range approvals {
    18  		approval := unittest.ResultApprovalFixture()
    19  		approvals[i] = approval
    20  		require.True(t, cache.Put(approval.Body.PartialID(), approval))
    21  		require.Equal(t, approval, cache.Get(approval.Body.PartialID()))
    22  	}
    23  	require.ElementsMatch(t, approvals, cache.All())
    24  	require.Len(t, cache.All(), int(numElements))
    25  }
    26  
    27  // TestIncorporatedResultsCache_Get_Put_All tests common use cases for incorporated results cache.
    28  func TestIncorporatedResultsCache_Get_Put_All(t *testing.T) {
    29  	numElements := uint(10)
    30  	cache := NewIncorporatedResultsCache(numElements)
    31  	results := make([]*flow.IncorporatedResult, numElements)
    32  	for i := range results {
    33  		result := unittest.IncorporatedResult.Fixture()
    34  		results[i] = result
    35  		require.True(t, cache.Put(result.ID(), result))
    36  		require.Equal(t, result, cache.Get(result.ID()))
    37  	}
    38  	require.ElementsMatch(t, results, cache.All())
    39  	require.Equal(t, int(numElements), len(cache.All()))
    40  }