github.com/cosmos/cosmos-sdk@v0.50.10/types/address_race_test.go (about)

     1  package types_test
     2  
     3  import (
     4  	"encoding/binary"
     5  	"testing"
     6  	"time"
     7  
     8  	"github.com/stretchr/testify/require"
     9  
    10  	"github.com/cosmos/cosmos-sdk/types"
    11  )
    12  
    13  // generates AccAddress with `prefix` and calls String method
    14  func addressStringCaller(require *require.Assertions, prefix byte, max uint32, cancel chan bool, done chan<- bool) {
    15  	bz := make([]byte, 5) // prefix + 4 bytes for uint
    16  	bz[0] = prefix
    17  	for i := uint32(0); ; i++ {
    18  		if i >= max {
    19  			i = 0
    20  		}
    21  		select {
    22  		case <-cancel:
    23  			done <- true
    24  			return
    25  		default:
    26  			binary.BigEndian.PutUint32(bz[1:], i)
    27  			str := types.AccAddress(bz).String()
    28  			require.True(str != "")
    29  		}
    30  
    31  	}
    32  }
    33  
    34  func (s *addressTestSuite) TestAddressRace() {
    35  	if testing.Short() {
    36  		s.T().Skip("AddressRace test is not short")
    37  	}
    38  
    39  	workers := 4
    40  	done := make(chan bool, workers)
    41  	cancel := make(chan bool)
    42  
    43  	for i := byte(1); i <= 2; i++ { // workes which will loop in first 100 addresses
    44  		go addressStringCaller(s.Require(), i, 100, cancel, done)
    45  	}
    46  
    47  	for i := byte(1); i <= 2; i++ { // workes which will generate 1e6 new addresses
    48  		go addressStringCaller(s.Require(), i, 1000000, cancel, done)
    49  	}
    50  
    51  	<-time.After(time.Millisecond * 30)
    52  	close(cancel)
    53  
    54  	// cleanup
    55  	for i := 0; i < 4; i++ {
    56  		<-done
    57  	}
    58  }