This repository has been archived by the owner on Dec 8, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 147
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added random source customization (#137)
* added random source customization Signed-off-by: Frederic BIDON <[email protected]> * added unit tests Signed-off-by: Frederic BIDON <[email protected]>
- Loading branch information
Showing
14 changed files
with
88 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,6 @@ | ||
package faker | ||
|
||
import ( | ||
"math/rand" | ||
"reflect" | ||
) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,6 @@ package faker | |
|
||
import ( | ||
"fmt" | ||
"math/rand" | ||
"reflect" | ||
"time" | ||
) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,6 @@ package faker | |
|
||
import ( | ||
"fmt" | ||
"math/rand" | ||
"net" | ||
"reflect" | ||
"strings" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,6 @@ package faker | |
|
||
import ( | ||
"fmt" | ||
"math/rand" | ||
"reflect" | ||
"strings" | ||
) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,6 @@ | ||
package faker | ||
|
||
import ( | ||
"math/rand" | ||
"reflect" | ||
"strconv" | ||
"strings" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,6 @@ package faker | |
|
||
import ( | ||
"fmt" | ||
"math/rand" | ||
"reflect" | ||
"strings" | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,6 @@ package faker | |
import ( | ||
"fmt" | ||
"math" | ||
"math/rand" | ||
"reflect" | ||
) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package faker | ||
|
||
import ( | ||
"io" | ||
mathrand "math/rand" | ||
"sync" | ||
) | ||
|
||
var ( | ||
rand *mathrand.Rand | ||
crypto io.Reader | ||
) | ||
|
||
type safeSource struct { | ||
mx sync.Mutex | ||
mathrand.Source | ||
} | ||
|
||
func (s *safeSource) Int63() int64 { | ||
s.mx.Lock() | ||
defer s.mx.Unlock() | ||
|
||
return s.Source.Int63() | ||
} | ||
|
||
// NewSafeSource wraps an unsafe rand.Source with a mutex to guard the random source | ||
// against concurrent access. | ||
func NewSafeSource(in mathrand.Source) mathrand.Source { | ||
return &safeSource{ | ||
Source: in, | ||
} | ||
} | ||
|
||
// SetRandomSource sets a new random source at the package level. | ||
// | ||
// To use a concurrent-safe source, you may wrap it with NewSafeSource, | ||
// e.g. SetRandomSource(NewSafeSource(mysource)). | ||
// | ||
// The default is the global, concurrent-safe source provided by math/rand. | ||
func SetRandomSource(in mathrand.Source) { | ||
rand = mathrand.New(in) | ||
} | ||
|
||
// SetCryptoSource sets a new reader for functions using a cryptographically-safe random generator (e.g. UUID). | ||
// | ||
// The default is the global source provided by crypto/rand. | ||
func SetCryptoSource(in io.Reader) { | ||
crypto = in | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package faker | ||
|
||
import ( | ||
cryptorand "crypto/rand" | ||
"io" | ||
mathrand "math/rand" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestSetRandomSource(t *testing.T) { | ||
SetRandomSource(NewSafeSource(mathrand.NewSource(time.Now().UnixNano()))) | ||
|
||
_ = rand.Int31n(100) | ||
} | ||
|
||
func TestSetCryptoSource(t *testing.T) { | ||
SetCryptoSource(cryptorand.Reader) | ||
|
||
buf := make([]byte, 10) | ||
_, err := io.ReadFull(crypto, buf) | ||
if err != nil { | ||
t.Error("Expected Not Error, But Got: ", err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,6 @@ package faker | |
|
||
import ( | ||
"fmt" | ||
"math/rand" | ||
"strconv" | ||
"strings" | ||
) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters