From 92c2e13cf0c6d451854162a02842b4b6960c94c8 Mon Sep 17 00:00:00 2001 From: Tuan Tran Date: Sat, 21 Dec 2024 00:04:46 +0700 Subject: [PATCH 1/2] fix flaky test --- mempool/cat/pool_test.go | 45 ++++++++++++++++++++++------------------ 1 file changed, 25 insertions(+), 20 deletions(-) diff --git a/mempool/cat/pool_test.go b/mempool/cat/pool_test.go index 03d917dcd3..46253eade5 100644 --- a/mempool/cat/pool_test.go +++ b/mempool/cat/pool_test.go @@ -563,30 +563,35 @@ func TestTxPool_ExpiredTxs_Timestamp(t *testing.T) { // // The exact intervals are not important except that the delta should be // large relative to the cost of CheckTx (ms vs. ns is fine here). - time.Sleep(3 * time.Millisecond) + time.Sleep(2500 * time.Microsecond) added2 := checkTxs(t, txmp, 10, 1) - // Wait a while longer, so that the first batch will expire. - time.Sleep(3 * time.Millisecond) - - // Trigger an update so that pruning will occur. - txmp.Lock() - defer txmp.Unlock() - require.NoError(t, txmp.Update(txmp.height+1, nil, nil, nil, nil)) - - // All the transactions in the original set should have been purged. - for _, tx := range added1 { - // Check that it was added to the evictedTxCache - evicted := txmp.WasRecentlyEvicted(tx.tx.Key()) - require.True(t, evicted) + // use require.Eventually to wait for the TTL to expire + require.Eventually(t, func() bool { + // Trigger an update so that pruning will occur. + txmp.Lock() + defer txmp.Unlock() + require.NoError(t, txmp.Update(txmp.height+1, nil, nil, nil, nil)) + + // All the transactions in the original set should have been purged. + for _, tx := range added1 { + // Check that it was added to the evictedTxCache + evicted := txmp.WasRecentlyEvicted(tx.tx.Key()) + if !evicted { + return false + } - if txmp.store.has(tx.tx.Key()) { - t.Errorf("Transaction %X should have been purged for TTL", tx.tx.Key()) - } - if txmp.rejectedTxCache.Has(tx.tx.Key()) { - t.Errorf("Transaction %X should have been removed from the cache", tx.tx.Key()) + if txmp.store.has(tx.tx.Key()) { + t.Errorf("Transaction %X should have been purged for TTL", tx.tx.Key()) + return false + } + if txmp.rejectedTxCache.Has(tx.tx.Key()) { + t.Errorf("Transaction %X should have been removed from the cache", tx.tx.Key()) + return false + } } - } + return true + }, 10*time.Millisecond, 50*time.Microsecond) // All the transactions added later should still be around. for _, tx := range added2 { From 1ad808dcfab61672695e172a8ef04ebb60042837 Mon Sep 17 00:00:00 2001 From: Tuan Tran Date: Sun, 22 Dec 2024 08:54:58 +0700 Subject: [PATCH 2/2] fix comment --- mempool/cat/pool_test.go | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/mempool/cat/pool_test.go b/mempool/cat/pool_test.go index 46253eade5..d37aad90d0 100644 --- a/mempool/cat/pool_test.go +++ b/mempool/cat/pool_test.go @@ -553,16 +553,11 @@ func TestTxPool_ExpiredTxs_Timestamp(t *testing.T) { // Wait a while, then add some more transactions that should not be expired // when the first batch TTLs out. - // - // ms: 0 1 2 3 4 5 6 - // ^ ^ ^ ^ - // | | | +-- Update (triggers pruning) - // | | +------ first batch expires - // | +-------------- second batch added - // +-------------------------- first batch added - // - // The exact intervals are not important except that the delta should be - // large relative to the cost of CheckTx (ms vs. ns is fine here). + // Because the TTL is 5ms which is very short, we need to have a more precise + // pruning interval to ensure that the transactions are expired + // so that the expired event is caught quickly enough + // that the second batch of transactions are not expired. + time.Sleep(2500 * time.Microsecond) added2 := checkTxs(t, txmp, 10, 1)