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