github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/module/mempool/stdmap/transaction_timings_test.go (about)

     1  package stdmap_test
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  	"github.com/stretchr/testify/require"
     9  
    10  	"github.com/onflow/flow-go/model/flow"
    11  	"github.com/onflow/flow-go/module/mempool/stdmap"
    12  	"github.com/onflow/flow-go/utils/unittest"
    13  )
    14  
    15  func TestTransactionTimingsPool(t *testing.T) {
    16  	item1 := &flow.TransactionTiming{TransactionID: unittest.IdentifierFixture(),
    17  		Received: time.Now().Add(-10 * time.Second), Executed: time.Now()}
    18  	item2 := &flow.TransactionTiming{TransactionID: unittest.IdentifierFixture(), Received: time.Now()}
    19  
    20  	pool, err := stdmap.NewTransactionTimings(1000)
    21  	require.NoError(t, err)
    22  
    23  	t.Run("should be able to add first", func(t *testing.T) {
    24  		added := pool.Add(item1)
    25  		assert.True(t, added)
    26  	})
    27  
    28  	t.Run("should be able to add second", func(t *testing.T) {
    29  		added := pool.Add(item2)
    30  		assert.True(t, added)
    31  	})
    32  
    33  	t.Run("should be able to get size", func(t *testing.T) {
    34  		size := pool.Size()
    35  		assert.EqualValues(t, 2, size)
    36  	})
    37  
    38  	t.Run("should be able to adjust the first", func(t *testing.T) {
    39  		finalized := time.Now()
    40  		entity, updated := pool.Adjust(item1.ID(), func(t *flow.TransactionTiming) *flow.TransactionTiming {
    41  			t.Finalized = finalized
    42  			return t
    43  		})
    44  		assert.True(t, updated)
    45  		assert.Equal(t, finalized, entity.Finalized)
    46  	})
    47  
    48  	t.Run("should be able to get first", func(t *testing.T) {
    49  		got, exists := pool.ByID(item1.ID())
    50  		assert.True(t, exists)
    51  		assert.Equal(t, item1, got)
    52  	})
    53  
    54  	t.Run("should be able to remove second", func(t *testing.T) {
    55  		ok := pool.Remove(item2.ID())
    56  		assert.True(t, ok)
    57  	})
    58  
    59  	t.Run("should be able to retrieve all", func(t *testing.T) {
    60  		items := pool.All()
    61  		assert.Len(t, items, 1)
    62  		assert.Equal(t, item1, items[0])
    63  	})
    64  
    65  	t.Run("should not panic if item does not exist yet", func(t *testing.T) {
    66  		entity, updated := pool.Adjust(unittest.IdentifierFixture(), func(tt *flow.TransactionTiming) *flow.TransactionTiming {
    67  			assert.Fail(t, "should not have found this item")
    68  			return tt
    69  		})
    70  		assert.False(t, updated)
    71  		assert.Nil(t, entity)
    72  	})
    73  }