-
Notifications
You must be signed in to change notification settings - Fork 25
/
uuid_test.go
73 lines (65 loc) · 1.59 KB
/
uuid_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
package sharding_test
import (
"bytes"
"math/rand"
"testing"
"time"
"gopkg.in/go-pg/sharding.v5"
)
func TestUUIDParse(t *testing.T) {
sharding.SetRandSeed(rand.New(rand.NewSource(0)))
tm := time.Date(2000, time.January, 1, 0, 0, 0, 0, time.UTC)
uuid := sharding.NewUUID(0, tm)
got := uuid.String()
wanted := "00035d01-3b37-e000-0000-fdc2fa2ffcc0"
if got != wanted {
t.Fatalf("got %q, wanted %q", got, wanted)
}
parsed, err := sharding.ParseUUID([]byte(got))
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(parsed, uuid) {
t.Fatalf("got %x, wanted %x", parsed, uuid)
}
}
func TestUUIDTime(t *testing.T) {
shard := int64(2047)
for i := 0; i < 100000; i++ {
tm := time.Date(i, time.January, 1, 0, 0, 0, 0, time.UTC)
uuid := sharding.NewUUID(shard, tm)
gotShard, gotTm := uuid.Split()
if tm.Unix() != gotTm.Unix() {
t.Fatalf("got time %s, wanted %s", tm, gotTm)
}
if gotShard != shard {
t.Fatalf("got shard %d, wanted %d", gotShard, shard)
}
}
}
func TestUUIDShard(t *testing.T) {
tm := time.Now()
for shard := int64(0); shard < 2048; shard++ {
uuid := sharding.NewUUID(shard, tm)
gotShard, gotTm := uuid.Split()
if tm.Unix() != gotTm.Unix() {
t.Fatalf("got time %s, wanted %s", tm, gotTm)
}
if gotShard != shard {
t.Fatalf("got shard %d, wanted %d", gotShard, shard)
}
}
}
func TestUUIDCollision(t *testing.T) {
tm := time.Now()
shard := int64(2047)
m := map[string]struct{}{}
for i := 0; i < 1e6; i++ {
uuid := sharding.NewUUID(shard, tm)
_, ok := m[string(uuid)]
if ok {
t.Fatalf("collision for %s", uuid)
}
m[string(uuid)] = struct{}{}
}
}