ulid: simplify, add function without a timestamp
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package ulid
|
||||
|
||||
import (
|
||||
cryptorand "crypto/rand"
|
||||
"sort"
|
||||
"sync"
|
||||
"testing"
|
||||
@@ -36,6 +37,38 @@ func TestMakeULID(t *testing.T) {
|
||||
t.Logf("ulid string 1 and 2: %s | %s", ul1.String(), ul2.String())
|
||||
}
|
||||
|
||||
func TestMake(t *testing.T) {
|
||||
// Test Make() function (uses current time)
|
||||
ul1, err := Make()
|
||||
if err != nil {
|
||||
t.Fatalf("Make failed: %s", err)
|
||||
}
|
||||
|
||||
if ul1 == nil {
|
||||
t.Fatal("Make returned nil ULID")
|
||||
}
|
||||
|
||||
// Sleep a bit and generate another
|
||||
time.Sleep(2 * time.Millisecond)
|
||||
|
||||
ul2, err := Make()
|
||||
if err != nil {
|
||||
t.Fatalf("Make failed: %s", err)
|
||||
}
|
||||
|
||||
// Should be different ULIDs
|
||||
if ul1.String() == ul2.String() {
|
||||
t.Errorf("ULIDs from Make() should be different: %s", ul1.String())
|
||||
}
|
||||
|
||||
// Second should be later (or at least not earlier)
|
||||
if ul1.Time() > ul2.Time() {
|
||||
t.Errorf("second ULID should not have earlier timestamp: %d > %d", ul1.Time(), ul2.Time())
|
||||
}
|
||||
|
||||
t.Logf("Make() ULIDs: %s | %s", ul1.String(), ul2.String())
|
||||
}
|
||||
|
||||
func TestMakeULIDUniqueness(t *testing.T) {
|
||||
tm := time.Now()
|
||||
seen := make(map[string]bool)
|
||||
@@ -54,6 +87,23 @@ func TestMakeULIDUniqueness(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestMakeUniqueness(t *testing.T) {
|
||||
seen := make(map[string]bool)
|
||||
|
||||
for i := 0; i < 1000; i++ {
|
||||
ul, err := Make()
|
||||
if err != nil {
|
||||
t.Fatalf("Make failed on iteration %d: %s", i, err)
|
||||
}
|
||||
|
||||
str := ul.String()
|
||||
if seen[str] {
|
||||
t.Errorf("duplicate ULID generated: %s", str)
|
||||
}
|
||||
seen[str] = true
|
||||
}
|
||||
}
|
||||
|
||||
func TestMakeULIDTimestampProgression(t *testing.T) {
|
||||
t1 := time.Now()
|
||||
ul1, err := MakeULID(t1)
|
||||
@@ -79,34 +129,6 @@ func TestMakeULIDTimestampProgression(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestMakeULIDMonotonicity(t *testing.T) {
|
||||
tm := time.Now()
|
||||
var ulids []*oklid.ULID
|
||||
|
||||
// Generate ULIDs rapidly with same timestamp
|
||||
for i := 0; i < 100; i++ {
|
||||
ul, err := MakeULID(tm)
|
||||
if err != nil {
|
||||
t.Fatalf("MakeULID failed on iteration %d: %s", i, err)
|
||||
}
|
||||
ulids = append(ulids, ul)
|
||||
}
|
||||
|
||||
// Count non-monotonic pairs
|
||||
nonMonotonicCount := 0
|
||||
for i := 1; i < len(ulids); i++ {
|
||||
if ulids[i-1].Compare(*ulids[i]) >= 0 {
|
||||
nonMonotonicCount++
|
||||
}
|
||||
}
|
||||
|
||||
// Report summary if any non-monotonic pairs found
|
||||
if nonMonotonicCount > 0 {
|
||||
t.Logf("Note: %d out of %d ULID pairs with same timestamp were not monotonic due to pool usage",
|
||||
nonMonotonicCount, len(ulids)-1)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMakeULIDConcurrency(t *testing.T) {
|
||||
const numGoroutines = 10
|
||||
const numULIDsPerGoroutine = 100
|
||||
@@ -152,6 +174,50 @@ func TestMakeULIDConcurrency(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestMakeConcurrency(t *testing.T) {
|
||||
const numGoroutines = 10
|
||||
const numULIDsPerGoroutine = 100
|
||||
|
||||
var wg sync.WaitGroup
|
||||
ulidChan := make(chan *oklid.ULID, numGoroutines*numULIDsPerGoroutine)
|
||||
|
||||
// Start multiple goroutines generating ULIDs concurrently
|
||||
for i := 0; i < numGoroutines; i++ {
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
for j := 0; j < numULIDsPerGoroutine; j++ {
|
||||
ul, err := Make()
|
||||
if err != nil {
|
||||
t.Errorf("Make failed: %s", err)
|
||||
return
|
||||
}
|
||||
ulidChan <- ul
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
wg.Wait()
|
||||
close(ulidChan)
|
||||
|
||||
// Collect all ULIDs and check uniqueness
|
||||
seen := make(map[string]bool)
|
||||
count := 0
|
||||
|
||||
for ul := range ulidChan {
|
||||
str := ul.String()
|
||||
if seen[str] {
|
||||
t.Errorf("duplicate ULID generated in concurrent test: %s", str)
|
||||
}
|
||||
seen[str] = true
|
||||
count++
|
||||
}
|
||||
|
||||
if count != numGoroutines*numULIDsPerGoroutine {
|
||||
t.Errorf("expected %d ULIDs, got %d", numGoroutines*numULIDsPerGoroutine, count)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMakeULIDErrorHandling(t *testing.T) {
|
||||
// Test with various timestamps
|
||||
timestamps := []time.Time{
|
||||
@@ -160,13 +226,13 @@ func TestMakeULIDErrorHandling(t *testing.T) {
|
||||
time.Now().Add(time.Hour), // Future time
|
||||
}
|
||||
|
||||
for _, tm := range timestamps {
|
||||
for i, tm := range timestamps {
|
||||
ul, err := MakeULID(tm)
|
||||
if err != nil {
|
||||
t.Errorf("MakeULID failed with timestamp %v: %s", tm, err)
|
||||
t.Errorf("MakeULID failed with timestamp %d: %s", i, err)
|
||||
}
|
||||
if ul == nil {
|
||||
t.Errorf("MakeULID returned nil ULID with timestamp %v", tm)
|
||||
t.Errorf("MakeULID returned nil ULID with timestamp %d", i)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -223,6 +289,17 @@ func BenchmarkMakeULID(b *testing.B) {
|
||||
}
|
||||
}
|
||||
|
||||
// Benchmark Make function
|
||||
func BenchmarkMake(b *testing.B) {
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
_, err := Make()
|
||||
if err != nil {
|
||||
b.Fatalf("Make failed: %s", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Benchmark concurrent ULID generation
|
||||
func BenchmarkMakeULIDConcurrent(b *testing.B) {
|
||||
tm := time.Now()
|
||||
@@ -237,11 +314,23 @@ func BenchmarkMakeULIDConcurrent(b *testing.B) {
|
||||
})
|
||||
}
|
||||
|
||||
// Benchmark pool performance
|
||||
func BenchmarkMonotonicPool(b *testing.B) {
|
||||
// Benchmark concurrent Make function
|
||||
func BenchmarkMakeConcurrent(b *testing.B) {
|
||||
b.RunParallel(func(pb *testing.PB) {
|
||||
for pb.Next() {
|
||||
_, err := Make()
|
||||
if err != nil {
|
||||
b.Fatalf("Make failed: %s", err)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// Benchmark random number generation
|
||||
func BenchmarkCryptoRand(b *testing.B) {
|
||||
buf := make([]byte, 10) // ULID entropy size
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
_ = monotonicPool.Get()
|
||||
// Note: we don't put it back as the current implementation doesn't reuse readers
|
||||
cryptorand.Read(buf)
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user