forked from mirror/go.uuid
better API for changing epoch calculation function
This commit is contained in:
parent
65354884e6
commit
ccb23b2132
10
uuid.go
10
uuid.go
|
@ -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
|
||||||
|
|
|
@ -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)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue