aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortalis <talis@il.ibm.com>2019-03-13 22:16:45 +0200
committertalis <talis@il.ibm.com>2019-03-13 22:16:45 +0200
commitf00b2048a800039aa9459130d964406d60b1a92f (patch)
tree8b84d352975ac426d2b34ce5b141a484f31bf037
parente23e7efa9b1c867fef2ff373e512d36343c66dd1 (diff)
downloadgoogle-uuid-f00b2048a800039aa9459130d964406d60b1a92f.tar.gz
remove uuid_source
-rw-r--r--uuid_source.go54
-rw-r--r--uuid_source_test.go62
-rw-r--r--uuid_test.go23
-rw-r--r--version4.go4
4 files changed, 23 insertions, 120 deletions
diff --git a/uuid_source.go b/uuid_source.go
deleted file mode 100644
index 83a569c..0000000
--- a/uuid_source.go
+++ /dev/null
@@ -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())
-}
diff --git a/uuid_source_test.go b/uuid_source_test.go
deleted file mode 100644
index b634c6a..0000000
--- a/uuid_source_test.go
+++ /dev/null
@@ -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)
- }
- }
-
-} \ No newline at end of file
diff --git a/uuid_test.go b/uuid_test.go
index 40f59cb..2a43fb1 100644
--- a/uuid_test.go
+++ b/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"
diff --git a/version4.go b/version4.go
index 66acd62..9ad1aba 100644
--- a/version4.go
+++ b/version4.go
@@ -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 {