forked from cchamplin/gocrush
-
Notifications
You must be signed in to change notification settings - Fork 0
/
unweightedhashselector_test.go
77 lines (71 loc) · 2.03 KB
/
unweightedhashselector_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
package gocrush
import (
"github.com/stretchr/testify/assert"
//"log"
"strconv"
"testing"
)
func TestUnwweightedHashSelector(t *testing.T) {
node := new(TestingNode)
node.Children = make([]Node, 8)
counter := make(map[string]int)
for i := 0; i < 8; i++ {
child := new(TestingNode)
child.Weight = 1
child.Id = "Child" + strconv.Itoa(i)
node.Children[i] = child
counter[child.Id] = 0
}
selector := NewUnweightedHashSelector(node)
for i := int64(0); i < 10000; i++ {
// Get replicants
for r := int64(0); r < 3; r++ {
nn := selector.Select(i, r)
counter[nn.GetId()] += 1
}
}
assert.Equal(t, counter["Child0"], 3745, "")
assert.Equal(t, counter["Child1"], 3759, "")
assert.Equal(t, counter["Child2"], 3758, "")
assert.Equal(t, counter["Child3"], 3695, "")
assert.Equal(t, counter["Child4"], 3751, "")
assert.Equal(t, counter["Child5"], 3695, "")
assert.Equal(t, counter["Child6"], 3803, "")
assert.Equal(t, counter["Child7"], 3794, "")
}
/*func TestUnweightedHashSelectorAdd(t *testing.T) {
node := new(TestingNode)
node.Children = make([]Node, 8)
counter := make(map[string]Node)
for i := 0; i < 8; i++ {
child := new(TestingNode)
child.Weight = 1
child.Id = "Child" + strconv.Itoa(i)
node.Children[i] = child
}
selector := NewUnweightedHashSelector(node)
for i := int64(0); i < 5; i++ {
for r := int64(0); r < 3; r++ {
nn := selector.Select(i, r)
counter[strconv.Itoa(int(i))+":"+strconv.Itoa(int(r))] = nn
//log.Printf("Node: %s selector for %d r%d", nn.Name, i, r)
}
}
//log.Printf("Round 2")
child := new(TestingNode)
child.Weight = 1
child.Id = "Child9"
node.Children = append(node.Children, child)
selector = NewUnweightedHashSelector(node)
for i := int64(0); i < 5; i++ {
for r := int64(0); r < 3; r++ {
nn := selector.Select(i, r)
if (i == 2 && r == 1) || (i == 4 && r == 2) {
assert.Equal(t, child, nn, "")
} else {
assert.Equal(t, counter[strconv.Itoa(int(i))+":"+strconv.Itoa(int(r))], nn, "")
}
//log.Printf("Node: %s selector for %d r%d", nn.Name, i, r)
}
}
}*/