github.com/benz9527/xboot@v0.0.0-20240504061247-c23f15593274/lib/id/monotonic_id_test.go (about) 1 package id 2 3 import ( 4 "math" 5 "testing" 6 7 "github.com/stretchr/testify/require" 8 ) 9 10 func TestMonotonicNonZeroID(t *testing.T) { 11 id := new(monotonicNonZeroID) 12 id.val = math.MaxUint64 13 require.Equal(t, uint64(1), id.next()) 14 15 gen, err := MonotonicNonZeroID() 16 require.Nil(t, err) 17 require.Equal(t, uint64(1), gen.Number()) 18 for i := 0; i < 1000; i++ { 19 require.Less(t, gen.Number(), gen.Number()) 20 } 21 require.NotEqual(t, gen.Str(), gen.Str()) 22 } 23 24 func BenchmarkMonotonicNonZeroID(b *testing.B) { 25 gen, err := MonotonicNonZeroID() 26 require.Nil(b, err) 27 b.ResetTimer() 28 for i := 0; i < b.N; i++ { 29 _ = gen.Number() 30 } 31 b.ReportAllocs() 32 }