github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/fvm/storage/derived/table_invalidator_test.go (about)

     1  package derived
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/require"
     7  
     8  	"github.com/onflow/flow-go/fvm/storage/snapshot"
     9  )
    10  
    11  type testInvalidator struct {
    12  	callCount      int
    13  	invalidateAll  bool
    14  	invalidateName string
    15  }
    16  
    17  func (invalidator testInvalidator) ShouldInvalidateEntries() bool {
    18  	return invalidator.invalidateAll ||
    19  		invalidator.invalidateName != ""
    20  }
    21  
    22  func (invalidator *testInvalidator) ShouldInvalidateEntry(
    23  	key string,
    24  	value *string,
    25  	snapshot *snapshot.ExecutionSnapshot,
    26  ) bool {
    27  	invalidator.callCount += 1
    28  	return invalidator.invalidateAll ||
    29  		invalidator.invalidateName == key
    30  }
    31  
    32  func TestChainedInvalidator(t *testing.T) {
    33  	var chain chainedTableInvalidators[string, *string]
    34  	require.False(t, chain.ShouldInvalidateEntries())
    35  	require.False(t, chain.ShouldInvalidateEntry("", nil, nil))
    36  
    37  	chain = chainedTableInvalidators[string, *string]{}
    38  	require.False(t, chain.ShouldInvalidateEntries())
    39  	require.False(t, chain.ShouldInvalidateEntry("", nil, nil))
    40  
    41  	chain = chainedTableInvalidators[string, *string]{
    42  		{
    43  			TableInvalidator: &testInvalidator{},
    44  			executionTime:    1,
    45  		},
    46  		{
    47  			TableInvalidator: &testInvalidator{},
    48  			executionTime:    2,
    49  		},
    50  		{
    51  			TableInvalidator: &testInvalidator{},
    52  			executionTime:    3,
    53  		},
    54  	}
    55  	require.False(t, chain.ShouldInvalidateEntries())
    56  
    57  	chain = chainedTableInvalidators[string, *string]{
    58  		{
    59  			TableInvalidator: &testInvalidator{invalidateName: "1"},
    60  			executionTime:    1,
    61  		},
    62  		{
    63  			TableInvalidator: &testInvalidator{invalidateName: "3a"},
    64  			executionTime:    3,
    65  		},
    66  		{
    67  			TableInvalidator: &testInvalidator{invalidateName: "3b"},
    68  			executionTime:    3,
    69  		},
    70  		{
    71  			TableInvalidator: &testInvalidator{invalidateName: "7"},
    72  			executionTime:    7,
    73  		},
    74  	}
    75  	require.True(t, chain.ShouldInvalidateEntries())
    76  
    77  	for _, name := range []string{"1", "3a", "3b", "7"} {
    78  		require.True(t, chain.ShouldInvalidateEntry(name, nil, nil))
    79  	}
    80  
    81  	for _, name := range []string{"0", "2", "3c", "4", "8"} {
    82  		require.False(t, chain.ShouldInvalidateEntry(name, nil, nil))
    83  	}
    84  
    85  	require.Equal(t, chain, chain.ApplicableInvalidators(0))
    86  	require.Equal(t, chain, chain.ApplicableInvalidators(1))
    87  	require.Equal(t, chain[1:], chain.ApplicableInvalidators(2))
    88  	require.Equal(t, chain[1:], chain.ApplicableInvalidators(3))
    89  	require.Equal(t, chain[3:], chain.ApplicableInvalidators(4))
    90  	require.Equal(t, chain[3:], chain.ApplicableInvalidators(5))
    91  	require.Equal(t, chain[3:], chain.ApplicableInvalidators(6))
    92  	require.Equal(t, chain[3:], chain.ApplicableInvalidators(7))
    93  	require.Nil(t, chain.ApplicableInvalidators(8))
    94  
    95  	key := "1"
    96  	require.True(t, chain.ShouldInvalidateEntry(key, nil, nil))
    97  	require.False(
    98  		t,
    99  		chain.ApplicableInvalidators(3).ShouldInvalidateEntry(key, nil, nil))
   100  }