github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/state/protocol/events/gadgets/heights_test.go (about)

     1  package gadgets
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/assert"
     7  
     8  	"github.com/onflow/flow-go/utils/unittest"
     9  )
    10  
    11  func TestHeights(t *testing.T) {
    12  	heights := NewHeights()
    13  
    14  	calls := 0
    15  
    16  	bad := func() { t.Fail() } // should not be called
    17  	good := func() { calls++ } // should be called
    18  
    19  	// we will start finalizing at 2, and finalize through 4. Registered
    20  	// heights 2-4 should be invoked, 1 and 5 should not be
    21  
    22  	heights.OnHeight(1, bad) // we start finalizing after block 1
    23  	heights.OnHeight(2, good)
    24  	heights.OnHeight(2, good) // register 2 callbacks for height 2
    25  	heights.OnHeight(4, good)
    26  	heights.OnHeight(5, bad) // we won't finalize block 5
    27  
    28  	for height := uint64(2); height <= 4; height++ {
    29  		block := unittest.BlockHeaderFixture()
    30  		block.Height = height
    31  		heights.BlockFinalized(block)
    32  	}
    33  
    34  	// ensure callbacks were invoked correctly
    35  	assert.Equal(t, 3, calls)
    36  
    37  	// ensure map is cleared appropriately (only height 5 should remain)
    38  	assert.Equal(t, 1, len(heights.heights))
    39  }