forked from gholt/store
/
pullreplication_test.go
86 lines (77 loc) · 1.95 KB
/
pullreplication_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
package valuestore
import (
"sync"
"testing"
"github.com/gholt/ring"
)
type msgRingPullReplicationTester struct {
ring ring.Ring
lock sync.Mutex
msgToNodeIDs []uint64
headerToPartitions [][]byte
bodyToPartitions [][]byte
}
func (m *msgRingPullReplicationTester) Ring() ring.Ring {
return m.ring
}
func (m *msgRingPullReplicationTester) MaxMsgLength() uint64 {
return 65536
}
func (m *msgRingPullReplicationTester) SetMsgHandler(msgType uint64, handler ring.MsgUnmarshaller) {
}
func (m *msgRingPullReplicationTester) MsgToNode(nodeID uint64, msg ring.Msg) {
m.lock.Lock()
m.msgToNodeIDs = append(m.msgToNodeIDs, nodeID)
m.lock.Unlock()
msg.Done()
}
func (m *msgRingPullReplicationTester) MsgToOtherReplicas(ringVersion int64, partition uint32, msg ring.Msg) {
prm, ok := msg.(*pullReplicationMsg)
if ok {
m.lock.Lock()
h := make([]byte, len(prm.header))
copy(h, prm.header)
m.headerToPartitions = append(m.headerToPartitions, h)
b := make([]byte, len(prm.body))
copy(b, prm.body)
m.bodyToPartitions = append(m.bodyToPartitions, b)
m.lock.Unlock()
}
msg.Done()
}
func TestPullReplicationSimple(t *testing.T) {
b := ring.NewBuilder()
b.SetReplicaCount(2)
n := b.AddNode(true, 1, nil, nil, "", nil)
b.AddNode(true, 1, nil, nil, "", nil)
r := b.Ring()
r.SetLocalNode(n.ID())
m := &msgRingPullReplicationTester{ring: r}
vs := New(&Config{MsgRing: m})
vs.EnableAll()
defer vs.DisableAll()
_, err := vs.write(1, 2, 0x300, []byte("testing"))
if err != nil {
t.Fatal(err)
}
vs.OutPullReplicationPass()
m.lock.Lock()
v := len(m.headerToPartitions)
m.lock.Unlock()
if v == 0 {
t.Fatal(v)
}
mayHave := false
m.lock.Lock()
for i := 0; i < len(m.headerToPartitions); i++ {
prm := &pullReplicationMsg{vs: vs, header: m.headerToPartitions[i], body: m.bodyToPartitions[i]}
bf := prm.ktBloomFilter()
if bf.mayHave(1, 2, 0x300) {
mayHave = true
}
}
m.lock.Unlock()
if !mayHave {
t.Fatal("")
}
}