mirror of
https://github.com/google/uuid.git
synced 2024-11-28 08:49:08 +02:00
remove uuid_source
This commit is contained in:
parent
e23e7efa9b
commit
f00b2048a8
@ -1,54 +0,0 @@
|
||||
package uuid
|
||||
|
||||
import (
|
||||
"io"
|
||||
"crypto/rand"
|
||||
)
|
||||
|
||||
// A UuidSource holds a random number generator and generates UUIDs using it as its source.
|
||||
//
|
||||
// It is useful when a process need its own random number generator,
|
||||
// e.g. when running some processes concurrently.
|
||||
type UuidSource struct {
|
||||
rander io.Reader
|
||||
}
|
||||
|
||||
// Creates a new UuidSource which holds its own random number generator.
|
||||
//
|
||||
// Calling NewSource with nil sets the random number generator to a default
|
||||
// generator.
|
||||
func NewSource(r io.Reader) UuidSource {
|
||||
var uuidSource UuidSource
|
||||
uuidSource.SetRand(r)
|
||||
return uuidSource
|
||||
|
||||
}
|
||||
|
||||
// SetRand sets the random number generator of the UuidSource to r, which implements io.Reader.
|
||||
// If r.Read returns an error when the package requests random data then
|
||||
// a panic will be issued.
|
||||
//
|
||||
// Calling SetRand with nil sets the random number generator to a default
|
||||
// generator.
|
||||
func (uuidSource *UuidSource) SetRand(r io.Reader) {
|
||||
if r == nil {
|
||||
uuidSource.rander = rand.Reader
|
||||
return
|
||||
}
|
||||
uuidSource.rander = r
|
||||
}
|
||||
|
||||
// NewRandom returns a Random (Version 4) UUID based on the random number generator in the UuidSource.
|
||||
//
|
||||
// See more detailed explanation here: https://godoc.org/github.com/google/uuid#NewRandom
|
||||
func (uuidSource UuidSource) NewRandom() (UUID, error) {
|
||||
return newRandom(uuidSource.rander)
|
||||
}
|
||||
|
||||
// New creates a new random UUID based on the random number generator in the UuidSource or panics. New is equivalent to
|
||||
// the expression
|
||||
//
|
||||
// uuid.Must(uuid.NewRandom())
|
||||
func (uuidSource UuidSource) New() UUID {
|
||||
return Must(uuidSource.NewRandom())
|
||||
}
|
@ -1,62 +0,0 @@
|
||||
package uuid
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"strings"
|
||||
|
||||
regen "github.com/zach-klippenstein/goregen"
|
||||
)
|
||||
|
||||
func TestUuidSources(t *testing.T) {
|
||||
|
||||
myString, _ := regen.Generate("[a-zA-Z]{1000}")
|
||||
|
||||
// Two identical sources, should give same sequence
|
||||
uuidSourceA := NewSource(strings.NewReader(myString))
|
||||
uuidSourceB := NewSource(strings.NewReader(myString))
|
||||
|
||||
for i := 0; i < 10; i++ {
|
||||
uuid1 := uuidSourceA.New()
|
||||
uuid2 := uuidSourceB.New()
|
||||
if uuid1 != uuid2 {
|
||||
t.Errorf("expected duplicates, got %q and %q", uuid1, uuid2)
|
||||
}
|
||||
}
|
||||
|
||||
// Set rander with nil, each source will be random
|
||||
uuidSourceA.SetRand(nil)
|
||||
uuidSourceB.SetRand(nil)
|
||||
|
||||
for i := 0; i < 10; i++ {
|
||||
uuid1 := uuidSourceA.New()
|
||||
uuid2 := uuidSourceB.New()
|
||||
if uuid1 == uuid2 {
|
||||
t.Errorf("unexpected duplicates, got %q", uuid1)
|
||||
}
|
||||
}
|
||||
|
||||
// Set rander to rand source with same seed, should give same sequence
|
||||
uuidSourceA.SetRand(strings.NewReader(myString))
|
||||
uuidSourceB.SetRand(strings.NewReader(myString))
|
||||
|
||||
for i := 0; i < 10; i++ {
|
||||
uuid1 := uuidSourceA.New()
|
||||
uuid2 := uuidSourceB.New()
|
||||
if uuid1 != uuid2 {
|
||||
t.Errorf("expected duplicates, got %q and %q", uuid1, uuid2)
|
||||
}
|
||||
}
|
||||
|
||||
// Set rander to rand source with different seeds, should not give same sequence
|
||||
uuidSourceA.SetRand(strings.NewReader("456" + myString))
|
||||
uuidSourceB.SetRand(strings.NewReader("myString" + myString))
|
||||
|
||||
for i := 0; i < 10; i++ {
|
||||
uuid1 := uuidSourceA.New()
|
||||
uuid2 := uuidSourceB.New()
|
||||
if uuid1 == uuid2 {
|
||||
t.Errorf("unexpected duplicates, got %q", uuid1)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
23
uuid_test.go
23
uuid_test.go
@ -496,8 +496,27 @@ func TestSetRand(t *testing.T) {
|
||||
if uuid2 != uuid4 {
|
||||
t.Errorf("expected duplicates, got %q and %q", uuid2, uuid4)
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
func TestRandomFromReader(t *testing.T) {
|
||||
myString := "8059ddhdle77cb52"
|
||||
r := bytes.NewReader([]byte(myString))
|
||||
r2 := bytes.NewReader([]byte(myString))
|
||||
uuid1, err := NewRandomFromReader(r)
|
||||
if err != nil {
|
||||
t.Errorf("failed generating UUID from a reader")
|
||||
}
|
||||
_, err = NewRandomFromReader(r)
|
||||
if err == nil {
|
||||
t.Errorf("expecting an error as reader has no more bytes. Got uuid. NewRandomFromReader may not be using the provided reader")
|
||||
}
|
||||
uuid3, err := NewRandomFromReader(r2)
|
||||
if err != nil {
|
||||
t.Errorf("failed generating UUID from a reader")
|
||||
}
|
||||
if uuid1 != uuid3 {
|
||||
t.Errorf("expected duplicates, got %q and %q", uuid1, uuid3)
|
||||
}
|
||||
}
|
||||
|
||||
var asString = "f47ac10b-58cc-0372-8567-0e02b2c3d479"
|
||||
|
@ -27,10 +27,10 @@ func New() UUID {
|
||||
// equivalent to the odds of creating a few tens of trillions of UUIDs in a
|
||||
// year and having one duplicate.
|
||||
func NewRandom() (UUID, error) {
|
||||
return newRandom(rander)
|
||||
return NewRandomFromReader(rander)
|
||||
}
|
||||
|
||||
func newRandom(r io.Reader) (UUID, error) {
|
||||
func NewRandomFromReader(r io.Reader) (UUID, error) {
|
||||
var uuid UUID
|
||||
_, err := io.ReadFull(r, uuid[:])
|
||||
if err != nil {
|
||||
|
Loading…
Reference in New Issue
Block a user