forked from sony/sonyflake
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnowflake_test.go
55 lines (45 loc) · 1.05 KB
/
snowflake_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
package snowflake
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func Test_Snowflake(t *testing.T) {
t.Parallel()
podsNum := 10
prefix := "origin-vod-"
uniquePodNames := make(map[string]struct{})
for len(uniquePodNames) < podsNum {
str := prefix + generateRandomString(10) + "-" + generateRandomString(5)
uniquePodNames[str] = struct{}{}
}
instances := make([]*Snowflake, 0, podsNum)
for name := range uniquePodNames {
snowflake, err := New(Settings{
StartTime: time.Now(),
MachineID: name,
})
assert.NoError(t, err)
instances = append(instances, snowflake)
}
consumer := make(chan int64)
IDnum := 10000
generate := func(sf *Snowflake) {
for i := 0; i < IDnum; i++ {
id, err := sf.NextID()
assert.NoError(t, err)
consumer <- id
}
}
for _, i := range instances {
go generate(i)
}
set := make(map[int64]struct{})
for i := 0; i < IDnum*podsNum; i++ {
id := <-consumer
assert.GreaterOrEqual(t, id, int64(0))
set[id] = struct{}{}
}
// collisions num
assert.Equal(t, 0, IDnum*podsNum-len(set))
}