Skip to content
This repository has been archived by the owner on Dec 8, 2022. It is now read-only.

Commit

Permalink
Thread safety by using the default source for the RNG (#23)
Browse files Browse the repository at this point in the history
* Changed library to use the default source to be thread safe.

* Adding seed change to the default source.
  • Loading branch information
rdelval authored and agoalofalife committed Apr 20, 2018
1 parent 8c8836d commit 1e3dfd5
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 36 deletions.
6 changes: 2 additions & 4 deletions address.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,8 @@ type Addresser interface {
type Address struct{}

func (i Address) Latitude(v reflect.Value) error {
r := rand.New(src)
kind := v.Kind()
val := (r.Float32() * 180) - 90
val := (rand.Float32() * 180) - 90
if kind == reflect.Float32 {
v.Set(reflect.ValueOf(val))
return nil
Expand All @@ -44,9 +43,8 @@ func (i Address) Latitude(v reflect.Value) error {
}

func (i Address) Longitude(v reflect.Value) error {
r := rand.New(src)
kind := v.Kind()
val := (r.Float32() * 360) - 180
val := (rand.Float32() * 360) - 180

if kind == reflect.Float32 {
v.Set(reflect.ValueOf(val))
Expand Down
46 changes: 24 additions & 22 deletions faker.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"time"
)

var src = rand.NewSource(time.Now().UnixNano())
var mu = &sync.Mutex{}

const (
Expand Down Expand Up @@ -112,6 +111,10 @@ var ErrMoreArguments = "Passed more arguments than is possible : (%d)"

var ErrNotSupportedPointer = "Use sample:=new(%s)\n faker.FakeData(sample) instead"

func init() {
rand.Seed(time.Now().UnixNano())
}

// FakeData is the main function. Will generate a fake data based on your struct. You can use this for automation testing, or anything that need automated data.
// You don't need to Create your own data for your testing.
func FakeData(a interface{}) error {
Expand All @@ -138,7 +141,6 @@ func FakeData(a interface{}) error {
}

func getValue(t reflect.Type) (reflect.Value, error) {
r := rand.New(src)
k := t.Kind()

switch k {
Expand All @@ -155,7 +157,7 @@ func getValue(t reflect.Type) (reflect.Value, error) {

switch t.String() {
case "time.Time":
ft := time.Now().Add(time.Duration(r.Int63()))
ft := time.Now().Add(time.Duration(rand.Int63()))
return reflect.ValueOf(ft), nil
default:
v := reflect.New(t).Elem()
Expand All @@ -182,7 +184,7 @@ func getValue(t reflect.Type) (reflect.Value, error) {
res := randomString(25)
return reflect.ValueOf(res), nil
case reflect.Array, reflect.Slice:
len := r.Intn(100)
len := rand.Intn(100)
v := reflect.MakeSlice(t, len, len)
for i := 0; i < v.Len(); i++ {
val, err := getValue(t.Elem())
Expand All @@ -193,41 +195,41 @@ func getValue(t reflect.Type) (reflect.Value, error) {
}
return v, nil
case reflect.Int:
return reflect.ValueOf(int(r.Intn(100))), nil
return reflect.ValueOf(int(rand.Intn(100))), nil
case reflect.Int8:
return reflect.ValueOf(int8(r.Intn(100))), nil
return reflect.ValueOf(int8(rand.Intn(100))), nil
case reflect.Int16:
return reflect.ValueOf(int16(r.Intn(100))), nil
return reflect.ValueOf(int16(rand.Intn(100))), nil
case reflect.Int32:
return reflect.ValueOf(int32(r.Intn(100))), nil
return reflect.ValueOf(int32(rand.Intn(100))), nil
case reflect.Int64:
return reflect.ValueOf(int64(r.Intn(100))), nil
return reflect.ValueOf(int64(rand.Intn(100))), nil
case reflect.Float32:
return reflect.ValueOf(r.Float32()), nil
return reflect.ValueOf(rand.Float32()), nil
case reflect.Float64:
return reflect.ValueOf(r.Float64()), nil
return reflect.ValueOf(rand.Float64()), nil
case reflect.Bool:
val := r.Intn(2) > 0
val := rand.Intn(2) > 0
return reflect.ValueOf(val), nil

case reflect.Uint:
return reflect.ValueOf(uint(r.Intn(100))), nil
return reflect.ValueOf(uint(rand.Intn(100))), nil

case reflect.Uint8:
return reflect.ValueOf(uint8(r.Intn(100))), nil
return reflect.ValueOf(uint8(rand.Intn(100))), nil

case reflect.Uint16:
return reflect.ValueOf(uint16(r.Intn(100))), nil
return reflect.ValueOf(uint16(rand.Intn(100))), nil

case reflect.Uint32:
return reflect.ValueOf(uint32(r.Intn(100))), nil
return reflect.ValueOf(uint32(rand.Intn(100))), nil

case reflect.Uint64:
return reflect.ValueOf(uint64(r.Intn(100))), nil
return reflect.ValueOf(uint64(rand.Intn(100))), nil

case reflect.Map:
v := reflect.MakeMap(t)
len := r.Intn(100)
len := rand.Intn(100)
for i := 0; i < len; i++ {
key, err := getValue(t.Key())
if err != nil {
Expand Down Expand Up @@ -294,9 +296,9 @@ func userDefinedInt(v reflect.Value, tag string) error {

func randomString(n int) string {
b := make([]byte, n)
for i, cache, remain := n-1, src.Int63(), letterIdxMax; i >= 0; {
for i, cache, remain := n-1, rand.Int63(), letterIdxMax; i >= 0; {
if remain == 0 {
cache, remain = src.Int63(), letterIdxMax
cache, remain = rand.Int63(), letterIdxMax
}
if idx := int(cache & letterIdxMask); idx < len(letterBytes) {
b[i] = letterBytes[idx]
Expand All @@ -315,9 +317,9 @@ func randomElementFromSliceString(s []string) string {
}
func randomStringNumber(n int) string {
b := make([]byte, n)
for i, cache, remain := n-1, src.Int63(), letterIdxMax; i >= 0; {
for i, cache, remain := n-1, rand.Int63(), letterIdxMax; i >= 0; {
if remain == 0 {
cache, remain = src.Int63(), letterIdxMax
cache, remain = rand.Int63(), letterIdxMax
}
if idx := int(cache & letterIdxMask); idx < len(numberBytes) {
b[i] = numberBytes[idx]
Expand Down
9 changes: 3 additions & 6 deletions internet.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,9 @@ func (internet Internet) Email() string {
return randomString(7) + "@" + randomString(5) + "." + randomElementFromSliceString(tld)
}
func (internet Internet) MacAddress() string {
r := rand.New(src)
ip := make([]byte, 6)
for i := 0; i < 6; i++ {
ip[i] = byte(r.Intn(256))
ip[i] = byte(rand.Intn(256))
}

return net.HardwareAddr(ip).String()
Expand All @@ -79,20 +78,18 @@ func (internet Internet) UserName() string {
return randomString(7)
}
func (internet Internet) Ipv4() string {
r := rand.New(src)
size := 4
ip := make([]byte, size)
for i := 0; i < size; i++ {
ip[i] = byte(r.Intn(256))
ip[i] = byte(rand.Intn(256))
}
return net.IP(ip).To4().String()
}
func (internet Internet) Ipv6() string {
r := rand.New(src)
size := 16
ip := make([]byte, size)
for i := 0; i < size; i++ {
ip[i] = byte(r.Intn(256))
ip[i] = byte(rand.Intn(256))
}
return net.IP(ip).To16().String()
}
Expand Down
6 changes: 2 additions & 4 deletions payment.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ type Payment struct{}

func (p Payment) CreditCardType() string {
n := len(creditCards)
r := rand.New(src)
if cacheCreditCard != "" {
return cacheCreditCard
}
Expand All @@ -63,17 +62,16 @@ func (p Payment) CreditCardType() string {
for _, cc := range creditCards {
ccTypes = append(ccTypes, cc.ccType)
}
cacheCreditCard = ccTypes[r.Intn(n)]
cacheCreditCard = ccTypes[rand.Intn(n)]
return cacheCreditCard
}

// CreditCardNum generated credit card number according to the card number rules
func (p Payment) CreditCardNumber() string {
r := rand.New(src)
ccType := strings.ToLower(p.CreditCardType())
cacheCreditCard = ccType
card := creditCards[ccType]
prefix := strconv.Itoa(card.prefixes[r.Intn(len(card.prefixes))])
prefix := strconv.Itoa(card.prefixes[rand.Intn(len(card.prefixes))])

num := prefix
digit := randomStringNumber(card.length - len(prefix))
Expand Down

0 comments on commit 1e3dfd5

Please sign in to comment.