github.com/hashgraph/hedera-sdk-go/v2@v2.48.0/network_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  	"time"
    30  
    31  	"github.com/stretchr/testify/require"
    32  )
    33  
    34  func newNetworkMockNodes() map[string]AccountID {
    35  	nodes := make(map[string]AccountID, 2)
    36  	nodes["0.testnet.hedera.com:50211"] = AccountID{0, 0, 3, nil, nil, nil}
    37  	nodes["1.testnet.hedera.com:50211"] = AccountID{0, 0, 4, nil, nil, nil}
    38  	nodes["2.testnet.hedera.com:50211"] = AccountID{0, 0, 5, nil, nil, nil}
    39  	nodes["3.testnet.hedera.com:50211"] = AccountID{0, 0, 6, nil, nil, nil}
    40  	nodes["4.testnet.hedera.com:50211"] = AccountID{0, 0, 7, nil, nil, nil}
    41  	return nodes
    42  }
    43  
    44  func TestUnitNetworkAddressBookGetsSet(t *testing.T) {
    45  	t.Parallel()
    46  
    47  	network := _NewNetwork()
    48  	network._SetTransportSecurity(true)
    49  
    50  	ledgerID, err := LedgerIDFromString("mainnet")
    51  	require.NoError(t, err)
    52  
    53  	network._SetLedgerID(*ledgerID)
    54  	require.NoError(t, err)
    55  
    56  	require.True(t, network.addressBook != nil)
    57  }
    58  
    59  func TestUnitNetworkIncreaseBackoffConcurrent(t *testing.T) {
    60  	t.Parallel()
    61  
    62  	network := _NewNetwork()
    63  	nodes := newNetworkMockNodes()
    64  	err := network.SetNetwork(nodes)
    65  	require.NoError(t, err)
    66  
    67  	node := network._GetNode()
    68  	require.NotNil(t, node)
    69  
    70  	numThreads := 20
    71  	var wg sync.WaitGroup
    72  	wg.Add(numThreads)
    73  	for i := 0; i < numThreads; i++ {
    74  		go func() {
    75  			network._IncreaseBackoff(node)
    76  			wg.Done()
    77  		}()
    78  	}
    79  	wg.Wait()
    80  
    81  	require.Equal(t, len(nodes)-1, len(network.healthyNodes))
    82  }
    83  
    84  func TestUnitConcurrentGetNodeReadmit(t *testing.T) {
    85  	t.Parallel()
    86  
    87  	network := _NewNetwork()
    88  	nodes := newNetworkMockNodes()
    89  	err := network.SetNetwork(nodes)
    90  	network._SetMinNodeReadmitPeriod(0)
    91  	network._SetMaxNodeReadmitPeriod(0)
    92  	require.NoError(t, err)
    93  
    94  	for _, node := range network.nodes {
    95  		node._SetMaxBackoff(-1 * time.Minute)
    96  	}
    97  
    98  	numThreads := 3
    99  	var wg sync.WaitGroup
   100  	wg.Add(numThreads)
   101  	for i := 0; i < numThreads; i++ {
   102  		go func() {
   103  			for i := 0; i < 20; i++ {
   104  				node := network._GetNode()
   105  				network._IncreaseBackoff(node)
   106  			}
   107  			wg.Done()
   108  		}()
   109  	}
   110  	wg.Wait()
   111  	network._ReadmitNodes()
   112  	require.Equal(t, len(nodes), len(network.healthyNodes))
   113  }
   114  
   115  func TestUnitConcurrentNodeAccess(t *testing.T) {
   116  	t.Parallel()
   117  
   118  	network := _NewNetwork()
   119  	nodes := newNetworkMockNodes()
   120  	err := network.SetNetwork(nodes)
   121  	network._SetMinNodeReadmitPeriod(0)
   122  	network._SetMaxNodeReadmitPeriod(0)
   123  	require.NoError(t, err)
   124  
   125  	for _, node := range network.nodes {
   126  		node._SetMaxBackoff(-1 * time.Minute)
   127  	}
   128  
   129  	numThreads := 3
   130  	var wg sync.WaitGroup
   131  	node := network._GetNode()
   132  	wg.Add(numThreads)
   133  	for i := 0; i < numThreads; i++ {
   134  		go func() {
   135  			for i := 0; i < 20; i++ {
   136  				network._GetNode()
   137  				network._IncreaseBackoff(node)
   138  				node._IsHealthy()
   139  				node._GetAttempts()
   140  				node._GetReadmitTime()
   141  				node._Wait()
   142  				node._InUse()
   143  			}
   144  			wg.Done()
   145  		}()
   146  	}
   147  	wg.Wait()
   148  	network._ReadmitNodes()
   149  	require.Equal(t, len(nodes), len(network.healthyNodes))
   150  }
   151  
   152  func TestUnitConcurrentNodeGetChannel(t *testing.T) {
   153  	t.Parallel()
   154  
   155  	network := _NewNetwork()
   156  	nodes := newNetworkMockNodes()
   157  	err := network.SetNetwork(nodes)
   158  	require.NoError(t, err)
   159  
   160  	numThreads := 20
   161  	var wg sync.WaitGroup
   162  	node := network._GetNode()
   163  	wg.Add(numThreads)
   164  	logger := NewLogger("", LoggerLevelError)
   165  	for i := 0; i < numThreads; i++ {
   166  		go func() {
   167  			node._GetChannel(logger)
   168  			wg.Done()
   169  		}()
   170  	}
   171  	wg.Wait()
   172  	network._ReadmitNodes()
   173  	require.Equal(t, len(nodes), len(network.healthyNodes))
   174  }