github.com/Finschia/finschia-sdk@v0.48.1/types/address_race_test.go (about)

     1  package types_test
     2  
     3  import (
     4  	"encoding/binary"
     5  	"fmt"
     6  	"testing"
     7  	"time"
     8  
     9  	"github.com/Finschia/finschia-sdk/types"
    10  	"github.com/stretchr/testify/require"
    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  	fmt.Println("starting test")
    36  	if testing.Short() {
    37  		s.T().Skip("AddressRace test is not short")
    38  	}
    39  	workers := 4
    40  	done := make(chan bool, workers)
    41  	cancel := make(chan bool)
    42  	for i := byte(1); i <= 2; i++ { // workes which will loop in first 100 addresses
    43  		go addressStringCaller(s.Require(), i, 100, cancel, done)
    44  	}
    45  	for i := byte(1); i <= 2; i++ { // workes which will generate 1e6 new addresses
    46  		go addressStringCaller(s.Require(), i, 1000000, cancel, done)
    47  	}
    48  	<-time.After(time.Millisecond * 30)
    49  	close(cancel)
    50  
    51  	// cleanup
    52  	for i := 0; i < 4; i++ {
    53  		<-done
    54  	}
    55  }