-
Notifications
You must be signed in to change notification settings - Fork 18
/
queue_internal_test.go
234 lines (204 loc) · 4.43 KB
/
queue_internal_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
package cleisthenes
import (
"sync"
"testing"
"time"
"github.com/DE-labtory/cleisthenes/pb"
)
type mockTransaction struct {
name string
}
var mockTxVerifier = func(Transaction) bool {
return true
}
type MockHoneyBadger struct {
HandleContributionFunc func(contribution Contribution)
HandleMessageFunc func(msg *pb.Message) error
OnConsensusFunc func() bool
}
func (hb *MockHoneyBadger) HandleContribution(contribution Contribution) {
hb.HandleContributionFunc(contribution)
}
func (hb *MockHoneyBadger) HandleMessage(msg *pb.Message) error {
return nil
}
func (hb *MockHoneyBadger) OnConsensus() bool {
return hb.OnConsensusFunc()
}
func TestQueue_peek(t *testing.T) {
inputs := []Transaction{
&mockTransaction{
name: "a",
},
&mockTransaction{
name: "b",
},
&mockTransaction{
name: "c",
},
}
que := NewTxQueue()
_, err := que.peek()
if err == nil {
t.Fatalf("test failed : peek must return error when queue is empty")
}
for i, input := range inputs {
que.Push(input)
tx, _ := que.peek()
if tx != inputs[0] {
t.Fatalf("test[%d] failed - Peek must return first element of queue", i)
}
}
}
func TestQueue_Poll(t *testing.T) {
inputs := []Transaction{
&mockTransaction{
name: "a",
},
&mockTransaction{
name: "b",
},
&mockTransaction{
name: "c",
},
}
que := NewTxQueue()
for i, input := range inputs {
que.Push(input)
result, _ := que.Poll()
if result != input {
t.Fatalf("test[%d] failed : poll must return the first element or queue", i)
}
}
}
func TestQueue_empty(t *testing.T) {
que := NewTxQueue()
if !que.empty() {
t.Fatalf("test empty failed : after creating queue, empty() must return true")
}
que.Push(&mockTransaction{name: "tx"})
if que.empty() {
t.Fatalf("test empty failed : after pushing tx, empty() must return false")
}
que.Poll()
if !que.empty() {
t.Fatalf("test empty failed : after poll all element, empty() must return true")
}
}
func TestQueue_len(t *testing.T) {
que := NewTxQueue()
for i := 1; i < 10; i++ {
que.Push(&mockTransaction{name: "tx"})
if que.Len() != i {
t.Fatalf("test length failed : expected = %d, got = %d", i, que.Len())
}
}
}
func TestQueue_at(t *testing.T) {
inputs := []Transaction{
&mockTransaction{
name: "tx1",
},
&mockTransaction{
name: "tx2",
},
&mockTransaction{
name: "tx3",
},
&mockTransaction{
name: "tx4",
},
&mockTransaction{
name: "tx5",
},
}
que := NewTxQueue()
for _, input := range inputs {
que.Push(input)
}
for i := 0; i < 5; i++ {
result, _ := que.at(i)
if result != inputs[i] {
t.Fatalf("test[%d] failed. expected = %v, got = %v", i, inputs[i], result)
}
}
_, err := que.at(10)
if err == nil {
t.Fatalf("test failed. index boundary error must be occured")
}
}
func TestQueue_Push(t *testing.T) {
inputs := []Transaction{
&mockTransaction{
name: "a",
},
&mockTransaction{
name: "b",
},
&mockTransaction{
name: "c",
},
}
que := NewTxQueue()
for i, input := range inputs {
que.Push(input)
if result, _ := que.at(0); result != inputs[0] {
t.Fatalf("test[%d] failed - queue's first element is invalid", i)
}
if result, _ := que.at(que.Len() - 1); result != input {
t.Fatalf("test[%d] failed - queue's last element is invalid", i)
}
}
}
func TestDefaultTxQueueManager_AddTransactionAndProposeContribution(t *testing.T) {
wg := sync.WaitGroup{}
wg.Add(1)
inputs := []Transaction{
&mockTransaction{
name: "tx1",
},
&mockTransaction{
name: "tx2",
},
&mockTransaction{
name: "tx3",
},
}
batchSize := 3
nodeNum := 8
hb := &MockHoneyBadger{}
hb.HandleContributionFunc = func(contribution Contribution) {
if len(contribution.TxList) != len(inputs) {
t.Fatalf("expected contribution tx list is %d, but got %d", len(inputs), len(contribution.TxList))
}
for _, tx := range contribution.TxList {
included := false
name := tx.(*mockTransaction).name
for _, candidate := range inputs {
if name == candidate.(*mockTransaction).name {
included = true
}
}
if !included {
t.Fatalf("%s is not included in contribution", name)
}
}
wg.Done()
}
hb.OnConsensusFunc = func() bool {
return false
}
txValidator := func(tx Transaction) bool { return true }
manager := NewDefaultTxQueueManager(
NewTxQueue(),
hb,
batchSize,
batchSize*nodeNum,
time.Second,
txValidator,
)
for _, input := range inputs {
manager.AddTransaction(input)
}
wg.Wait()
}