forked from sony/sonyflake
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhash_test.go
47 lines (39 loc) · 1.03 KB
/
hash_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
package snowflake
import (
"crypto/rand"
"fmt"
"math/big"
"testing"
"github.com/stretchr/testify/assert"
)
const (
testCharset = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
testPodsNumber = 100
)
func Test_hash(t *testing.T) {
t.Parallel()
prefix := "origin-vod-"
uniquePodNames := make(map[string]struct{})
for len(uniquePodNames) < testPodsNumber {
str := prefix + generateRandomString(10) + "-" + generateRandomString(5)
uniquePodNames[str] = struct{}{}
}
hashMap := make(map[uint16]struct{})
for name := range uniquePodNames {
h := hash(name, MachineIDBitMask)
assert.LessOrEqual(t, h, uint16(MachineIDBitMask))
hashMap[h] = struct{}{}
}
fmt.Printf("collisions generated: %d\n", len(uniquePodNames)-len(hashMap))
}
func generateRandomString(length int) string {
result := make([]byte, length)
for i := range result {
num, err := rand.Int(rand.Reader, big.NewInt(int64(len(testCharset))))
if err != nil {
panic(err)
}
result[i] = testCharset[num.Int64()]
}
return string(result)
}