better API for changing epoch calculation function

This commit is contained in:
Maxim Bublis 2013-07-09 17:10:32 +04:00
parent 65354884e6
commit ccb23b2132
2 changed files with 10 additions and 5 deletions

10
uuid.go
View File

@ -79,12 +79,14 @@ func unixTimeFunc() uint64 {
return epochStart + uint64(time.Now().UnixNano() / 100) return epochStart + uint64(time.Now().UnixNano() / 100)
} }
// Returns current epoch calculation function.
func GetEpochFunc() (func() uint64) {
return epochFunc
}
// Sets epoch calculation function. // Sets epoch calculation function.
// Returns old function. func SetEpochFunc(newEpochFunc func() uint64) {
func SetEpochFunc(newEpochFunc func() uint64) (func() uint64) {
oldEpochFunc := epochFunc
epochFunc = newEpochFunc epochFunc = newEpochFunc
return oldEpochFunc
} }
// UUID representation compliant with specification // UUID representation compliant with specification

View File

@ -95,13 +95,16 @@ func TestNewV1(t *testing.T) {
t.Errorf("UUIDv1 generated two equal UUIDs: %s and %s", u1, u2) t.Errorf("UUIDv1 generated two equal UUIDs: %s and %s", u1, u2)
} }
oldFunc := uuid.SetEpochFunc(func() uint64 { return 0}) oldFunc := uuid.GetEpochFunc()
uuid.SetEpochFunc(func() uint64 { return 0})
u3, _ := uuid.NewV1() u3, _ := uuid.NewV1()
u4, _ := uuid.NewV1() u4, _ := uuid.NewV1()
if uuid.Equal(u3, u4) { if uuid.Equal(u3, u4) {
t.Errorf("UUIDv1 generated two equal UUIDs: %s and %s", u3, u4) t.Errorf("UUIDv1 generated two equal UUIDs: %s and %s", u3, u4)
} }
uuid.SetEpochFunc(oldFunc) uuid.SetEpochFunc(oldFunc)
} }