github.com/ronaksoft/rony@v0.16.26-0.20230807065236-1743dbfe6959/tools/random_test.go (about)

     1  package tools_test
     2  
     3  import (
     4  	"sync"
     5  	"testing"
     6  
     7  	"github.com/ronaksoft/rony/tools"
     8  	. "github.com/smartystreets/goconvey/convey"
     9  )
    10  
    11  /*
    12     Creation Time: 2021 - Jan - 01
    13     Created by:  (ehsan)
    14     Maintainers:
    15        1.  Ehsan N. Moosa (E2)
    16     Auditor: Ehsan N. Moosa (E2)
    17     Copyright Ronak Software Group 2020
    18  */
    19  
    20  func BenchmarkRandomInt64(b *testing.B) {
    21  	b.ReportAllocs()
    22  	b.RunParallel(func(pb *testing.PB) {
    23  		for pb.Next() {
    24  			_ = tools.RandomInt64(0)
    25  		}
    26  	})
    27  }
    28  
    29  func BenchmarkRandomID(b *testing.B) {
    30  	b.ReportAllocs()
    31  	b.RunParallel(func(pb *testing.PB) {
    32  		for pb.Next() {
    33  			_ = tools.RandomID(10)
    34  		}
    35  	})
    36  }
    37  
    38  func TestSecureRandomInt63(t *testing.T) {
    39  	t.Parallel()
    40  	Convey("SecureRandom", t, func(c C) {
    41  		size := 10000
    42  		mtx := tools.SpinLock{}
    43  		wg := sync.WaitGroup{}
    44  		m := make(map[int64]struct{}, size)
    45  		for i := 0; i < size; i++ {
    46  			wg.Add(1)
    47  			go func() {
    48  				defer wg.Done()
    49  				mtx.Lock()
    50  				x := tools.SecureRandomInt63(0)
    51  				_, ok := m[x]
    52  				c.So(ok, ShouldBeFalse)
    53  				m[x] = struct{}{}
    54  				mtx.Unlock()
    55  			}()
    56  		}
    57  		wg.Wait()
    58  	})
    59  }