Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: TestTxPool_ConcurrentTxs #1503

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 30 additions & 30 deletions mempool/cat/pool_test.go
Original file line number Diff line number Diff line change
@@ -480,19 +480,21 @@ func TestTxPool_CheckTxSamePeer(t *testing.T) {
require.Error(t, txmp.CheckTx(tx, nil, mempool.TxInfo{SenderID: peerID}))
}

// TestTxPool_ConcurrentTxs adds a bunch of txs to the txPool (via checkTx) and
// then reaps transactions from the mempool. At the end it asserts that the
// mempool is empty.
func TestTxPool_ConcurrentTxs(t *testing.T) {
txmp := setup(t, 100)
rng := rand.New(rand.NewSource(time.Now().UnixNano()))
cacheSize := 10
txPool := setup(t, cacheSize)
checkTxDone := make(chan struct{})

var wg sync.WaitGroup

wg.Add(1)
go func() {
for i := 0; i < 20; i++ {
_ = checkTxs(t, txmp, 100, 0)
dur := rng.Intn(1000-500) + 500
time.Sleep(time.Duration(dur) * time.Millisecond)
for i := 0; i < 10; i++ {
numTxs := 10
peerID := uint16(0)
_ = checkTxs(t, txPool, numTxs, peerID)
}

wg.Done()
@@ -505,33 +507,18 @@ func TestTxPool_ConcurrentTxs(t *testing.T) {
defer ticker.Stop()
defer wg.Done()

var height int64 = 1

height := int64(1)
for range ticker.C {
reapedTxs := txmp.ReapMaxTxs(200)
reapedTxs := txPool.ReapMaxTxs(50)
if len(reapedTxs) > 0 {
responses := make([]*abci.ResponseDeliverTx, len(reapedTxs))
for i := 0; i < len(responses); i++ {
var code uint32

if i%10 == 0 {
code = 100
} else {
code = abci.CodeTypeOK
}

responses[i] = &abci.ResponseDeliverTx{Code: code}
}

txmp.Lock()
require.NoError(t, txmp.Update(height, reapedTxs, responses, nil, nil))
txmp.Unlock()

responses := generateResponses(len(reapedTxs))
err := txPool.Update(height, reapedTxs, responses, nil, nil)
require.NoError(t, err)
height++
} else {
// only return once we know we finished the CheckTx loop
select {
case <-checkTxDone:
// only return once we know we finished the CheckTx loop
return
default:
}
@@ -540,8 +527,21 @@ func TestTxPool_ConcurrentTxs(t *testing.T) {
}()

wg.Wait()
require.Zero(t, txmp.Size())
require.Zero(t, txmp.SizeBytes())
assert.Zero(t, txPool.Size())
assert.Zero(t, txPool.SizeBytes())
}

func generateResponses(numResponses int) (responses []*abci.ResponseDeliverTx) {
for i := 0; i < numResponses; i++ {
var response *abci.ResponseDeliverTx
if i%2 == 0 {
response = &abci.ResponseDeliverTx{Code: abci.CodeTypeOK}
} else {
response = &abci.ResponseDeliverTx{Code: 100}
}
responses = append(responses, response)
}
return responses
}

func TestTxPool_ExpiredTxs_Timestamp(t *testing.T) {
Loading