Skip to content

Commit

Permalink
Merge pull request #44 from trabetti/master
Browse files Browse the repository at this point in the history
Allow concurrent, re-creatable usage
  • Loading branch information
pborman committed Apr 16, 2019
2 parents 0cd6bf5 + f00b204 commit c2e93f3
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
40 changes: 40 additions & 0 deletions uuid_test.go
Expand Up @@ -479,6 +479,46 @@ func TestBadRand(t *testing.T) {
}
}

func TestSetRand(t *testing.T) {
myString := "805-9dd6-1a877cb526c678e71d38-7122-44c0-9b7c-04e7001cc78783ac3e82-47a3-4cc3-9951-13f3339d88088f5d685a-11f7-4078-ada9-de44ad2daeb7"

SetRand(strings.NewReader(myString))
uuid1 := New()
uuid2 := New()

SetRand(strings.NewReader(myString))
uuid3 := New()
uuid4 := New()

if uuid1 != uuid3 {
t.Errorf("expected duplicates, got %q and %q", uuid1, uuid3)
}
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"
var asBytes = []byte(asString)

Expand Down
7 changes: 6 additions & 1 deletion version4.go
Expand Up @@ -27,12 +27,17 @@ 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 NewRandomFromReader(rander)
}

func NewRandomFromReader(r io.Reader) (UUID, error) {
var uuid UUID
_, err := io.ReadFull(rander, uuid[:])
_, err := io.ReadFull(r, uuid[:])
if err != nil {
return Nil, err
}
uuid[6] = (uuid[6] & 0x0f) | 0x40 // Version 4
uuid[8] = (uuid[8] & 0x3f) | 0x80 // Variant is 10
return uuid, nil
}

0 comments on commit c2e93f3

Please sign in to comment.