File tree 2 files changed +34
-1
lines changed 2 files changed +34
-1
lines changed Original file line number Diff line number Diff line change @@ -3,6 +3,7 @@ package idgen
3
3
import (
4
4
"errors"
5
5
"io"
6
+ "math"
6
7
"math/rand"
7
8
"strings"
8
9
"sync"
@@ -35,7 +36,8 @@ func New(prefix string) string {
35
36
36
37
var entropyPool = sync.Pool {
37
38
New : func () interface {} {
38
- return ulid .Monotonic (rand .New (rand .NewSource (time .Now ().UnixNano ())), 0 )
39
+ ns := time .Now ().UnixNano ()
40
+ return ulid .Monotonic (rand .New (rand .NewSource (ns )), uint64 (ns )% math .MaxUint32 )
39
41
},
40
42
}
41
43
Original file line number Diff line number Diff line change 5
5
"net/url"
6
6
"sort"
7
7
"strings"
8
+ "sync"
8
9
"testing"
9
10
"time"
10
11
@@ -52,6 +53,36 @@ func TestUniqueness(t *testing.T) {
52
53
}
53
54
}
54
55
56
+ func TestUniquenessParallel (t * testing.T ) {
57
+ var wg sync.WaitGroup
58
+
59
+ ids := make (chan string , 1000 )
60
+ for i := 0 ; i < 100 ; i ++ {
61
+ go func (ids chan <- string ) {
62
+ wg .Add (1 )
63
+ defer wg .Done ()
64
+
65
+ for i := 0 ; i < 100 ; i ++ {
66
+ ids <- idgen .New ("usr" )
67
+ }
68
+ }(ids )
69
+ }
70
+
71
+ go func () {
72
+ wg .Wait ()
73
+ close (ids )
74
+ }()
75
+
76
+ set := map [string ]bool {}
77
+ for id := range ids {
78
+ if set [id ] {
79
+ t .Errorf ("generating repeated strings" )
80
+ }
81
+
82
+ set [id ] = true
83
+ }
84
+ }
85
+
55
86
func TestLexicalOrder (t * testing.T ) {
56
87
var ii [1000 ]string
57
88
for k := range ii {
You can’t perform that action at this time.
0 commit comments