-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathvaluepullreplication_GEN_test.go
99 lines (90 loc) · 2.35 KB
/
valuepullreplication_GEN_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
87
88
89
90
91
92
93
94
95
96
97
98
99
package store
import (
"sync"
"testing"
"time"
ring "github.com/gholt/devicering"
"github.com/gholt/msgring"
"golang.org/x/net/context"
)
type msgRingValuePullReplicationTester struct {
ring ring.Ring
lock sync.Mutex
msgToNodeIDs []uint64
headerToPartitions [][]byte
bodyToPartitions [][]byte
}
func (m *msgRingValuePullReplicationTester) Ring() ring.Ring {
return m.ring
}
func (m *msgRingValuePullReplicationTester) MaxMsgLength() uint64 {
return 65536
}
func (m *msgRingValuePullReplicationTester) SetMsgHandler(msgType uint64, handler msgring.MsgUnmarshaller) {
}
func (m *msgRingValuePullReplicationTester) MsgToNode(msg msgring.Msg, nodeID uint64, timeout time.Duration) {
m.lock.Lock()
m.msgToNodeIDs = append(m.msgToNodeIDs, nodeID)
m.lock.Unlock()
msg.Free(0, 0)
}
func (m *msgRingValuePullReplicationTester) MsgToOtherReplicas(msg msgring.Msg, partition uint32, timeout time.Duration) {
prm, ok := msg.(*valuePullReplicationMsg)
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.Free(0, 0)
}
func TestValuePullReplicationSimple(t *testing.T) {
b := ring.NewBuilder(64)
b.SetReplicaCount(2)
n, err := b.AddNode(true, 1, nil, nil, "", nil)
if err != nil {
t.Fatal(err)
}
_, err = b.AddNode(true, 1, nil, nil, "", nil)
if err != nil {
t.Fatal(err)
}
r := b.Ring()
r.SetLocalNode(n.ID())
m := &msgRingValuePullReplicationTester{ring: r}
cfg := newTestValueStoreConfig()
cfg.MsgRing = m
store, _ := newTestValueStore(cfg)
if err := store.Startup(context.Background()); err != nil {
t.Fatal(err)
}
defer store.Shutdown(context.Background())
_, err = store.write(1, 2, 0x500, []byte("testing"), false)
if err != nil {
t.Fatal(err)
}
store.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 := &valuePullReplicationMsg{store: store, header: m.headerToPartitions[i], body: m.bodyToPartitions[i]}
bf := prm.ktBloomFilter()
if bf.mayHave(1, 2, 0x500) {
mayHave = true
}
}
m.lock.Unlock()
if !mayHave {
t.Fatal("")
}
}