Skip to content

Commit

Permalink
Merge pull request #187 from multiversx/dedug_failing_test
Browse files Browse the repository at this point in the history
Fix flaky tests in nonceHandlerV3.
  • Loading branch information
cristure authored Sep 12, 2024
2 parents 397a5d7 + 4346d82 commit e1d80f0
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 13 deletions.
6 changes: 3 additions & 3 deletions interactors/nonceHandlerV3/nonceTransactionsHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ func (nth *nonceTransactionsHandlerV3) filterTransactionsBySenderAddress(transac

// SendTransactions will store and send the provided transaction
func (nth *nonceTransactionsHandlerV3) SendTransactions(ctx context.Context, txs ...*transaction.FrontendTransaction) ([]string, error) {
g, ctx := errgroup.WithContext(ctx)
group := errgroup.Group{}
sentHashes := make([]string, len(txs))
for i, tx := range txs {
if tx == nil {
Expand All @@ -164,7 +164,7 @@ func (nth *nonceTransactionsHandlerV3) SendTransactions(ctx context.Context, txs
}

idx := i
g.Go(func() error {
group.Go(func() error {
sentHash, errSend := anh.SendTransaction(ctx, &txCopy)
if errSend != nil {
return fmt.Errorf("%w while sending transaction for address %s", errSend, addrAsBech32)
Expand All @@ -175,7 +175,7 @@ func (nth *nonceTransactionsHandlerV3) SendTransactions(ctx context.Context, txs
})
}

err := g.Wait()
err := group.Wait()

return sentHashes, err
}
Expand Down
20 changes: 10 additions & 10 deletions interactors/nonceHandlerV3/nonceTransactionsHandler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"sort"
"strconv"
"sync"
"sync/atomic"
"testing"
"time"

Expand Down Expand Up @@ -306,27 +307,27 @@ func TestSendDuplicateNonces(t *testing.T) {
}

wg := sync.WaitGroup{}
var errCount int
var sentCount int
errCount := atomic.Uint32{}
sentCount := atomic.Uint32{}

for i := 0; i < 2; i++ {
wg.Add(1)
go func() {
defer wg.Done()
hashes, sendErr := transactionHandler.SendTransactions(context.Background(), tx)
if sendErr != nil {
errCount++
errCount.Add(1)
}

if hashes[0] != "" {
sentCount++
sentCount.Add(1)
}
}()
}
wg.Wait()

require.Equal(t, 1, errCount)
require.Equal(t, 1, sentCount)
require.Equal(t, uint32(1), errCount.Load())
require.Equal(t, uint32(1), sentCount.Load())
}

func TestSendDuplicateNoncesBatch(t *testing.T) {
Expand All @@ -337,6 +338,7 @@ func TestSendDuplicateNoncesBatch(t *testing.T) {
transactionHandler, err := NewNonceTransactionHandlerV3(createMockArgsNonceTransactionsHandlerV3(&getAccountCalled))
require.NoError(t, err, "failed to create transaction handler")

nonce := uint64(0)
txs := make([]*transaction.FrontendTransaction, 0)
for i := 0; i < 100; i++ {
tx := &transaction.FrontendTransaction{
Expand All @@ -345,17 +347,15 @@ func TestSendDuplicateNoncesBatch(t *testing.T) {
GasLimit: 50000,
ChainID: "T",
Value: "5000000000000000000",
Nonce: 0,
Nonce: nonce,
GasPrice: 1000000000,
Version: 2,
}
txs = append(txs, tx)
}

hashes, err := transactionHandler.SendTransactions(context.Background(), txs...)
for _, h := range hashes {
require.Equal(t, "", h, "a transaction has been sent")
}
require.Contains(t, hashes, strconv.FormatUint(nonce, 10), "no transaction has been sent")
require.Error(t, errors.New("transaction with nonce: 0 has already been scheduled to send while sending transaction for address erd1zptg3eu7uw0qvzhnu009lwxupcn6ntjxptj5gaxt8curhxjqr9tsqpsnht"), err)
}

Expand Down

0 comments on commit e1d80f0

Please sign in to comment.