-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrequests_test.go
208 lines (196 loc) · 4.72 KB
/
requests_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
package paxos
import (
"bytes"
"reflect"
"sync"
"testing"
"time"
)
const (
testTickInMillisecond uint64 = 50
)
func TestRequestStateRelease(t *testing.T) {
rs := RequestState{
data: make([]byte, 10),
key: 100,
deadline: 500,
pool: &sync.Pool{},
}
exp := RequestState{pool: rs.pool}
rs.Release()
if !reflect.DeepEqual(&exp, &rs) {
t.Errorf("unexpected state, got %+v, want %+v", rs, exp)
}
}
func getPendingProposal() (*pendingProposal, *entryQueue) {
c := newEntryQueue(5)
p := &sync.Pool{}
p.New = func() interface{} {
obj := &RequestState{}
obj.pool = p
obj.CompletedC = make(chan RequestResult, 1)
return obj
}
return newPendingProposal(p, c, 100, 120, "nodehost:12345", testTickInMillisecond), c
}
func TestPendingProposalCanBeCreatedAndClosed(t *testing.T) {
pp, c := getPendingProposal()
if _, ok := c.get(); ok {
t.Errorf("unexpected item in entry queue")
}
pp.close()
if !c.stopped {
t.Errorf("entry queue not closed")
}
}
func countPendingProposal(p *pendingProposal) int {
total := 0
for i := uint64(0); i < p.ps; i++ {
total += len(p.shards[i].pending)
}
return total
}
func TestProposalCanBeProposed(t *testing.T) {
pp, c := getPendingProposal()
rs, err := pp.propose([]byte("test data"), nil, time.Second)
if err != nil {
t.Errorf("failed to make proposal, %v", err)
}
if countPendingProposal(pp) != 1 {
t.Errorf("len(pending)=%d, want 1", countPendingProposal(pp))
}
select {
case <-rs.CompletedC:
t.Errorf("not suppose to have anything completed")
default:
}
_, ok := c.get()
if !ok {
t.Errorf("cannot get a propose from pending proposal")
}
pp.close()
select {
case v := <-rs.CompletedC:
if !v.Terminated() {
t.Errorf("get %d, want %d", v, requestTerminated)
}
default:
t.Errorf("suppose to return terminated")
}
}
func TestProposeOnClosedPendingProposalReturnError(t *testing.T) {
pp, _ := getPendingProposal()
pp.close()
_, err := pp.propose([]byte("test data"), nil, time.Second)
if err != ErrGroupClosed {
t.Errorf("unexpected err %v", err)
}
}
func TestProposalCanBeCompleted(t *testing.T) {
pp, _ := getPendingProposal()
rs, err := pp.propose([]byte("test data"), nil, time.Second)
if err != nil {
t.Errorf("failed to make proposal, %v", err)
}
pp.applied(rs.key+1, 0, false)
select {
case <-rs.CompletedC:
t.Errorf("unexpected applied proposal with invalid key")
default:
}
if countPendingProposal(pp) == 0 {
t.Errorf("pending is empty")
}
pp.applied(rs.key, 0, false)
select {
case v := <-rs.CompletedC:
if !v.Completed() {
t.Errorf("get %d, want %d", v, requestCompleted)
}
default:
t.Errorf("expect to get complete signal")
}
if countPendingProposal(pp) != 0 {
t.Errorf("pending is not empty")
}
}
func TestProposalCanBeExpired(t *testing.T) {
pp, _ := getPendingProposal()
timeout := time.Duration(1000 * time.Millisecond)
rs, err := pp.propose([]byte("test data"), nil, timeout)
if err != nil {
t.Errorf("failed to make proposal, %v", err)
}
tickCount := uint64(1000 / testTickInMillisecond)
for i := uint64(0); i < tickCount; i++ {
pp.increaseTick()
pp.gc()
}
plog.Infof("%v", pp.shards[0].getTick())
select {
case <-rs.CompletedC:
t.Errorf("not suppose to return anything")
default:
}
for i := uint64(0); i < defaultGCTick+1; i++ {
pp.increaseTick()
pp.gc()
}
select {
case v := <-rs.CompletedC:
if !v.Timeout() {
t.Errorf("got %d, want %d", v, requestTimeout)
}
default:
}
if countPendingProposal(pp) != 0 {
t.Errorf("pending/keys is not empty")
}
}
func TestProposalErrorsAreReported(t *testing.T) {
pp, _ := getPendingProposal()
for i := 0; i < 5; i++ {
_, err := pp.propose([]byte("test data"), nil, time.Second)
if err != nil {
t.Errorf("propose failed")
}
}
_, err := pp.propose([]byte("test data"), nil, time.Second)
if err != ErrSystemBusy {
t.Errorf("suppose to return ErrSystemBusy")
}
pp, _ = getPendingProposal()
var buffer bytes.Buffer
for i := uint64(0); i < maxProposalPayloadSize; i++ {
buffer.WriteString("a")
}
data := buffer.Bytes()
_, err = pp.propose(data, nil, time.Second)
if err != nil {
t.Errorf("suppose to be successful")
}
buffer.WriteString("a")
data = buffer.Bytes()
_, err = pp.propose(data, nil, time.Second)
if err != ErrPayloadTooBig {
t.Errorf("suppose to return ErrPayloadTooBig")
}
}
func TestClosePendingProposalIgnoresStepEngineActivities(t *testing.T) {
pp, _ := getPendingProposal()
rs, _ := pp.propose(nil, nil, time.Duration(5*time.Second))
select {
case <-rs.CompletedC:
t.Fatalf("completedC is already signalled")
default:
}
for i := uint64(0); i < pp.ps; i++ {
pp.shards[i].stopped = true
}
pp.applied(rs.key, 1, false)
select {
case <-rs.CompletedC:
t.Fatalf("completeC unexpectedly signaled")
default:
}
}