-
Notifications
You must be signed in to change notification settings - Fork 371
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added source to enable concurrent reproducible usage
- Loading branch information
Showing
2 changed files
with
95 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package uuid | ||
|
||
import ( | ||
"io" | ||
"crypto/rand" | ||
) | ||
|
||
type UuidSource struct { | ||
rander io.Reader | ||
} | ||
|
||
func NewSource(r io.Reader) UuidSource { | ||
var uuidSource UuidSource | ||
uuidSource.SetRand(r) | ||
return uuidSource | ||
|
||
} | ||
|
||
func (uuidSource *UuidSource) SetRand(r io.Reader) { | ||
if r == nil { | ||
uuidSource.rander = rand.Reader | ||
return | ||
} | ||
uuidSource.rander = r | ||
} | ||
|
||
func (uuidSource UuidSource) NewRandom() (UUID, error) { | ||
var uuid UUID | ||
_, err := io.ReadFull(uuidSource.rander, 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 | ||
} | ||
|
||
func (uuidSource UuidSource) New() UUID { | ||
return Must(uuidSource.NewRandom()) | ||
} |
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,55 @@ | ||
package uuid | ||
|
||
import ( | ||
"testing" | ||
"fmt" | ||
"strings" | ||
"math/rand" | ||
|
||
) | ||
|
||
func TestUuidSources(t *testing.T) { | ||
|
||
uuidSourceA := NewSource(rand.New(rand.NewSource(34576))) | ||
uuidSourceB := NewSource(rand.New(rand.NewSource(34576))) | ||
|
||
var uuidStrA, uuidStrB string | ||
fmt.Printf("Random values: ") | ||
for i := 0; i < 10; i++ { | ||
uuidStrA += uuidSourceA.New().String() + "." | ||
} | ||
fmt.Printf("%v\n", uuidStrA) | ||
|
||
fmt.Printf("Random values: ") | ||
for i := 0; i < 10; i++ { | ||
uuidStrB += uuidSourceB.New().String() + "." | ||
} | ||
fmt.Printf("%v\n", uuidStrB) | ||
|
||
if !strings.EqualFold(uuidStrA, uuidStrB) { | ||
t.Error("Uuid values are not reproducaible!") | ||
} | ||
|
||
uuidSourceA = NewSource(rand.New(rand.NewSource(66))) | ||
uuidSourceB = NewSource(rand.New(rand.NewSource(77))) | ||
|
||
|
||
uuidStrA = "" | ||
uuidStrB = "" | ||
fmt.Printf("Random values: ") | ||
for i := 0; i < 10; i++ { | ||
uuidStrA += uuidSourceA.New().String() + "." | ||
} | ||
fmt.Printf("%v\n", uuidStrA) | ||
|
||
fmt.Printf("Random values: ") | ||
for i := 0; i < 10; i++ { | ||
uuidStrB += uuidSourceB.New().String() + "." | ||
} | ||
fmt.Printf("%v\n", uuidStrB) | ||
|
||
if strings.EqualFold(uuidStrA, uuidStrB) { | ||
t.Error("Uuid values should not be reproducaible!") | ||
} | ||
|
||
} |