github.com/hashgraph/hedera-sdk-go/v2@v2.48.0/transaction_id_unit_test.go (about)

     1  //go:build all || unit
     2  // +build all unit
     3  
     4  package hedera
     5  
     6  /*-
     7   *
     8   * Hedera Go SDK
     9   *
    10   * Copyright (C) 2020 - 2024 Hedera Hashgraph, LLC
    11   *
    12   * Licensed under the Apache License, Version 2.0 (the "License");
    13   * you may not use this file except in compliance with the License.
    14   * You may obtain a copy of the License at
    15   *
    16   *      http://www.apache.org/licenses/LICENSE-2.0
    17   *
    18   * Unless required by applicable law or agreed to in writing, software
    19   * distributed under the License is distributed on an "AS IS" BASIS,
    20   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    21   * See the License for the specific language governing permissions and
    22   * limitations under the License.
    23   *
    24   */
    25  
    26  import (
    27  	"sync"
    28  	"testing"
    29  
    30  	"github.com/stretchr/testify/require"
    31  )
    32  
    33  func TestUnitTransactionID(t *testing.T) {
    34  	t.Parallel()
    35  
    36  	txID := TransactionIDGenerate(AccountID{0, 0, 3, nil, nil, nil})
    37  	txID = txID.SetScheduled(true)
    38  }
    39  
    40  func TestUnitTransactionIDFromString(t *testing.T) {
    41  	t.Parallel()
    42  
    43  	txID, err := TransactionIdFromString("0.0.3@1614997926.774912965?scheduled")
    44  	require.NoError(t, err)
    45  	require.Equal(t, txID.AccountID.String(), "0.0.3")
    46  	require.True(t, txID.scheduled)
    47  }
    48  
    49  func TestUnitTransactionIDFromStringNonce(t *testing.T) {
    50  	t.Parallel()
    51  
    52  	txID, err := TransactionIdFromString("0.0.3@1614997926.774912965?scheduled/4")
    53  	require.NoError(t, err)
    54  	require.Equal(t, *txID.Nonce, int32(4))
    55  	require.Equal(t, txID.AccountID.String(), "0.0.3")
    56  }
    57  
    58  func TestUnitTransactionIDFromStringLeadingZero(t *testing.T) {
    59  	t.Parallel()
    60  
    61  	txID, err := TransactionIdFromString("0.0.3@1614997926.074912965")
    62  	require.NoError(t, err)
    63  	require.Equal(t, txID.String(), "0.0.3@1614997926.074912965")
    64  }
    65  
    66  func TestUnitTransactionIDFromStringTrimmedZeroes(t *testing.T) {
    67  	t.Parallel()
    68  
    69  	txID, err := TransactionIdFromString("0.0.3@1614997926.5")
    70  	require.NoError(t, err)
    71  	require.Equal(t, txID.String(), "0.0.3@1614997926.000000005")
    72  }
    73  
    74  func TestUnitConcurrentTransactionIDsAreUnique(t *testing.T) {
    75  	const numOfTxns = 100000
    76  
    77  	account := AccountID{Account: 1}
    78  
    79  	// Channel to collect generated transaction IDs
    80  	idsCh := make(chan TransactionID, numOfTxns)
    81  
    82  	var wg sync.WaitGroup
    83  	for i := 0; i < numOfTxns; i++ {
    84  		wg.Add(1)
    85  		go func() {
    86  			defer wg.Done()
    87  			idsCh <- TransactionIDGenerate(account)
    88  		}()
    89  	}
    90  
    91  	// Close idsCh after all goroutines complete
    92  	go func() {
    93  		wg.Wait()
    94  		close(idsCh)
    95  	}()
    96  
    97  	seen := make(map[TransactionID]bool)
    98  	for id := range idsCh {
    99  		require.False(t, seen[id], "Transaction ID %v is not unique", id)
   100  		seen[id] = true
   101  	}
   102  
   103  	require.Equal(t, len(seen), numOfTxns)
   104  }