github.com/koko1123/flow-go-1@v0.29.6/module/mempool/stdmap/transaction_timings_test.go (about)

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