-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspaxoslog_test.go
259 lines (215 loc) · 5.61 KB
/
spaxoslog_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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
package spaxos
import (
"bytes"
"flag"
// "fmt"
"os"
"runtime"
"testing"
"time"
// pb "spaxos/spaxospb"
pb "github.com/dengoswei/spaxos/spaxospb"
)
var testslog []*SpaxosLog
var testsw []*SSwitch
func setupSpaxosLogTest() {
printIndicate()
assert(nil == testslog)
assert(nil == testsw)
for id := uint64(1); id <= uint64(3); id++ {
c := NewDefaultConfig()
assert(nil != c)
c.Selfid = id
assert(3 == len(c.Groups))
db := NewFakeStorage()
assert(nil != db)
slog, err := NewSpaxosLog(c, db)
assert(nil == err)
assert(nil != slog)
sw, err := NewSwitch(c)
assert(nil == err)
assert(nil != sw)
go slog.Run(sw)
go sw.Run()
testslog = append(testslog, slog)
testsw = append(testsw, sw)
}
}
func teardownSpaxosLogTest() {
assert(nil != testslog)
assert(nil != testsw)
assert(len(testslog) == len(testsw))
for i := 0; i < len(testslog); i++ {
testslog[i].Stop()
testsw[i].Stop()
}
}
func TestNewSpaxosLog(t *testing.T) {
printIndicate()
c := NewDefaultConfig()
assert(nil != c)
db := NewFakeStorage()
assert(nil != db)
slog, err := NewSpaxosLog(c, db)
assert(nil == err)
assert(nil != slog)
}
func TestSpaxosLogRunAndStop(t *testing.T) {
printIndicate()
c := NewDefaultConfig()
assert(nil != c)
db := NewFakeStorage()
assert(nil != db)
slog, err := NewSpaxosLog(c, db)
assert(nil == err)
assert(nil != slog)
sw := NewFakeSwitch(c.Selfid)
assert(nil != sw)
go slog.Run(sw)
time.Sleep(1 * time.Millisecond)
slog.Stop()
}
func waitUntil(slog *SpaxosLog, beginIndex uint64,
reqids []uint64, values [][][]byte, hostReqidMap map[uint64]uint64) {
assert(0 < beginIndex)
for {
err := slog.Wait()
assert(nil == err)
cnt, err := slog.Get(beginIndex, reqids, values, hostReqidMap)
if 0 != cnt {
break
}
LogDebug("%s hostid %d slog.Get cnt %d",
GetCurrentFuncName(), slog.sp.id, cnt)
}
}
func busyWaitUntil(slog *SpaxosLog, beginIndex uint64,
reqids []uint64, values [][][]byte, hostReqidMap map[uint64]uint64) {
assert(0 < beginIndex)
for {
cnt, err := slog.Get(beginIndex, reqids, values, hostReqidMap)
assert(nil == err)
if 0 != cnt {
break
}
LogDebug("%s hostid %d slog.Get cnt %d",
GetCurrentFuncName(), slog.sp.id, cnt)
time.Sleep(10 * time.Millisecond)
}
}
func TestSpaxosLogSimplePropose(t *testing.T) {
printIndicate()
assert(0 < len(testslog))
assert(0 < len(testsw))
slog := testslog[0]
assert(nil != slog)
// case 1
{
// Get on empty spaxos log
reqids := make([]uint64, 1)
values := make([][][]byte, 1)
cnt, err := slog.Get(1, reqids, values, nil)
assert(nil == err)
assert(0 == cnt)
}
// case 2: sp 1 prop, all sp reach consensus
{
propItem := randPropItem()
assert(nil != propItem)
err := slog.Propose(propItem.Reqid, propItem.Values[0], false)
assert(nil == err)
reqids := make([]uint64, 1)
values := make([][][]byte, 1)
hostReqidMap := make(map[uint64]uint64)
// TODO: fix busy wait;
// should be more go style:
// busyWaitUntil(slog, 1, reqids, values, hostReqidMap)
waitUntil(slog, 1, reqids, values, hostReqidMap)
assert(1 == len(reqids))
assert(1 == len(values))
assert(1 == len(hostReqidMap))
assert(uint64(1) == hostReqidMap[propItem.Reqid])
assert(reqids[0] == propItem.Reqid)
assert(0 == bytes.Compare(propItem.Values[0], values[0][0]))
// read chosen item from other peers
for i := 1; i < len(testslog); i++ {
oreqids := make([]uint64, 1)
ovalues := make([][][]byte, 1)
ohostReqidMap := make(map[uint64]uint64)
assert(slog.sp.id != testslog[i].sp.id)
// busyWaitUntil(testslog[i], 1, oreqids, ovalues, ohostReqidMap)
waitUntil(testslog[i], 1, oreqids, ovalues, ohostReqidMap)
assert(1 == len(oreqids))
assert(1 == len(ovalues))
assert(0 == len(ohostReqidMap))
assert(oreqids[0] == propItem.Reqid)
assert(0 == bytes.Compare(propItem.Values[0], ovalues[0][0]))
}
}
// case 3: multi prop
{
propItems := []*pb.ProposeItem{}
propReqids := make(map[uint64]bool)
for i := 0; i < 2; i++ {
propItem := randPropItem()
propReqids[propItem.Reqid] = true
propItems = append(propItems, propItem)
err := testslog[i].Propose(propItem.Reqid, propItem.Values[0], false)
assert(nil == err)
}
// wait one testslog[0]: slog
hostSuccCnt := 0
{
reqids := make([]uint64, 1)
values := make([][][]byte, 1)
hostReqidMap := make(map[uint64]uint64)
// busyWaitUntil(slog, 2, reqids, values, hostReqidMap)
waitUntil(slog, 2, reqids, values, hostReqidMap)
assert(1 == len(reqids))
assert(1 == len(values))
assert(1 == len(hostReqidMap))
if reqids[0] == propItems[0].Reqid {
hostSuccCnt++
assert(0 == bytes.Compare(propItems[0].Values[0], values[0][0]))
} else {
assert(reqids[0] == propItems[1].Reqid)
assert(0 == bytes.Compare(propItems[1].Values[0], values[0][0]))
}
}
assert(0 <= hostSuccCnt)
{
for {
oreqids := make([]uint64, 2)
ovalues := make([][][]byte, 2)
ohostReqidMap := make(map[uint64]uint64)
// busyWaitUntil(testslog[1], 2, oreqids, ovalues, ohostReqidMap)
waitUntil(testslog[1], 2, oreqids, ovalues, ohostReqidMap)
assert(1 <= len(oreqids))
assert(1 <= len(ovalues))
assert(1 == len(ohostReqidMap))
oindex, ok := ohostReqidMap[propItems[1].Reqid]
if !ok {
println("hostid %d prop not yet ready")
continue
}
assert(0 < oindex)
if oreqids[oindex-1] == propItems[1].Reqid {
hostSuccCnt++
}
break
}
}
}
}
func TestMain(m *testing.M) {
flag.Parse()
runtime.GOMAXPROCS(runtime.NumCPU())
var retcode int
{
setupSpaxosLogTest()
retcode = m.Run()
teardownSpaxosLogTest()
}
println("NumCPU", runtime.NumCPU())
os.Exit(retcode)
}