-
Notifications
You must be signed in to change notification settings - Fork 0
/
random_test.go
110 lines (96 loc) · 2.06 KB
/
random_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// Random
// For the full copyright and license information, please view the LICENSE.txt file.
package random_test
import (
"sync"
"testing"
"github.com/devfacet/random"
)
func TestMustULID(t *testing.T) {
u := random.MustULID(true)
if u == "" {
t.Errorf("failed to create ULID (secure=true)")
}
u = random.MustULID(false)
if u == "" {
t.Errorf("failed to create ULID (secure=false)")
}
}
func TestMustULIDDuplicate(t *testing.T) {
var wg sync.WaitGroup
var mu sync.Mutex
limit := 1000
ul1 := make(map[string]int, limit)
dups1 := 0
for i := 0; i < limit; i++ {
wg.Add(1)
go func() {
defer wg.Done()
u := random.MustULID(true)
mu.Lock()
ul1[u]++
if ul1[u] > 1 {
dups1++
}
mu.Unlock()
}()
}
if dups1 > 0 {
t.Errorf("failed to create unique ULIDs (secure=true): %d duplicates", dups1)
}
ul2 := make(map[string]int, limit)
dups2 := 0
for i := 0; i < limit; i++ {
wg.Add(1)
go func() {
u := random.MustULID(false)
mu.Lock()
ul2[u]++
if ul2[u] > 1 {
dups2++
}
mu.Unlock()
wg.Done()
}()
}
if dups2 > 0 {
t.Errorf("failed to create unique ULIDs (secure=false): %d duplicates", dups2)
}
wg.Wait()
}
func BenchmarkMustULID(b *testing.B) {
for i := 0; i < b.N; i++ {
random.MustULID(true)
}
}
func BenchmarkMustULIDInsecure(b *testing.B) {
for i := 0; i < b.N; i++ {
random.MustULID(false)
}
}
func TestMustBytes(t *testing.T) {
b := random.MustBytes(32, true)
if l := len(b); l != 32 {
t.Errorf("failed to create random bytes (secure=true): %d bytes", l)
}
b = random.MustBytes(32, false)
if l := len(b); l != 32 {
t.Errorf("failed to create random bytes (secure=false): %d bytes", l)
}
}
func BenchmarkMustBytes(b *testing.B) {
for i := 0; i < b.N; i++ {
random.MustBytes(32, true)
}
}
func BenchmarkMustBytesInsecure(b *testing.B) {
for i := 0; i < b.N; i++ {
random.MustBytes(32, false)
}
}
func TestMillisecond(t *testing.T) {
d := random.Millisecond(1, 5000)
if d.Milliseconds() < 1 || d.Milliseconds() > 5000 {
t.Errorf("failed to create correct duration: %s)", d)
}
}