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

Test: Unique concurent transaction ids #807

Merged
merged 1 commit into from
Sep 20, 2023
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
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)
}