Skip to content

Commit

Permalink
Test: Unique concurent transaction ids
Browse files Browse the repository at this point in the history
Signed-off-by: Emanuel Pargov <[email protected]>
  • Loading branch information
bamzedev committed Sep 20, 2023
1 parent 3ed558f commit 2b2d9f8
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions transaction_id_unit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ package hedera
*/

import (
"sync"
"testing"

"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -69,3 +70,35 @@ func TestUnitTransactionIDFromStringTrimmedZeroes(t *testing.T) {
require.NoError(t, err)
require.Equal(t, txID.String(), "[email protected]")
}

func TestUnitConcurrentTransactionIDsAreUnique(t *testing.T) {
const numOfTxns = 100000

account := AccountID{Account: 1}

// Channel to collect generated transaction IDs
idsCh := make(chan TransactionID, numOfTxns)

var wg sync.WaitGroup
for i := 0; i < numOfTxns; i++ {
wg.Add(1)
go func() {
defer wg.Done()
idsCh <- TransactionIDGenerate(account)
}()
}

// Close idsCh after all goroutines complete
go func() {
wg.Wait()
close(idsCh)
}()

seen := make(map[TransactionID]bool)
for id := range idsCh {
require.False(t, seen[id], "Transaction ID %v is not unique", id)
seen[id] = true
}

require.Equal(t, len(seen), numOfTxns)
}

0 comments on commit 2b2d9f8

Please sign in to comment.