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

     1  package epochs_test
     2  
     3  import (
     4  	"math/rand"
     5  	"sync"
     6  	"testing"
     7  	"time"
     8  
     9  	"github.com/stretchr/testify/assert"
    10  
    11  	"github.com/koko1123/flow-go-1/model/flow"
    12  	"github.com/koko1123/flow-go-1/module/mempool"
    13  	"github.com/koko1123/flow-go-1/module/mempool/epochs"
    14  	"github.com/koko1123/flow-go-1/module/mempool/herocache"
    15  	"github.com/koko1123/flow-go-1/module/metrics"
    16  	"github.com/koko1123/flow-go-1/utils/unittest"
    17  )
    18  
    19  // subsequent calls to Get should return the same transaction pool
    20  func TestConsistency(t *testing.T) {
    21  
    22  	create := func(_ uint64) mempool.Transactions {
    23  		return herocache.NewTransactions(100, unittest.Logger(), metrics.NewNoopCollector())
    24  	}
    25  	pools := epochs.NewTransactionPools(create)
    26  	epoch := rand.Uint64()
    27  
    28  	pool := pools.ForEpoch(epoch)
    29  	assert.Equal(t, pool, pools.ForEpoch(epoch))
    30  }
    31  
    32  // test that different epochs don't interfere, also test concurrent access
    33  func TestMultipleEpochs(t *testing.T) {
    34  
    35  	create := func(_ uint64) mempool.Transactions {
    36  		return herocache.NewTransactions(100, unittest.Logger(), metrics.NewNoopCollector())
    37  	}
    38  	pools := epochs.NewTransactionPools(create)
    39  
    40  	var wg sync.WaitGroup
    41  	for i := 0; i < 10; i++ {
    42  		wg.Add(1)
    43  		go func() {
    44  			defer wg.Done()
    45  			epoch := rand.Uint64()
    46  
    47  			var transactions []*flow.TransactionBody
    48  			for i := 0; i < 10; i++ {
    49  				pool := pools.ForEpoch(epoch)
    50  				assert.Equal(t, uint(len(transactions)), pool.Size())
    51  				for _, tx := range transactions {
    52  					assert.True(t, pool.Has(tx.ID()))
    53  				}
    54  
    55  				tx := unittest.TransactionBodyFixture()
    56  				transactions = append(transactions, &tx)
    57  				pool.Add(&tx)
    58  			}
    59  		}()
    60  	}
    61  	unittest.AssertReturnsBefore(t, wg.Wait, time.Second)
    62  }
    63  
    64  func TestCombinedSize(t *testing.T) {
    65  
    66  	create := func(_ uint64) mempool.Transactions {
    67  		return herocache.NewTransactions(100, unittest.Logger(), metrics.NewNoopCollector())
    68  	}
    69  	pools := epochs.NewTransactionPools(create)
    70  
    71  	nEpochs := rand.Uint64() % 10
    72  	transactionsPerEpoch := rand.Uint64() % 10
    73  	expected := uint(nEpochs * transactionsPerEpoch)
    74  
    75  	for epoch := uint64(0); epoch < nEpochs; epoch++ {
    76  		pool := pools.ForEpoch(epoch)
    77  		for i := 0; i < int(transactionsPerEpoch); i++ {
    78  			next := unittest.TransactionBodyFixture()
    79  			pool.Add(&next)
    80  		}
    81  	}
    82  
    83  	assert.Equal(t, expected, pools.CombinedSize())
    84  }