Skip to content

Commit 16939da

Browse files
authored
chore(tests): add strict monotonicity test case for uuid v7. (#154)
* TestVersion7MonotonicityStrict * reset timeNow and rand
1 parent 016b199 commit 16939da

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

uuid_test.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -896,3 +896,35 @@ func TestVersion7Monotonicity(t *testing.T) {
896896
u1 = u2
897897
}
898898
}
899+
900+
type fakeRand struct{}
901+
902+
func (g fakeRand) Read(bs []byte) (int, error) {
903+
for i, _ := range bs {
904+
bs[i] = 0x88
905+
}
906+
return len(bs), nil
907+
}
908+
909+
func TestVersion7MonotonicityStrict(t *testing.T) {
910+
timeNow = func() time.Time {
911+
return time.Date(2008, 8, 8, 8, 8, 8, 8, time.UTC)
912+
}
913+
defer func() {
914+
timeNow = time.Now
915+
}()
916+
917+
SetRand(fakeRand{})
918+
defer SetRand(nil)
919+
920+
length := 100000 // > 3906
921+
u1 := Must(NewV7()).String()
922+
for i := 0; i < length; i++ {
923+
u2 := Must(NewV7()).String()
924+
if u2 <= u1 {
925+
t.Errorf("monotonicity failed at #%d: %s(next) < %s(before)", i, u2, u1)
926+
break
927+
}
928+
u1 = u2
929+
}
930+
}

0 commit comments

Comments
 (0)