From 74c320fb48fab3af18e20d04d9040697b4741a8f Mon Sep 17 00:00:00 2001 From: it512 Date: Sat, 13 Jan 2024 13:17:22 +0800 Subject: [PATCH 1/2] TestVersion7MonotonicityStrict --- uuid_test.go | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/uuid_test.go b/uuid_test.go index 21118cf..3efc632 100644 --- a/uuid_test.go +++ b/uuid_test.go @@ -896,3 +896,30 @@ func TestVersion7Monotonicity(t *testing.T) { u1 = u2 } } + +type fakeRand struct{} + +func (g fakeRand) Read(bs []byte) (int, error) { + for i, _ := range bs { + bs[i] = 0x88 + } + return len(bs), nil +} + +func TestVersion7MonotonicityStrict(t *testing.T) { + timeNow = func() time.Time { + return time.Date(2008, 8, 8, 8, 8, 8, 8, time.UTC) + } + SetRand(fakeRand{}) + + length := 100000 // > 3906 + u1 := Must(NewV7()).String() + for i := 0; i < length; i++ { + u2 := Must(NewV7()).String() + if u2 <= u1 { + t.Errorf("monotonicity failed at #%d: %s(next) < %s(before)", i, u2, u1) + break + } + u1 = u2 + } +} From 6f04689d52bea3d183fe73f7b066fb538ac8ecd3 Mon Sep 17 00:00:00 2001 From: it512 Date: Sat, 13 Jan 2024 13:35:38 +0800 Subject: [PATCH 2/2] reset timeNow and rand --- uuid_test.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/uuid_test.go b/uuid_test.go index 3efc632..c1c6001 100644 --- a/uuid_test.go +++ b/uuid_test.go @@ -910,7 +910,12 @@ func TestVersion7MonotonicityStrict(t *testing.T) { timeNow = func() time.Time { return time.Date(2008, 8, 8, 8, 8, 8, 8, time.UTC) } + defer func() { + timeNow = time.Now + }() + SetRand(fakeRand{}) + defer SetRand(nil) length := 100000 // > 3906 u1 := Must(NewV7()).String()